请选择 进入手机版 | 继续访问电脑版

学JAVA网

 找回密码
 立即注册

easyui datagrid源码

[复制链接]
发表于 2018-7-19 10:09:26 |显示全部楼层

    在学习Easyui的过程中我们可以发现 虽然easyui的源码大部分都是对我们开放的,但是datagrid和tree的源码却是加密的。

    在这里 我们找到了在easyui将datagrid加密之前的一个版本 虽然是旧的版本 但是对于我们学习datagrid来说 应该也是足够了。
  1. /**
  2. * jQuery EasyUI 1.2.3
  3. *
  4. * Licensed under the GPL terms
  5. * To use it on other terms please contact us
  6. *
  7. * Copyright(c) 2009-2011 stworthy [ stworthy@gmail.com ]
  8. *
  9. */
  10. (function ($) {
  11. $.extend(Array.prototype, { indexOf: function (o) {
  12. for (var i = 0, len = this.length; i < len; i++) {
  13. if (this[i] == o) {
  14. return i;
  15. }
  16. }
  17. return -1;
  18. }, remove: function (o) {
  19. var index = this.indexOf(o);
  20. if (index != -1) {
  21. this.splice(index, 1);
  22. }
  23. return this;
  24. }, removeById: function (filed, id) {
  25. for (var i = 0, len = this.length; i < len; i++) {
  26. if (this[i][filed] == id) {
  27. this.splice(i, 1);
  28. return this;
  29. }
  30. }
  31. return this;
  32. }
  33. });


  34. function setSize(target, param) {
  35. var opts = $.data(target, "datagrid").options;
  36. var pnl = $.data(target, "datagrid").panel;
  37. if (param) {
  38. if (param.width) {
  39. opts.width = param.width;
  40. }
  41. if (param.height) {
  42. opts.height = param.height;
  43. }
  44. }
  45. if (opts.fit == true) {
  46. var p = pnl.panel("panel").parent();
  47. opts.width = p.width();
  48. opts.height = p.height();
  49. }
  50. pnl.panel("resize", { width: opts.width, height: opts.height });
  51. };



  52. function fixDataGridHeight(target) {
  53. var opts = $.data(target, "datagrid").options;
  54. var pnl = $.data(target, "datagrid").panel;
  55. var pnlwidth = pnl.width();
  56. var pnlheight = pnl.height();
  57. var view = pnl.children("div.datagrid-view");
  58. var view1 = view.children("div.datagrid-view1");
  59. var view2 = view.children("div.datagrid-view2");
  60. var view1header = view1.children("div.datagrid-header");
  61. var view2header = view2.children("div.datagrid-header");
  62. var view1headerTable = view1header.find("table");
  63. var view2headerTable = view2header.find("table");
  64. view.width(pnlwidth);
  65. var view1headerInner = view1header.children("div.datagrid-header-inner").show();
  66. view1.width(view1headerInner.find("table").width());
  67. if (!opts.showHeader) {
  68. view1headerInner.hide();
  69. }
  70. view2.width(pnlwidth - view1.outerWidth());
  71. view1.children("div.datagrid-header,div.datagrid-body,div.datagrid-footer").width(view1.width());
  72. view2.children("div.datagrid-header,div.datagrid-body,div.datagrid-footer").width(view2.width());
  73. var hh;
  74. view1header.css("height", "");
  75. view2header.css("height", "");
  76. view1headerTable.css("height", "");
  77. view2headerTable.css("height", "");
  78. hh = Math.max(view1headerTable.height(), view2headerTable.height());
  79. view1headerTable.height(hh);
  80. view2headerTable.height(hh);
  81. if ($.boxModel == true) {
  82. view1header.height(hh - (view1header.outerHeight() - view1header.height()));
  83. view2header.height(hh - (view2header.outerHeight() - view2header.height()));
  84. } else {
  85. view1header.height(hh);
  86. view2header.height(hh);
  87. }
  88. if (opts.height != "auto") {
  89. var bodyHeight = pnlheight - view2.children("div.datagrid-header").outerHeight(true) - view2.children("div.datagrid-footer").outerHeight(true) - pnl.children("div.datagrid-toolbar").outerHeight(true) - pnl.children("div.datagrid-pager").outerHeight(true);
  90. view1.children("div.datagrid-body").height(bodyHeight);
  91. view2.children("div.datagrid-body").height(bodyHeight);
  92. }
  93. view.height(view2.height());
  94. view2.css("left", view1.outerWidth());
  95. };


  96. function fixRowHeight(target, index) {
  97. var rows = $.data(target, "datagrid").data.rows;
  98. var opts = $.data(target, "datagrid").options;
  99. var pnl = $.data(target, "datagrid").panel;
  100. var view = pnl.children("div.datagrid-view");
  101. var view1 = view.children("div.datagrid-view1");
  102. var view2 = view.children("div.datagrid-view2");
  103. if (!view1.find("div.datagrid-body-inner").is(":empty")) {
  104. if (index >= 0) {
  105. fixSingleRowHeight(index);
  106. } else {
  107. for (var i = 0; i < rows.length; i++) {
  108. fixSingleRowHeight(i);
  109. }
  110. if (opts.showFooter) {
  111. var footerRows = $(target).datagrid("getFooterRows") || [];
  112. var c1 = view1.children("div.datagrid-footer");
  113. var c2 = view2.children("div.datagrid-footer");
  114. for (var i = 0; i < footerRows.length; i++) {
  115. fixSingleRowHeight(i, c1, c2);
  116. }
  117. fixDataGridHeight(target);
  118. }
  119. }
  120. }
  121. if (opts.height == "auto") {
  122. var view1body = view1.children("div.datagrid-body");
  123. var view2body = view2.children("div.datagrid-body");
  124. var outerHeightTotal = 0;
  125. var tmpHeight = 0;
  126. view2body.children().each(function () {
  127. var c = $(this);
  128. if (c.is(":visible")) {
  129. outerHeightTotal += c.outerHeight();
  130. if (tmpHeight < c.outerWidth()) {
  131. tmpHeight = c.outerWidth();
  132. }
  133. }
  134. });
  135. if (tmpHeight > view2body.width()) {
  136. outerHeightTotal += 18;
  137. }
  138. view1body.height(outerHeightTotal);
  139. view2body.height(outerHeightTotal);
  140. view.height(view2.height());
  141. }
  142. view2.children("div.datagrid-body").triggerHandler("scroll");
  143. function fixSingleRowHeight(index, c1, c2) {
  144. c1 = c1 || view1;
  145. c2 = c2 || view2;
  146. var tr1 = c1.find("tr[datagrid-row-index=" + index + "]");
  147. var tr2 = c2.find("tr[datagrid-row-index=" + index + "]");
  148. tr1.css("height", "");
  149. tr2.css("height", "");
  150. var maxheight = Math.max(tr1.height(), tr2.height());
  151. tr1.css("height", maxheight);
  152. tr2.css("height", maxheight);
  153. };
  154. };


  155. /****
  156. *
  157. ****/
  158. function wrapGrid(target, rownumbers) {
  159. function getDataByCondition(conditons) {
  160. var trAry = [];
  161. $("tr", conditons).each(function () {
  162. var thAry = [];
  163. $("th", this).each(function () {
  164. var th = $(this);
  165. var col = { title: th.html(), align: th.attr("align") || "left", sortable: th.attr("sortable") == "true" || false, checkbox: th.attr("checkbox") == "true" || false };
  166. if (th.attr("field")) {
  167. col.field = th.attr("field");
  168. }
  169. if (th.attr("formatter")) {
  170. col.formatter = eval(th.attr("formatter"));
  171. }
  172. if (th.attr("styler")) {
  173. col.styler = eval(th.attr("styler"));
  174. }
  175. if (th.attr("editor")) {
  176. var s = $.trim(th.attr("editor"));
  177. if (s.substr(0, 1) == "{") {
  178. col.editor = eval("(" + s + ")");
  179. } else {
  180. col.editor = s;
  181. }
  182. }
  183. if (th.attr("rowspan")) {
  184. col.rowspan = parseInt(th.attr("rowspan"));
  185. }
  186. if (th.attr("colspan")) {
  187. col.colspan = parseInt(th.attr("colspan"));
  188. }
  189. if (th.attr("width")) {
  190. col.width = parseInt(th.attr("width"));
  191. }
  192. if (th.attr("hidden")) {
  193. col.hidden = th.attr("hidden") == "true";
  194. }
  195. if (th.attr("resizable")) {
  196. col.resizable = th.attr("resizable") == "true";
  197. }
  198. thAry.push(col);
  199. });
  200. trAry.push(thAry);
  201. });
  202. return trAry;
  203. };
  204. var wrapObj = $("<div class=\"datagrid-wrap\">" + "<div class=\"datagrid-view\">" + "<div class=\"datagrid-view1\">" + "<div class=\"datagrid-header\">" + "<div class=\"datagrid-header-inner\"></div>" + "</div>" + "<div class=\"datagrid-body\">" + "<div class=\"datagrid-body-inner\"></div>" + "</div>" + "<div class=\"datagrid-footer\">" + "<div class=\"datagrid-footer-inner\"></div>" + "</div>" + "</div>" + "<div class=\"datagrid-view2\">" + "<div class=\"datagrid-header\">" + "<div class=\"datagrid-header-inner\"></div>" + "</div>" + "<div class=\"datagrid-body\"></div>" + "<div class=\"datagrid-footer\">" + "<div class=\"datagrid-footer-inner\"></div>" + "</div>" + "</div>" + "<div class=\"datagrid-resize-proxy\"></div>" + "</div>" + "</div>").insertAfter(target);
  205. wrapObj.panel({ doSize: false });
  206. wrapObj.panel("panel").addClass("datagrid").bind("_resize", function (e, _32) {
  207. var opts = $.data(target, "datagrid").options;
  208. if (opts.fit == true || _32) {
  209. setSize(target);
  210. setTimeout(function () {
  211. if ($.data(target, "datagrid")) {
  212. fixColumnSize(target);
  213. }
  214. }, 0);
  215. }
  216. return false;
  217. });
  218. $(target).hide().appendTo(wrapObj.children("div.datagrid-view"));
  219. var dataFrozen = getDataByCondition($("thead[frozen=true]", target));
  220. var dataFit = getDataByCondition($("thead[frozen!=true]", target));
  221. return { panel: wrapObj, frozenColumns: dataFrozen, columns: dataFit };
  222. };



  223. function getData(target) {
  224. var data = { total: 0, rows: [] };
  225. var getCols = getColumnFiles(target, true).concat(getColumnFiles(target, false));
  226. $(target).find("tbody tr").each(function () {
  227. data.total++;
  228. var col = {};
  229. for (var i = 0; i < getCols.length; i++) {
  230. col[getCols[i]] = $("td:eq(" + i + ")", this).html();
  231. }
  232. data.rows.push(col);
  233. });
  234. return data;
  235. };


  236. function initDatagrid(target) {
  237. var opts = $.data(target, "datagrid").options;
  238. var pnl = $.data(target, "datagrid").panel;
  239. pnl.panel($.extend({}, opts, { doSize: false, onResize: function (_40, _41) {
  240. setTimeout(function () {
  241. if ($.data(target, "datagrid")) {
  242. fixDataGridHeight(target);
  243. fixColumnWidth(target);
  244. opts.onResize.call(pnl, _40, _41);
  245. }
  246. }, 0);
  247. }, onExpand: function () {
  248. fixDataGridHeight(target);
  249. fixRowHeight(target);
  250. opts.onExpand.call(pnl);
  251. }
  252. }));
  253. var view = pnl.children("div.datagrid-view");
  254. var view1 = view.children("div.datagrid-view1");
  255. var view2 = view.children("div.datagrid-view2");
  256. var view1HeaderInner = view1.children("div.datagrid-header").children("div.datagrid-header-inner");
  257. var view2HeaderInner = view2.children("div.datagrid-header").children("div.datagrid-header-inner");
  258. appendColumns(view1HeaderInner, opts.frozenColumns, true);
  259. appendColumns(view2HeaderInner, opts.columns, false);
  260. view1HeaderInner.css("display", opts.showHeader ? "block" : "none");
  261. view2HeaderInner.css("display", opts.showHeader ? "block" : "none");
  262. view1.find("div.datagrid-footer-inner").css("display", opts.showFooter ? "block" : "none");
  263. view2.find("div.datagrid-footer-inner").css("display", opts.showFooter ? "block" : "none");
  264. if (opts.toolbar) {
  265. if (typeof opts.toolbar == "string") {
  266. $(opts.toolbar).addClass("datagrid-toolbar").prependTo(pnl);
  267. $(opts.toolbar).show();
  268. } else {
  269. $("div.datagrid-toolbar", pnl).remove();
  270. var tb = $("<div class=\"datagrid-toolbar\"></div>").prependTo(pnl);
  271. for (var i = 0; i < opts.toolbar.length; i++) {
  272. var btn = opts.toolbar[i];
  273. if (btn == "-") {
  274. $("<div class=\"datagrid-btn-separator\"></div>").appendTo(tb);
  275. } else {
  276. var lnkObj = $("<a href=\"javascript:void(0)\"></a>");
  277. lnkObj[0].onclick = eval(btn.handler || function () {
  278. });
  279. lnkObj.css("float", "left").appendTo(tb).linkbutton($.extend({}, btn, { plain: true }));
  280. }
  281. }
  282. }
  283. } else {
  284. $("div.datagrid-toolbar", pnl).remove();
  285. }
  286. $("div.datagrid-pager", pnl).remove();
  287. if (opts.pagination) {
  288. var pagerObj = $("<div class=\"datagrid-pager\"></div>").appendTo(pnl);
  289. pagerObj.pagination({ pageNumber: opts.pageNumber, pageSize: opts.pageSize, pageList: opts.pageList, onSelectPage: function (_4a, _4b) {
  290. opts.pageNumber = _4a;
  291. opts.pageSize = _4b;
  292. request(target);
  293. }
  294. });
  295. opts.pageSize = pagerObj.pagination("options").pageSize;
  296. }


  297. function appendColumns(container, columns, frozen) {
  298. if (!columns) {
  299. return;
  300. }
  301. $(container).show();
  302. $(container).empty();
  303. var t = $("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tbody></tbody></table>").appendTo(container);
  304. for (var i = 0; i < columns.length; i++) {
  305. var tr = $("<tr></tr>").appendTo($("tbody", t));
  306. var findCol = columns[i];
  307. for (var j = 0; j < findCol.length; j++) {
  308. var col = findCol[j];
  309. var strHtml = "";
  310. if (col.rowspan) {
  311. strHtml += "rowspan=\"" + col.rowspan + "\" ";
  312. }
  313. if (col.colspan) {
  314. strHtml += "colspan=\"" + col.colspan + "\" ";
  315. }
  316. var td = $("<td " + strHtml + "></td>").appendTo(tr);
  317. if (col.checkbox) {
  318. td.attr("field", col.field);
  319. $("<div class=\"datagrid-header-check\"></div>").html("<input type=\"checkbox\"/>").appendTo(td);
  320. } else {
  321. if (col.field) {
  322. td.attr("field", col.field);
  323. td.append("<div class=\"datagrid-cell\"><span></span><span class=\"datagrid-sort-icon\"></span></div>");
  324. $("span", td).html(col.title);
  325. $("span.datagrid-sort-icon", td).html(" ");
  326. var findCell = td.find("div.datagrid-cell");
  327. if (col.resizable == false) {
  328. findCell.attr("resizable", "false");
  329. }
  330. col.boxWidth = $.boxModel ? (col.width - (findCell.outerWidth() - findCell.width())) : col.width;
  331. findCell.width(col.boxWidth);
  332. findCell.css("text-align", (col.align || "left"));
  333. } else {
  334. $("<div class=\"datagrid-cell-group\"></div>").html(col.title).appendTo(td);
  335. }
  336. }
  337. if (col.hidden) {
  338. td.hide();
  339. }
  340. }
  341. }
  342. if (frozen && opts.rownumbers) {
  343. var td = $("<td rowspan=\"" + opts.frozenColumns.length + "\"><div class=\"datagrid-header-rownumber\"></div></td>");
  344. if ($("tr", t).length == 0) {
  345. td.wrap("<tr></tr>").parent().appendTo($("tbody", t));
  346. } else {
  347. td.prependTo($("tr:first", t));
  348. }
  349. }
  350. };
  351. };


  352. /*****
  353. *
  354. *
  355. ****/
  356. function bindRowEvents(target) {
  357. var pnl = $.data(target, "datagrid").panel;
  358. var opts = $.data(target, "datagrid").options;
  359. var data = $.data(target, "datagrid").data;
  360. var pnlBody = pnl.find("div.datagrid-body");
  361. pnlBody.find("tr[datagrid-row-index]").unbind(".datagrid").bind("mouseenter.datagrid", function () {
  362. var findIndex = $(this).attr("datagrid-row-index");
  363. pnlBody.find("tr[datagrid-row-index=" + findIndex + "]").addClass("datagrid-row-over");
  364. }).bind("mouseleave.datagrid", function () {
  365. var findIndex = $(this).attr("datagrid-row-index");
  366. pnlBody.find("tr[datagrid-row-index=" + findIndex + "]").removeClass("datagrid-row-over");
  367. }).bind("click.datagrid", function () {
  368. var findIndex = $(this).attr("datagrid-row-index");
  369. if (opts.singleSelect == true) {
  370. clearSelections(target);
  371. selectRow(target, findIndex);
  372. } else {
  373. if ($(this).hasClass("datagrid-row-selected")) {
  374. unSelectRow(target, findIndex);
  375. } else {
  376. selectRow(target, findIndex);
  377. }
  378. }
  379. if (opts.onClickRow) {
  380. opts.onClickRow.call(target, findIndex, data.rows[findIndex]);
  381. }
  382. }).bind("dblclick.datagrid", function () {
  383. var findIndex = $(this).attr("datagrid-row-index");
  384. if (opts.onDblClickRow) {
  385. opts.onDblClickRow.call(target, findIndex, data.rows[findIndex]);
  386. }
  387. }).bind("contextmenu.datagrid", function (e) {
  388. var findIndex = $(this).attr("datagrid-row-index");
  389. if (opts.onRowContextMenu) {
  390. opts.onRowContextMenu.call(target, e, findIndex, data.rows[findIndex]);
  391. }
  392. });
  393. pnlBody.find("td[field]").unbind(".datagrid").bind("click.datagrid", function () {
  394. var findIndex = $(this).parent().attr("datagrid-row-index");
  395. var findField = $(this).attr("field");
  396. var findRow = data.rows[findIndex][findField];
  397. opts.onClickCell.call(target, findIndex, findField, findRow);
  398. }).bind("dblclick.datagrid", function () {
  399. var findIndex = $(this).parent().attr("datagrid-row-index");
  400. var findField = $(this).attr("field");
  401. var findRow = data.rows[findIndex][findField];
  402. opts.onDblClickCell.call(target, findIndex, findField, findRow);
  403. });
  404. pnlBody.find("div.datagrid-cell-check input[type=checkbox]").unbind(".datagrid").bind("click.datagrid", function (e) {
  405. var findIndex = $(this).parent().parent().parent().attr("datagrid-row-index");
  406. if (opts.singleSelect) {
  407. clearSelections(target);
  408. selectRow(target, findIndex);
  409. } else {
  410. if ($(this).attr("checked")) {
  411. selectRow(target, findIndex);
  412. } else {
  413. unSelectRow(target, findIndex);
  414. }
  415. }
  416. e.stopPropagation();
  417. });
  418. };


  419. function bindCellsEvents(target) {
  420. var pnl = $.data(target, "datagrid").panel;
  421. var opts = $.data(target, "datagrid").options;
  422. var pnlHeader = pnl.find("div.datagrid-header");
  423. pnlHeader.find("td:has(div.datagrid-cell)").unbind(".datagrid").bind("mouseenter.datagrid", function () {
  424. $(this).addClass("datagrid-header-over");
  425. }).bind("mouseleave.datagrid", function () {
  426. $(this).removeClass("datagrid-header-over");
  427. }).bind("contextmenu.datagrid", function (e) {
  428. var filed = $(this).attr("field");
  429. opts.onHeaderContextMenu.call(target, e, filed);
  430. });
  431. pnlHeader.find("div.datagrid-cell").unbind(".datagrid").bind("click.datagrid", function () {
  432. var filed = $(this).parent().attr("field");
  433. var opt = getColOptions(target, filed);
  434. if (!opt.sortable) {
  435. return;
  436. }
  437. opts.sortName = filed;
  438. opts.sortOrder = "asc";
  439. var c = "datagrid-sort-asc";
  440. if ($(this).hasClass("datagrid-sort-asc")) {
  441. c = "datagrid-sort-desc";
  442. opts.sortOrder = "desc";
  443. }
  444. pnlHeader.find("div.datagrid-cell").removeClass("datagrid-sort-asc datagrid-sort-desc");
  445. $(this).addClass(c);
  446. if (opts.onSortColumn) {
  447. opts.onSortColumn.call(target, opts.sortName, opts.sortOrder);
  448. }
  449. if (opts.remoteSort) {
  450. request(target);
  451. } else {
  452. var data = $.data(target, "datagrid").data;
  453. loadData(target, data);
  454. }
  455. });
  456. pnlHeader.find("input[type=checkbox]").unbind(".datagrid").bind("click.datagrid", function () {
  457. if (opts.singleSelect) {
  458. return false;
  459. }
  460. if ($(this).attr("checked")) {
  461. selectAll(target);
  462. } else {
  463. clearSelectRows(target);
  464. }
  465. });
  466. var view = pnl.children("div.datagrid-view");
  467. var view1 = view.children("div.datagrid-view1");
  468. var view2 = view.children("div.datagrid-view2");
  469. view2.children("div.datagrid-body").unbind(".datagrid").bind("scroll.datagrid", function () {
  470. view1.children("div.datagrid-body").scrollTop($(this).scrollTop());
  471. view2.children("div.datagrid-header").scrollLeft($(this).scrollLeft());
  472. view2.children("div.datagrid-footer").scrollLeft($(this).scrollLeft());
  473. });
  474. pnlHeader.find("div.datagrid-cell").each(function () {
  475. $(this).resizable({ handles: "e", disabled: ($(this).attr("resizable") ? $(this).attr("resizable") == "false" : false), minWidth: 25, onStartResize: function (e) {
  476. view.children("div.datagrid-resize-proxy").css({ left: e.pageX - $(pnl).offset().left - 1, display: "block" });
  477. }, onResize: function (e) {
  478. view.children("div.datagrid-resize-proxy").css({ display: "block", left: e.pageX - $(pnl).offset().left - 1 });
  479. return false;
  480. }, onStopResize: function (e) {
  481. var field = $(this).parent().attr("field");
  482. var col = getColOptions(target, field);
  483. col.width = $(this).outerWidth();
  484. col.boxWidth = $.boxModel == true ? $(this).width() : $(this).outerWidth();
  485. fixColumnSize(target, field);
  486. fixColumnWidth(target);
  487. var tmpView = pnl.find("div.datagrid-view2");
  488. tmpView.find("div.datagrid-header").scrollLeft(tmpView.find("div.datagrid-body").scrollLeft());
  489. view.children("div.datagrid-resize-proxy").css("display", "none");
  490. opts.onResizeColumn.call(target, field, col.width);
  491. }
  492. });
  493. });
  494. view1.children("div.datagrid-header").find("div.datagrid-cell").resizable({ onStopResize: function (e) {
  495. var filed = $(this).parent().attr("field");
  496. var col = getColOptions(target, filed);
  497. col.width = $(this).outerWidth();
  498. col.boxWidth = $.boxModel == true ? $(this).width() : $(this).outerWidth();
  499. fixColumnSize(target, filed);
  500. var tmpView = pnl.find("div.datagrid-view2");
  501. tmpView.find("div.datagrid-header").scrollLeft(tmpView.find("div.datagrid-body").scrollLeft());
  502. view.children("div.datagrid-resize-proxy").css("display", "none");
  503. fixDataGridHeight(target);
  504. fixColumnWidth(target);
  505. opts.onResizeColumn.call(target, filed, col.width);
  506. }
  507. });
  508. };


  509. function fixColumnWidth(target) {
  510. var opts = $.data(target, "datagrid").options;
  511. if (!opts.fitColumns) {
  512. return;
  513. }
  514. var pnl = $.data(target, "datagrid").panel;
  515. var view2Header = pnl.find("div.datagrid-view2 div.datagrid-header");
  516. var tmpWidth = 0;
  517. var tmpCol;
  518. var coFields = getColumnFiles(target, false);
  519. for (var i = 0; i < coFields.length; i++) {
  520. var col = getColOptions(target, coFields[i]);
  521. if (!col.hidden && !col.checkbox) {
  522. tmpWidth += col.width;
  523. tmpCol = col;
  524. }
  525. }
  526. var view2HeaderInner = view2Header.children("div.datagrid-header-inner").show();
  527. var jzWidth = view2Header.width() - view2Header.find("table").width() - opts.scrollbarSize;
  528. var ratio = jzWidth / tmpWidth;
  529. if (!opts.showHeader) {
  530. view2HeaderInner.hide();
  531. }
  532. for (var i = 0; i < coFields.length; i++) {
  533. var col = getColOptions(target, coFields[i]);
  534. if (!col.hidden && !col.checkbox) {
  535. var floorWidth = Math.floor(col.width * ratio);
  536. setCellWidth(col, floorWidth);
  537. jzWidth -= floorWidth;
  538. }
  539. }
  540. fixColumnSize(target);
  541. if (jzWidth) {
  542. setCellWidth(tmpCol, jzWidth);
  543. fixColumnSize(target, tmpCol.field);
  544. }
  545. function setCellWidth(col, width) {
  546. col.width += width;
  547. col.boxWidth += width;
  548. view2Header.find("td[field=" + col.field + "] div.datagrid-cell").width(col.boxWidth);
  549. };
  550. };


  551. function fixColumnSize(target, filed) {
  552. var pnl = $.data(target, "datagrid").panel;
  553. var bf = pnl.find("div.datagrid-body,div.datagrid-footer");
  554. if (filed) {
  555. fix(filed);
  556. } else {
  557. pnl.find("div.datagrid-header td[field]").each(function () {
  558. fix($(this).attr("field"));
  559. });
  560. }
  561. fixCellWidth(target);
  562. setTimeout(function () {
  563. fixRowHeight(target);
  564. resizeEditor(target);
  565. }, 0);
  566. function fix(_88) {
  567. var col = getColOptions(target, _88);
  568. bf.find("td[field=" + _88 + "]").each(function () {
  569. var td = $(this);
  570. var _89 = td.attr("colspan") || 1;
  571. if (_89 == 1) {
  572. td.find("div.datagrid-cell").width(col.boxWidth);
  573. td.find("div.datagrid-editable").width(col.width);
  574. }
  575. });
  576. };
  577. };


  578. function fixCellWidth(target) {
  579. var pnl = $.data(target, "datagrid").panel;
  580. var pnlHeader = pnl.find("div.datagrid-header");
  581. pnl.find("div.datagrid-body td.datagrid-td-merged").each(function () {
  582. var td = $(this);
  583. var colspan = td.attr("colspan") || 1;
  584. var tdFiled = td.attr("field");
  585. var findTd = pnlHeader.find("td[field=" + tdFiled + "]");
  586. var findTdWidth = findTd.width();
  587. for (var i = 1; i < colspan; i++) {
  588. findTd = findTd.next();
  589. findTdWidth += findTd.outerWidth();
  590. }
  591. var tdCell = td.children("div.datagrid-cell");
  592. if ($.boxModel == true) {
  593. tdCell.width(findTdWidth - (tdCell.outerWidth() - tdCell.width()));
  594. } else {
  595. tdCell.width(findTdWidth);
  596. }
  597. });
  598. };
  599. function resizeEditor(target) {
  600. var pnl = $.data(target, "datagrid").panel;
  601. pnl.find("div.datagrid-editable").each(function () {
  602. var ed = $.data(this, "datagrid.editor");
  603. if (ed.actions.resize) {
  604. ed.actions.resize(ed.target, $(this).width());
  605. }
  606. });
  607. };
  608. function getColOptions(target, filed) {
  609. var opts = $.data(target, "datagrid").options;
  610. if (opts.columns) {
  611. for (var i = 0; i < opts.columns.length; i++) {
  612. var colOpts = opts.columns[i];
  613. for (var j = 0; j < colOpts.length; j++) {
  614. var col = colOpts[j];
  615. if (col.field == filed) {
  616. return col;
  617. }
  618. }
  619. }
  620. }
  621. if (opts.frozenColumns) {
  622. for (var i = 0; i < opts.frozenColumns.length; i++) {
  623. var colOpts = opts.frozenColumns[i];
  624. for (var j = 0; j < colOpts.length; j++) {
  625. var col = colOpts[j];
  626. if (col.field == filed) {
  627. return col;
  628. }
  629. }
  630. }
  631. }
  632. return null;
  633. };
  634. function getColumnFiles(target, frozen) {
  635. var opts = $.data(target, "datagrid").options;
  636. var cols = (frozen == true) ? (opts.frozenColumns || [[]]) : opts.columns;
  637. if (cols.length == 0) {
  638. return [];
  639. }
  640. var columnFileds = [];
  641. function findLocation(index) {
  642. var c = 0;
  643. var i = 0;
  644. while (true) {
  645. if (columnFileds[i] == undefined) {
  646. if (c == index) {
  647. return i;
  648. }
  649. c++;
  650. }
  651. i++;
  652. }
  653. };
  654. function getColumnFileds(r) {
  655. var ff = [];
  656. var c = 0;
  657. for (var i = 0; i < cols[r].length; i++) {
  658. var col = cols[r][i];
  659. if (col.field) {
  660. ff.push([c, col.field]);
  661. }
  662. c += parseInt(col.colspan || "1");
  663. }
  664. for (var i = 0; i < ff.length; i++) {
  665. ff[i][0] = findLocation(ff[i][0]);
  666. }
  667. for (var i = 0; i < ff.length; i++) {
  668. var f = ff[i];
  669. columnFileds[f[0]] = f[1];
  670. }
  671. };
  672. for (var i = 0; i < cols.length; i++) {
  673. getColumnFileds(i);
  674. }
  675. return columnFileds;
  676. };
  677. function loadData(target, data) {
  678. var opts = $.data(target, "datagrid").options;
  679. var pnl = $.data(target, "datagrid").panel;
  680. var selectRows = $.data(target, "datagrid").selectedRows;
  681. data = opts.loadFilter.call(target, data);
  682. var dataRows = data.rows;
  683. $.data(target, "datagrid").data = data;
  684. if (data.footer) {
  685. $.data(target, "datagrid").footer = data.footer;
  686. }
  687. if (!opts.remoteSort) {
  688. var opt = getColOptions(target, opts.sortName);
  689. if (opt) {
  690. var _a9 = opt.sorter || function (a, b) {
  691. return (a > b ? 1 : -1);
  692. };
  693. data.rows.sort(function (r1, r2) {
  694. return _a9(r1[opts.sortName], r2[opts.sortName]) * (opts.sortOrder == "asc" ? 1 : -1);
  695. });
  696. }
  697. }
  698. var view = pnl.children("div.datagrid-view");
  699. var view1 = view.children("div.datagrid-view1");
  700. var view2 = view.children("div.datagrid-view2");
  701. if (opts.view.onBeforeRender) {
  702. opts.view.onBeforeRender.call(opts.view, target, dataRows);
  703. }
  704. opts.view.render.call(opts.view, target, view2.children("div.datagrid-body"), false);
  705. opts.view.render.call(opts.view, target, view1.children("div.datagrid-body").children("div.datagrid-body-inner"), true);
  706. if (opts.showFooter) {
  707. opts.view.renderFooter.call(opts.view, target, view2.find("div.datagrid-footer-inner"), false);
  708. opts.view.renderFooter.call(opts.view, target, view1.find("div.datagrid-footer-inner"), true);
  709. }
  710. if (opts.view.onAfterRender) {
  711. opts.view.onAfterRender.call(opts.view, target);
  712. }
  713. opts.onLoadSuccess.call(target, data);
  714. var pager = pnl.children("div.datagrid-pager");
  715. if (pager.length) {
  716. if (pager.pagination("options").total != data.total) {
  717. pager.pagination({ total: data.total });
  718. }
  719. }
  720. fixRowHeight(target);
  721. bindRowEvents(target);
  722. view2.children("div.datagrid-body").triggerHandler("scroll");
  723. if (opts.idField) {
  724. for (var i = 0; i < dataRows.length; i++) {
  725. if (isExistsRow(dataRows[i])) {
  726. selectRecord(target, dataRows[i][opts.idField]);
  727. }
  728. }
  729. }
  730. function isExistsRow(row) {
  731. for (var i = 0; i < selectRows.length; i++) {
  732. if (selectRows[i][opts.idField] == row[opts.idField]) {
  733. selectRows[i] = row;
  734. return true;
  735. }
  736. }
  737. return false;
  738. };
  739. };
  740. function getRowIndex(target, row) {
  741. var opts = $.data(target, "datagrid").options;
  742. var rows = $.data(target, "datagrid").data.rows;
  743. if (typeof row == "object") {
  744. return rows.indexOf(row);
  745. } else {
  746. for (var i = 0; i < rows.length; i++) {
  747. if (rows[i][opts.idField] == row) {
  748. return i;
  749. }
  750. }
  751. return -1;
  752. }
  753. };
  754. function getSelectRows(target) {
  755. var opts = $.data(target, "datagrid").options;
  756. var pnl = $.data(target, "datagrid").panel;
  757. var data = $.data(target, "datagrid").data;
  758. if (opts.idField) {
  759. return $.data(target, "datagrid").selectedRows;
  760. } else {
  761. var rowAry = [];
  762. $("div.datagrid-view2 div.datagrid-body tr.datagrid-row-selected", pnl).each(function () {
  763. var row_index = parseInt($(this).attr("datagrid-row-index"));
  764. rowAry.push(data.rows[row_index]);
  765. });
  766. return rowAry;
  767. }
  768. };
  769. function clearSelections(target) {
  770. clearSelectRows(target);
  771. var selectRows = $.data(target, "datagrid").selectedRows;
  772. selectRows.splice(0, selectRows.length);
  773. };
  774. function selectAll(target) {
  775. var opts = $.data(target, "datagrid").options;
  776. var pnl = $.data(target, "datagrid").panel;
  777. var data = $.data(target, "datagrid").data;
  778. var selectRows = $.data(target, "datagrid").selectedRows;
  779. var rows = data.rows;
  780. var pnlBody = pnl.find("div.datagrid-body");
  781. $("tr", pnlBody).addClass("datagrid-row-selected");
  782. $("div.datagrid-cell-check input[type=checkbox]", pnlBody).attr("checked", true);
  783. for (var i = 0; i < rows.length; i++) {
  784. if (opts.idField) {
  785. (function () {
  786. var row = rows[i];
  787. for (var i = 0; i < selectRows.length; i++) {
  788. if (selectRows[i][opts.idField] == row[opts.idField]) {
  789. return;
  790. }
  791. }
  792. selectRows.push(row);
  793. })();
  794. }
  795. }
  796. opts.onSelectAll.call(target, rows);
  797. };
  798. function clearSelectRows(target) {
  799. var opts = $.data(target, "datagrid").options;
  800. var pnl = $.data(target, "datagrid").panel;
  801. var data = $.data(target, "datagrid").data;
  802. var selectRows = $.data(target, "datagrid").selectedRows;
  803. $("div.datagrid-body tr.datagrid-row-selected", pnl).removeClass("datagrid-row-selected");
  804. $("div.datagrid-body div.datagrid-cell-check input[type=checkbox]", pnl).attr("checked", false);
  805. if (opts.idField) {
  806. for (var i = 0; i < data.rows.length; i++) {
  807. selectRows.removeById(opts.idField, data.rows[i][opts.idField]);
  808. }
  809. }
  810. opts.onUnselectAll.call(target, data.rows);
  811. };
  812. function selectRow(target, index) {
  813. var pnl = $.data(target, "datagrid").panel;
  814. var opts = $.data(target, "datagrid").options;
  815. var data = $.data(target, "datagrid").data;
  816. var selectRows = $.data(target, "datagrid").selectedRows;
  817. if (index < 0 || index >= data.rows.length) {
  818. return;
  819. }
  820. if (opts.singleSelect == true) {
  821. clearSelections(target);
  822. }
  823. var tr = $("div.datagrid-body tr[datagrid-row-index=" + index + "]", pnl);
  824. if (!tr.hasClass("datagrid-row-selected")) {
  825. tr.addClass("datagrid-row-selected");
  826. var ck = $("div.datagrid-cell-check input[type=checkbox]", tr);
  827. ck.attr("checked", true);
  828. if (opts.idField) {
  829. var row = data.rows[index];
  830. (function () {
  831. for (var i = 0; i < selectRows.length; i++) {
  832. if (selectRows[i][opts.idField] == row[opts.idField]) {
  833. return;
  834. }
  835. }
  836. selectRows.push(row);
  837. })();
  838. }
  839. }
  840. opts.onSelect.call(target, index, data.rows[index]);
  841. var view2 = pnl.find("div.datagrid-view2");
  842. var view2Header = view2.find("div.datagrid-header").outerHeight();
  843. var view2Body = view2.find("div.datagrid-body");
  844. var top = tr.position().top - view2Header;
  845. if (top <= 0) {
  846. view2Body.scrollTop(view2Body.scrollTop() + top);
  847. } else {
  848. if (top + tr.outerHeight() > view2Body.height() - 18) {
  849. view2Body.scrollTop(view2Body.scrollTop() + top + tr.outerHeight() - view2Body.height() + 18);
  850. }
  851. }
  852. };
  853. function selectRecord(target, id) {
  854. var opts = $.data(target, "datagrid").options;
  855. var data = $.data(target, "datagrid").data;
  856. if (opts.idField) {
  857. var _da = -1;
  858. for (var i = 0; i < data.rows.length; i++) {
  859. if (data.rows[i][opts.idField] == id) {
  860. _da = i;
  861. break;
  862. }
  863. }
  864. if (_da >= 0) {
  865. selectRow(target, _da);
  866. }
  867. }
  868. };
  869. function unSelectRow(target, index) {
  870. var opts = $.data(target, "datagrid").options;
  871. var pnl = $.data(target, "datagrid").panel;
  872. var data = $.data(target, "datagrid").data;
  873. var selectRows = $.data(target, "datagrid").selectedRows;
  874. if (index < 0 || index >= data.rows.length) {
  875. return;
  876. }
  877. var pnlBody = pnl.find("div.datagrid-body");
  878. var tr = $("tr[datagrid-row-index=" + index + "]", pnlBody);
  879. var ck = $("tr[datagrid-row-index=" + index + "] div.datagrid-cell-check input[type=checkbox]", pnlBody);
  880. tr.removeClass("datagrid-row-selected");
  881. ck.attr("checked", false);
  882. var row = data.rows[index];
  883. if (opts.idField) {
  884. selectRows.removeById(opts.idField, row[opts.idField]);
  885. }
  886. opts.onUnselect.call(target, index, row);
  887. };
  888. function beginEdit(target, index) {
  889. var opts = $.data(target, "datagrid").options;
  890. var tr = opts.editConfig.getTr(target, index);
  891. var row = opts.editConfig.getRow(target, index);
  892. if (tr.hasClass("datagrid-row-editing")) {
  893. return;
  894. }
  895. if (opts.onBeforeEdit.call(target, index, row) == false) {
  896. return;
  897. }
  898. tr.addClass("datagrid-row-editing");
  899. beginEditRow(target, index);
  900. resizeEditor(target);
  901. tr.find("div.datagrid-editable").each(function () {
  902. var filed = $(this).parent().attr("field");
  903. var ed = $.data(this, "datagrid.editor");
  904. ed.actions.setValue(ed.target, row[filed]);
  905. });
  906. validateRow(target, index);
  907. };
  908. function endEdit(target, index, canceld) {
  909. var opts = $.data(target, "datagrid").options;
  910. var updatedRows = $.data(target, "datagrid").updatedRows;
  911. var insertedRows = $.data(target, "datagrid").insertedRows;
  912. var tr = opts.editConfig.getTr(target, index);
  913. var row = opts.editConfig.getRow(target, index);
  914. if (!tr.hasClass("datagrid-row-editing")) {
  915. return;
  916. }
  917. if (!canceld) {
  918. if (!validateRow(target, index)) {
  919. return;
  920. }
  921. var flag = false;
  922. var tmpObj = {};
  923. tr.find("div.datagrid-editable").each(function () {
  924. var field = $(this).parent().attr("field");
  925. var ed = $.data(this, "datagrid.editor");
  926. var editValue = ed.actions.getValue(ed.target);
  927. if (row[field] != editValue) {
  928. row[field] = editValue;
  929. flag = true;
  930. tmpObj[field] = editValue;
  931. }
  932. });
  933. if (flag) {
  934. if (insertedRows.indexOf(row) == -1) {
  935. if (updatedRows.indexOf(row) == -1) {
  936. updatedRows.push(row);
  937. }
  938. }
  939. }
  940. }
  941. tr.removeClass("datagrid-row-editing");
  942. destroyEdit(target, index);
  943. $(target).datagrid("refreshRow", index);
  944. if (!canceld) {
  945. opts.onAfterEdit.call(target, index, row, tmpObj);
  946. } else {
  947. opts.onCancelEdit.call(target, index, row);
  948. }
  949. };
  950. function getEditors(target, index) {
  951. var editors = [];
  952. var pnl = $.data(target, "datagrid").panel;
  953. var tr = $("div.datagrid-body tr[datagrid-row-index=" + index + "]", pnl);
  954. tr.children("td").each(function () {
  955. var editorsAry = $(this).find("div.datagrid-editable");
  956. if (editorsAry.length) {
  957. var ed = $.data(editorsAry[0], "datagrid.editor");
  958. editors.push(ed);
  959. }
  960. });
  961. return editors;
  962. };
  963. function getEditor(target, options) {
  964. var editors = getEditors(target, options.index);
  965. for (var i = 0; i < editors.length; i++) {
  966. if (editors[i].field == options.field) {
  967. return editors[i];
  968. }
  969. }
  970. return null;
  971. };
  972. function beginEditRow(target, index) {
  973. var opts = $.data(target, "datagrid").options;
  974. var tr = opts.editConfig.getTr(target, index);
  975. tr.children("td").each(function () {
  976. var cell = $(this).find("div.datagrid-cell");
  977. var thisFiled = $(this).attr("field");
  978. var col = getColOptions(target, thisFiled);
  979. if (col && col.editor) {
  980. var tmpType, tmpOpts;
  981. if (typeof col.editor == "string") {
  982. tmpType = col.editor;
  983. } else {
  984. tmpType = col.editor.type;
  985. tmpOpts = col.editor.options;
  986. }
  987. var tmpEditor = opts.editors[tmpType];
  988. if (tmpEditor) {
  989. var cellHtml = cell.html();
  990. var cellOutWidth = cell.outerWidth();
  991. cell.addClass("datagrid-editable");
  992. if ($.boxModel == true) {
  993. cell.width(cellOutWidth - (cell.outerWidth() - cell.width()));
  994. }
  995. cell.html("<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\"><tr><td></td></tr></table>");
  996. cell.children("table").attr("align", col.align);
  997. cell.children("table").bind("click dblclick contextmenu", function (e) {
  998. e.stopPropagation();
  999. });
  1000. $.data(cell[0], "datagrid.editor", { actions: tmpEditor, target: tmpEditor.init(cell.find("td"), tmpOpts), field: thisFiled, type: tmpType, oldHtml: cellHtml });
  1001. }
  1002. }
  1003. });
  1004. fixRowHeight(target, index);
  1005. };
  1006. function destroyEdit(target, index) {
  1007. var opts = $.data(target, "datagrid").options;
  1008. var tr = opts.editConfig.getTr(target, index);
  1009. tr.children("td").each(function () {
  1010. var cell = $(this).find("div.datagrid-editable");
  1011. if (cell.length) {
  1012. var ed = $.data(cell[0], "datagrid.editor");
  1013. if (ed.actions.destroy) {
  1014. ed.actions.destroy(ed.target);
  1015. }
  1016. cell.html(ed.oldHtml);
  1017. $.removeData(cell[0], "datagrid.editor");
  1018. var cellOuterWidth = cell.outerWidth();
  1019. cell.removeClass("datagrid-editable");
  1020. if ($.boxModel == true) {
  1021. cell.width(cellOuterWidth - (cell.outerWidth() - cell.width()));
  1022. }
  1023. }
  1024. });
  1025. };
  1026. function validateRow(target, index) {
  1027. var tr = $.data(target, "datagrid").options.editConfig.getTr(target, index);
  1028. if (!tr.hasClass("datagrid-row-editing")) {
  1029. return true;
  1030. }
  1031. var vbox = tr.find(".validatebox-text");
  1032. vbox.validatebox("validate");
  1033. vbox.trigger("mouseleave");
  1034. var trAry = tr.find(".validatebox-invalid");
  1035. return trAry.length == 0;
  1036. };
  1037. function getChanges(target, type) {
  1038. var insertedRows = $.data(target, "datagrid").insertedRows;
  1039. var deletedRows = $.data(target, "datagrid").deletedRows;
  1040. var updatedRows = $.data(target, "datagrid").updatedRows;
  1041. if (!type) {
  1042. var rows = [];
  1043. rows = rows.concat(insertedRows);
  1044. rows = rows.concat(deletedRows);
  1045. rows = rows.concat(updatedRows);
  1046. return rows;
  1047. } else {
  1048. if (type == "inserted") {
  1049. return insertedRows;
  1050. } else {
  1051. if (type == "deleted") {
  1052. return deletedRows;
  1053. } else {
  1054. if (type == "updated") {
  1055. return updatedRows;
  1056. }
  1057. }
  1058. }
  1059. }
  1060. return [];
  1061. };
  1062. function deleteRow(taget, index) {
  1063. var opts = $.data(taget, "datagrid").options;
  1064. var data = $.data(taget, "datagrid").data;
  1065. var insertedRows = $.data(taget, "datagrid").insertedRows;
  1066. var deletedRows = $.data(taget, "datagrid").deletedRows;
  1067. var selectedRows = $.data(taget, "datagrid").selectedRows;
  1068. $(taget).datagrid("cancelEdit", index);
  1069. var row = data.rows[index];
  1070. if (insertedRows.indexOf(row) >= 0) {
  1071. insertedRows.remove(row);
  1072. } else {
  1073. deletedRows.push(row);
  1074. }
  1075. selectedRows.removeById(opts.idField, data.rows[index][opts.idField]);
  1076. opts.view.deleteRow.call(opts.view, taget, index);
  1077. if (opts.height == "auto") {
  1078. fixRowHeight(taget);
  1079. }
  1080. };
  1081. function insertRow(target, param) {
  1082. var view = $.data(target, "datagrid").options.view;
  1083. var insertedRows = $.data(target, "datagrid").insertedRows;
  1084. view.insertRow.call(view, target, param.index, param.row);
  1085. bindRowEvents(target);
  1086. insertedRows.push(param.row);
  1087. };
  1088. function appendRow(target, row) {
  1089. var view = $.data(target, "datagrid").options.view;
  1090. var insertedRows = $.data(target, "datagrid").insertedRows;
  1091. view.insertRow.call(view, target, null, row);
  1092. bindRowEvents(target);
  1093. insertedRows.push(row);
  1094. };
  1095. function cacheRows(target) {
  1096. var data = $.data(target, "datagrid").data;
  1097. var rows = data.rows;
  1098. var rowAry = [];
  1099. for (var i = 0; i < rows.length; i++) {
  1100. rowAry.push($.extend({}, rows[i]));
  1101. }
  1102. $.data(target, "datagrid").originalRows = rowAry;
  1103. $.data(target, "datagrid").updatedRows = [];
  1104. $.data(target, "datagrid").insertedRows = [];
  1105. $.data(target, "datagrid").deletedRows = [];
  1106. };
  1107. function acceptChanges(target) {
  1108. var data = $.data(target, "datagrid").data;
  1109. var ok = true;
  1110. for (var i = 0, len = data.rows.length; i < len; i++) {
  1111. if (validateRow(target, i)) {
  1112. endEdit(target, i, false);
  1113. } else {
  1114. ok = false;
  1115. }
  1116. }
  1117. if (ok) {
  1118. cacheRows(target);
  1119. }
  1120. };
  1121. function rejectChanges(target) {
  1122. var opts = $.data(target, "datagrid").options;
  1123. var originalRows = $.data(target, "datagrid").originalRows;
  1124. var insertedRows = $.data(target, "datagrid").insertedRows;
  1125. var deletedRows = $.data(target, "datagrid").deletedRows;
  1126. var selectedRows = $.data(target, "datagrid").selectedRows;
  1127. var data = $.data(target, "datagrid").data;
  1128. for (var i = 0; i < data.rows.length; i++) {
  1129. endEdit(target, i, true);
  1130. }
  1131. var rowAry = [];
  1132. for (var i = 0; i < selectedRows.length; i++) {
  1133. rowAry.push(selectedRows[i][opts.idField]);
  1134. }
  1135. selectedRows.splice(0, selectedRows.length);
  1136. data.total += deletedRows.length - insertedRows.length;
  1137. data.rows = originalRows;
  1138. loadData(target, data);
  1139. for (var i = 0; i < rowAry.length; i++) {
  1140. selectRecord(target, rowAry[i]);
  1141. }
  1142. cacheRows(target);
  1143. };
  1144. function request(target, param) {
  1145. var pnl = $.data(target, "datagrid").panel;
  1146. var opts = $.data(target, "datagrid").options;
  1147. if (param) {
  1148. opts.queryParams = param;
  1149. }
  1150. if (!opts.url) {
  1151. return;
  1152. }
  1153. var queryObj = $.extend({}, opts.queryParams);
  1154. if (opts.pagination) {
  1155. $.extend(queryObj, { page: opts.pageNumber, rows: opts.pageSize });
  1156. }
  1157. if (opts.sortName) {
  1158. $.extend(queryObj, { sort: opts.sortName, order: opts.sortOrder });
  1159. }
  1160. if (opts.onBeforeLoad.call(target, queryObj) == false) {
  1161. return;
  1162. }
  1163. $(target).datagrid("loading");
  1164. setTimeout(function () {
  1165. ajaxRequest();
  1166. }, 0);
  1167. function ajaxRequest() {
  1168. $.ajax({ type: opts.method, url: opts.url, data: queryObj, dataType: "json", success: function (data) {
  1169. setTimeout(function () {
  1170. $(target).datagrid("loaded");
  1171. }, 0);
  1172. parent.closeLoadinglayer();
  1173. loadData(target, data);
  1174. setTimeout(function () {
  1175. cacheRows(target);
  1176. }, 0);
  1177. }, error: function () {
  1178. setTimeout(function () {
  1179. $(target).datagrid("loaded");
  1180. }, 0);

  1181. if (opts.onLoadError) {
  1182. opts.onLoadError.apply(target, arguments);
  1183. }
  1184. }
  1185. });
  1186. };
  1187. };
  1188. function mergeCells(target, options) {
  1189. var rows = $.data(target, "datagrid").data.rows;
  1190. var pnl = $.data(target, "datagrid").panel;
  1191. options.rowspan = options.rowspan || 1;
  1192. options.colspan = options.colspan || 1;
  1193. if (options.index < 0 || options.index >= rows.length) {
  1194. return;
  1195. }
  1196. if (options.rowspan == 1 && options.colspan == 1) {
  1197. return;
  1198. }
  1199. var findRow = rows[options.index][options.field];
  1200. var tr = pnl.find("div.datagrid-body tr[datagrid-row-index=" + options.index + "]");
  1201. var td = tr.find("td[field=" + options.field + "]");
  1202. td.attr("rowspan", options.rowspan).attr("colspan", options.colspan);
  1203. td.addClass("datagrid-td-merged");
  1204. for (var i = 1; i < options.colspan; i++) {
  1205. td = td.next();
  1206. td.hide();
  1207. rows[options.index][td.attr("field")] = findRow;
  1208. }
  1209. for (var i = 1; i < options.rowspan; i++) {
  1210. tr = tr.next();
  1211. var td = tr.find("td[field=" + options.field + "]").hide();
  1212. rows[options.index + i][td.attr("field")] = findRow;
  1213. for (var j = 1; j < options.colspan; j++) {
  1214. td = td.next();
  1215. td.hide();
  1216. rows[options.index + i][td.attr("field")] = findRow;
  1217. }
  1218. }
  1219. setTimeout(function () {
  1220. fixCellWidth(target);
  1221. }, 0);
  1222. };
  1223. $.fn.datagrid = function (options, param) {
  1224. if (typeof options == "string") {
  1225. return $.fn.datagrid.methods[options](this, param);
  1226. }
  1227. options = options || {};
  1228. return this.each(function () {
  1229. var state = $.data(this, "datagrid");
  1230. var opts;
  1231. if (state) {
  1232. opts = $.extend(state.options, options);
  1233. state.options = opts;
  1234. } else {
  1235. opts = $.extend({}, $.fn.datagrid.defaults, $.fn.datagrid.parseOptions(this), options);
  1236. $(this).css("width", "").css("height", "");
  1237. var wrapResult = wrapGrid(this, opts.rownumbers);
  1238. if (!opts.columns) {
  1239. opts.columns = wrapResult.columns;
  1240. }
  1241. if (!opts.frozenColumns) {
  1242. opts.frozenColumns = wrapResult.frozenColumns;
  1243. }
  1244. $.data(this, "datagrid", { options: opts, panel: wrapResult.panel, selectedRows: [], data: { total: 0, rows: [] }, originalRows: [], updatedRows: [], insertedRows: [], deletedRows: [] });
  1245. }
  1246. initDatagrid(this);
  1247. if (!state) {
  1248. var data = getData(this);
  1249. if (data.total > 0) {
  1250. loadData(this, data);
  1251. cacheRows(this);
  1252. }
  1253. }
  1254. setSize(this);
  1255. if (opts.url) {
  1256. request(this);
  1257. }
  1258. bindCellsEvents(this);
  1259. });
  1260. };
  1261. var editorsObj = { text: { init: function (container, options) {
  1262. var gridInput = $("<input type=\"text\" class=\"datagrid-editable-input\">").appendTo(container);
  1263. return gridInput;
  1264. }, getValue: function (target) {
  1265. return $(target).val();
  1266. }, setValue: function (target, value) {
  1267. $(target).val(value);
  1268. }, resize: function (target, width) {
  1269. var targetObj = $(target);
  1270. if ($.boxModel == true) {
  1271. targetObj.width(width - (targetObj.outerWidth() - targetObj.width()));
  1272. } else {
  1273. targetObj.width(width);
  1274. }
  1275. }
  1276. }, textarea: { init: function (container, options) {
  1277. var gridTextAreaObj = $("<textarea class=\"datagrid-editable-input\"></textarea>").appendTo(container);
  1278. return gridTextAreaObj;
  1279. }, getValue: function (target) {
  1280. return $(target).val();
  1281. }, setValue: function (target, value) {
  1282. $(target).val(value);
  1283. }, resize: function (target, width) {
  1284. var targetObj = $(target);
  1285. if ($.boxModel == true) {
  1286. targetObj.width(width - (targetObj.outerWidth() - targetObj.width()));
  1287. } else {
  1288. targetObj.width(width);
  1289. }
  1290. }
  1291. }, checkbox: { init: function (container, options) {
  1292. var gridCheckBoxObj = $("<input type=\"checkbox\">").appendTo(container);
  1293. gridCheckBoxObj.val(options.on);
  1294. gridCheckBoxObj.attr("offval", options.off);
  1295. return gridCheckBoxObj;
  1296. }, getValue: function (target) {
  1297. if ($(target).attr("checked")) {
  1298. return $(target).val();
  1299. } else {
  1300. return $(target).attr("offval");
  1301. }
  1302. }, setValue: function (target, value) {
  1303. if ($(target).val() == value) {
  1304. $(target).attr("checked", true);
  1305. } else {
  1306. $(target).attr("checked", false);
  1307. }
  1308. }
  1309. }, numberbox: { init: function (container, options) {
  1310. var gridNumBoxObj = $("<input type=\"text\" class=\"datagrid-editable-input\">").appendTo(container);
  1311. gridNumBoxObj.numberbox(options);
  1312. return gridNumBoxObj;
  1313. }, destroy: function (target) {
  1314. $(target).numberbox("destroy");
  1315. }, getValue: function (target) {
  1316. return $(target).val();
  1317. }, setValue: function (target, value) {
  1318. $(target).val(value);
  1319. }, resize: function (target, width) {
  1320. var targetObj = $(target);
  1321. if ($.boxModel == true) {
  1322. targetObj.width(width - (targetObj.outerWidth() - targetObj.width()));
  1323. } else {
  1324. targetObj.width(width);
  1325. }
  1326. }
  1327. }, validatebox: { init: function (container, options) {
  1328. var gridvalidateBoxObj = $("<input type=\"text\" class=\"datagrid-editable-input\">").appendTo(container);
  1329. gridvalidateBoxObj.validatebox(options);
  1330. return gridvalidateBoxObj;
  1331. }, destroy: function (target) {
  1332. $(target).validatebox("destroy");
  1333. }, getValue: function (target) {
  1334. return $(target).val();
  1335. }, setValue: function (target, value) {
  1336. $(target).val(value);
  1337. }, resize: function (taget, width) {
  1338. var targetObj = $(taget);
  1339. if ($.boxModel == true) {
  1340. targetObj.width(width - (targetObj.outerWidth() - targetObj.width()));
  1341. } else {
  1342. targetObj.width(width);
  1343. }
  1344. }
  1345. }, datebox: { init: function (container, options) {
  1346. var gridDateBoxObj = $("<input type=\"text\">").appendTo(container);
  1347. gridDateBoxObj.datebox(options);
  1348. return gridDateBoxObj;
  1349. }, destroy: function (target) {
  1350. $(target).datebox("destroy");
  1351. }, getValue: function (target) {
  1352. return $(target).datebox("getValue");
  1353. }, setValue: function (target, value) {
  1354. $(target).datebox("setValue", value);
  1355. }, resize: function (target, width) {
  1356. $(target).datebox("resize", width);
  1357. }
  1358. }, combobox: { init: function (container, options) {
  1359. var gridcomboxObj = $("<input type=\"text\">").appendTo(container);
  1360. gridcomboxObj.combobox(options || {});
  1361. return gridcomboxObj;
  1362. }, destroy: function (target) {
  1363. $(target).combobox("destroy");
  1364. }, getValue: function (target) {
  1365. return $(target).combobox("getValue");
  1366. }, setValue: function (target, value) {
  1367. $(target).combobox("setValue", value);
  1368. }, resize: function (target, width) {
  1369. $(target).combobox("resize", width);
  1370. }
  1371. }, combotree: { init: function (container, options) {
  1372. var gridcombotreeObj = $("<input type=\"text\">").appendTo(container);
  1373. gridcombotreeObj.combotree(options);
  1374. return gridcombotreeObj;
  1375. }, destroy: function (target) {
  1376. $(target).combotree("destroy");
  1377. }, getValue: function (target) {
  1378. return $(target).combotree("getValue");
  1379. }, setValue: function (target, value) {
  1380. $(target).combotree("setValue", value);
  1381. }, resize: function (target, width) {
  1382. $(target).combotree("resize", width);
  1383. }
  1384. }
  1385. };
  1386. $.fn.datagrid.methods = { options: function (jq) {
  1387. var opts = $.data(jq[0], "datagrid").options;
  1388. var pnlOpts = $.data(jq[0], "datagrid").panel.panel("options");
  1389. var opts = $.extend(opts, { width: pnlOpts.width, height: pnlOpts.height, closed: pnlOpts.closed, collapsed: pnlOpts.collapsed, minimized: pnlOpts.minimized, maximized: pnlOpts.maximized });
  1390. var pager = jq.datagrid("getPager");
  1391. if (pager.length) {
  1392. var pagerOpts = pager.pagination("options");
  1393. $.extend(opts, { pageNumber: pagerOpts.pageNumber, pageSize: pagerOpts.pageSize });
  1394. }
  1395. return opts;
  1396. }, getPanel: function (jq) {
  1397. return $.data(jq[0], "datagrid").panel;
  1398. }, getPager: function (jq) {
  1399. return $.data(jq[0], "datagrid").panel.find("div.datagrid-pager");
  1400. }, getColumnFields: function (jq, frozen) {
  1401. return getColumnFiles(jq[0], frozen);
  1402. }, getColumnOption: function (jq, filed) {
  1403. return getColOptions(jq[0], filed);
  1404. }, resize: function (jq, params) {
  1405. return jq.each(function () {
  1406. setSize(this, params);
  1407. });
  1408. }, load: function (jq, param) {
  1409. return jq.each(function () {
  1410. var opts = $(this).datagrid("options");
  1411. opts.pageNumber = 1;
  1412. var pager = $(this).datagrid("getPager");
  1413. pager.pagination({ pageNumber: 1 });
  1414. request(this, param);
  1415. });
  1416. }, reload: function (jq, param) {
  1417. return jq.each(function () {
  1418. request(this, param);
  1419. });
  1420. }, reloadFooter: function (jq, footer) {
  1421. return jq.each(function () {
  1422. var opts = $.data(this, "datagrid").options;
  1423. var view = $(this).datagrid("getPanel").children("div.datagrid-view");
  1424. var view1 = view.children("div.datagrid-view1");
  1425. var view2 = view.children("div.datagrid-view2");
  1426. if (footer) {
  1427. $.data(this, "datagrid").footer = footer;
  1428. }
  1429. if (opts.showFooter) {
  1430. opts.view.renderFooter.call(opts.view, this, view2.find("div.datagrid-footer-inner"), false);
  1431. opts.view.renderFooter.call(opts.view, this, view1.find("div.datagrid-footer-inner"), true);
  1432. if (opts.view.onAfterRender) {
  1433. opts.view.onAfterRender.call(opts.view, this);
  1434. }
  1435. $(this).datagrid("fixRowHeight");
  1436. }
  1437. });
  1438. }, loading: function (jq) {
  1439. return jq.each(function () {
  1440. var opts = $.data(this, "datagrid").options;
  1441. $(this).datagrid("getPager").pagination("loading");
  1442. if (opts.loadMsg) {
  1443. var wrap = $(this).datagrid("getPanel");
  1444. $("<div class=\"datagrid-mask\"></div>").css({ display: "block", width: wrap.width(), height: wrap.height() }).appendTo(wrap);
  1445. $("<div class=\"datagrid-mask-msg\"></div>").html(opts.loadMsg).appendTo(wrap).css({ display: "block", left: (wrap.width() - $("div.datagrid-mask-msg", wrap).outerWidth()) / 2, top: (wrap.height() - $("div.datagrid-mask-msg", wrap).outerHeight()) / 2 });
  1446. }
  1447. });
  1448. }, loaded: function (jq) {
  1449. return jq.each(function () {
  1450. $(this).datagrid("getPager").pagination("loaded");
  1451. var pnl = $(this).datagrid("getPanel");
  1452. pnl.children("div.datagrid-mask-msg").remove();
  1453. pnl.children("div.datagrid-mask").remove();
  1454. });
  1455. }, fitColumns: function (jq) {
  1456. return jq.each(function () {
  1457. fixColumnWidth(this);
  1458. });
  1459. }, fixColumnSize: function (jq) {
  1460. return jq.each(function () {
  1461. fixColumnSize(this);
  1462. });
  1463. }, fixRowHeight: function (jq, index) {
  1464. return jq.each(function () {
  1465. fixRowHeight(this, index);
  1466. });
  1467. }, loadData: function (jq, data) {
  1468. return jq.each(function () {
  1469. loadData(this, data);
  1470. cacheRows(this);
  1471. });
  1472. }, getData: function (jq) {
  1473. return $.data(jq[0], "datagrid").data;
  1474. }, getRows: function (jq) {
  1475. return $.data(jq[0], "datagrid").data.rows;
  1476. }, getFooterRows: function (jq) {
  1477. return $.data(jq[0], "datagrid").footer;
  1478. }, getRowIndex: function (jq, id) {
  1479. return getRowIndex(jq[0], id);
  1480. }, getSelected: function (jq) {
  1481. var rows = getSelectRows(jq[0]);
  1482. return rows.length > 0 ? rows[0] : null;
  1483. }, getSelections: function (jq) {
  1484. return getSelectRows(jq[0]);
  1485. }, clearSelections: function (jq) {
  1486. return jq.each(function () {
  1487. clearSelections(this);
  1488. });
  1489. }, selectAll: function (jq) {
  1490. return jq.each(function () {
  1491. selectAll(this);
  1492. });
  1493. }, unselectAll: function (jq) {
  1494. return jq.each(function () {
  1495. clearSelectRows(this);
  1496. });
  1497. }, selectRow: function (jq, index) {
  1498. return jq.each(function () {
  1499. selectRow(this, index);
  1500. });
  1501. }, selectRecord: function (jq, id) {
  1502. return jq.each(function () {
  1503. selectRecord(this, id);
  1504. });
  1505. }, unselectRow: function (jq, index) {
  1506. return jq.each(function () {
  1507. unSelectRow(this, index);
  1508. });
  1509. }, beginEdit: function (jq, index) {
  1510. return jq.each(function () {
  1511. beginEdit(this, index);
  1512. });
  1513. }, endEdit: function (jq, index) {
  1514. return jq.each(function () {
  1515. endEdit(this, index, false);
  1516. });
  1517. }, cancelEdit: function (jq, index) {
  1518. return jq.each(function () {
  1519. endEdit(this, index, true);
  1520. });
  1521. }, getEditors: function (jq, index) {
  1522. return getEditors(jq[0], index);
  1523. }, getEditor: function (jq, options) {
  1524. return getEditor(jq[0], options);
  1525. }, refreshRow: function (jq, index) {
  1526. return jq.each(function () {
  1527. var opts = $.data(this, "datagrid").options;
  1528. opts.view.refreshRow.call(opts.view, this, index);
  1529. });
  1530. }, validateRow: function (jq, index) {
  1531. return validateRow(jq[0], index);
  1532. }, updateRow: function (jq, param) {
  1533. return jq.each(function () {
  1534. var opts = $.data(this, "datagrid").options;
  1535. opts.view.updateRow.call(opts.view, this, param.index, param.row);
  1536. });
  1537. }, appendRow: function (jq, row) {
  1538. return jq.each(function () {
  1539. appendRow(this, row);
  1540. });
  1541. }, insertRow: function (jq, param) {
  1542. return jq.each(function () {
  1543. insertRow(this, param);
  1544. });
  1545. }, deleteRow: function (jq, index) {
  1546. return jq.each(function () {
  1547. deleteRow(this, index);
  1548. });
  1549. }, getChanges: function (jq, type) {
  1550. return getChanges(jq[0], type);
  1551. }, acceptChanges: function (jq) {
  1552. return jq.each(function () {
  1553. acceptChanges(this);
  1554. });
  1555. }, rejectChanges: function (jq) {
  1556. return jq.each(function () {
  1557. rejectChanges(this);
  1558. });
  1559. }, mergeCells: function (jq, options) {
  1560. return jq.each(function () {
  1561. mergeCells(this, options);
  1562. });
  1563. }, showColumn: function (jq, filed) {
  1564. return jq.each(function () {
  1565. var pnl = $(this).datagrid("getPanel");
  1566. pnl.find("td[field=" + filed + "]").show();
  1567. $(this).datagrid("getColumnOption", filed).hidden = false;
  1568. $(this).datagrid("fitColumns");
  1569. });
  1570. }, hideColumn: function (jq, filed) {
  1571. return jq.each(function () {
  1572. var pnl = $(this).datagrid("getPanel");
  1573. pnl.find("td[field=" + filed + "]").hide();
  1574. $(this).datagrid("getColumnOption", filed).hidden = true;
  1575. $(this).datagrid("fitColumns");
  1576. });
  1577. }
  1578. };
  1579. $.fn.datagrid.parseOptions = function (target) {
  1580. var t = $(target);
  1581. return $.extend({}, $.fn.panel.parseOptions(target), { fitColumns: (t.attr("fitColumns") ? t.attr("fitColumns") == "true" : undefined), striped: (t.attr("striped") ? t.attr("striped") == "true" : undefined), nowrap: (t.attr("nowrap") ? t.attr("nowrap") == "true" : undefined), rownumbers: (t.attr("rownumbers") ? t.attr("rownumbers") == "true" : undefined), singleSelect: (t.attr("singleSelect") ? t.attr("singleSelect") == "true" : undefined), pagination: (t.attr("pagination") ? t.attr("pagination") == "true" : undefined), pageSize: (t.attr("pageSize") ? parseInt(t.attr("pageSize")) : undefined), pageList: (t.attr("pageList") ? eval(t.attr("pageList")) : undefined), remoteSort: (t.attr("remoteSort") ? t.attr("remoteSort") == "true" : undefined), showHeader: (t.attr("showHeader") ? t.attr("showHeader") == "true" : undefined), showFooter: (t.attr("showFooter") ? t.attr("showFooter") == "true" : undefined), scrollbarSize: (t.attr("scrollbarSize") ? parseInt(t.attr("scrollbarSize")) : undefined), loadMsg: (t.attr("loadMsg") != undefined ? t.attr("loadMsg") : undefined), idField: t.attr("idField"), toolbar: t.attr("toolbar"), url: t.attr("url") });
  1582. };
  1583. var renderView = { render: function (target, container, frozen) {
  1584. var opts = $.data(target, "datagrid").options;
  1585. var rows = $.data(target, "datagrid").data.rows;
  1586. var closFileds = $(target).datagrid("getColumnFields", frozen);
  1587. if (frozen) {
  1588. if (!(opts.rownumbers || (opts.frozenColumns && opts.frozenColumns.length))) {
  1589. return;
  1590. }
  1591. }
  1592. var htmlAry = ["<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody>"];
  1593. for (var i = 0; i < rows.length; i++) {
  1594. var cls = (i % 2 && opts.striped) ? "class=\"datagrid-row-alt\"" : "";
  1595. var rowStyle = opts.rowStyler ? opts.rowStyler.call(target, i, rows[i]) : "";
  1596. var strStyle = rowStyle ? "style=\"" + rowStyle + "\"" : "";
  1597. htmlAry.push("<tr datagrid-row-index=\"" + i + "\" " + cls + " " + strStyle + ">");
  1598. htmlAry.push(this.renderRow.call(this, target, closFileds, frozen, i, rows[i]));
  1599. htmlAry.push("</tr>");
  1600. }
  1601. htmlAry.push("</tbody></table>");
  1602. $(container).html(htmlAry.join(""));
  1603. }, renderFooter: function (target, container, frozen) {
  1604. var opts = $.data(target, "datagrid").options;
  1605. var rows = $.data(target, "datagrid").footer || [];
  1606. var cols = $(target).datagrid("getColumnFields", frozen);
  1607. var htmlAry = ["<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody>"];
  1608. for (var i = 0; i < rows.length; i++) {
  1609. htmlAry.push("<tr datagrid-row-index=\"" + i + "\">");
  1610. htmlAry.push(this.renderRow.call(this, target, cols, frozen, i, rows[i]));
  1611. htmlAry.push("</tr>");
  1612. }
  1613. htmlAry.push("</tbody></table>");
  1614. $(container).html(htmlAry.join(""));
  1615. }, renderRow: function (target, fileds, frozen, rowIndex, rowData) {
  1616. var opts = $.data(target, "datagrid").options;
  1617. var cc = [];
  1618. if (frozen && opts.rownumbers) {
  1619. var tmpIndex = rowIndex + 1;
  1620. if (opts.pagination) {
  1621. tmpIndex += (opts.pageNumber - 1) * opts.pageSize;
  1622. }
  1623. cc.push("<td class=\"datagrid-td-rownumber\"><div class=\"datagrid-cell-rownumber\">" + tmpIndex + "</div></td>");
  1624. }
  1625. for (var i = 0; i < fileds.length; i++) {
  1626. var findFiled = fileds[i];
  1627. var col = $(target).datagrid("getColumnOption", findFiled);
  1628. if (col) {
  1629. var findStyler = col.styler ? (col.styler(rowData[findFiled], rowData, rowIndex) || "") : "";
  1630. var strFindStyler = col.hidden ? "style=\"display:none;" + findStyler + "\"" : (findStyler ? "style=\"" + findStyler + "\"" : "");
  1631. cc.push("<td field=\"" + findFiled + "\" " + strFindStyler + ">");
  1632. var strFindStyler = "width:" + (col.boxWidth) + "px;";
  1633. strFindStyler += "text-align:" + (col.align || "left") + ";";
  1634. strFindStyler += opts.nowrap == false ? "white-space:normal;" : "";
  1635. cc.push("<div style=\"" + strFindStyler + "\" ");
  1636. if (col.checkbox) {
  1637. cc.push("class=\"datagrid-cell-check ");
  1638. } else {
  1639. cc.push("class=\"datagrid-cell ");
  1640. }
  1641. cc.push("\">");
  1642. if (col.checkbox) {
  1643. cc.push("<input type=\"checkbox\"/>");
  1644. } else {
  1645. if (col.formatter) {
  1646. cc.push(col.formatter(rowData[findFiled], rowData, rowIndex));
  1647. } else {
  1648. cc.push(rowData[findFiled]);
  1649. }
  1650. }
  1651. cc.push("</div>");
  1652. cc.push("</td>");
  1653. }
  1654. }
  1655. return cc.join("");
  1656. }, refreshRow: function (target, rowIndex) {
  1657. var rows = $(target).datagrid("getRows");
  1658. this.updateRow.call(this, target, rowIndex, rows[rowIndex]);
  1659. }, updateRow: function (target, rowIndex, row) {
  1660. var opts = $.data(target, "datagrid").options;
  1661. var pnl = $(target).datagrid("getPanel");
  1662. var rows = $(target).datagrid("getRows");
  1663. var tr = pnl.find("div.datagrid-body tr[datagrid-row-index=" + rowIndex + "]");
  1664. for (var tmpfiled in row) {
  1665. rows[rowIndex][tmpfiled] = row[tmpfiled];
  1666. var td = tr.children("td[field=" + tmpfiled + "]");
  1667. var cell = td.find("div.datagrid-cell");
  1668. var col = $(target).datagrid("getColumnOption", tmpfiled);
  1669. if (col) {
  1670. var findStyler = col.styler ? col.styler(rows[rowIndex][tmpfiled], rows[rowIndex], rowIndex) : "";
  1671. td.attr("style", findStyler || "");
  1672. if (col.hidden) {
  1673. td.hide();
  1674. }
  1675. if (col.formatter) {
  1676. cell.html(col.formatter(rows[rowIndex][tmpfiled], rows[rowIndex], rowIndex));
  1677. } else {
  1678. cell.html(rows[rowIndex][tmpfiled]);
  1679. }
  1680. }
  1681. }
  1682. var findStyler = opts.rowStyler ? opts.rowStyler.call(target, rowIndex, rows[rowIndex]) : "";
  1683. tr.attr("style", findStyler || "");
  1684. $(target).datagrid("fixRowHeight", rowIndex);
  1685. }, insertRow: function (target, rowIndex, row) {
  1686. var opts = $.data(target, "datagrid").options;
  1687. var data = $.data(target, "datagrid").data;
  1688. var view = $(target).datagrid("getPanel").children("div.datagrid-view");
  1689. var view1 = view.children("div.datagrid-view1");
  1690. var view2 = view.children("div.datagrid-view2");
  1691. if (rowIndex == undefined || rowIndex == null) {
  1692. rowIndex = data.rows.length;
  1693. }
  1694. if (rowIndex > data.rows.length) {
  1695. rowIndex = data.rows.length;
  1696. }
  1697. for (var i = data.rows.length - 1; i >= rowIndex; i--) {
  1698. view2.children("div.datagrid-body").find("tr[datagrid-row-index=" + i + "]").attr("datagrid-row-index", i + 1);
  1699. var tr = view1.children("div.datagrid-body").find("tr[datagrid-row-index=" + i + "]").attr("datagrid-row-index", i + 1);
  1700. if (opts.rownumbers) {
  1701. tr.find("div.datagrid-cell-rownumber").html(i + 2);
  1702. }
  1703. }
  1704. var frozenCols = $(target).datagrid("getColumnFields", true);
  1705. var fitCols = $(target).datagrid("getColumnFields", false);
  1706. var tr1 = "<tr datagrid-row-index=\"" + rowIndex + "\">" + this.renderRow.call(this, target, frozenCols, true, rowIndex, row) + "</tr>";
  1707. var tr2 = "<tr datagrid-row-index=\"" + rowIndex + "\">" + this.renderRow.call(this, target, fitCols, false, rowIndex, row) + "</tr>";
  1708. if (rowIndex >= data.rows.length) {
  1709. var view1BodyInner = view1.children("div.datagrid-body").children("div.datagrid-body-inner");
  1710. var view2Body = view2.children("div.datagrid-body");
  1711. if (data.rows.length) {
  1712. view1BodyInner.find("tr:last[datagrid-row-index]").after(tr1);
  1713. view2Body.find("tr:last[datagrid-row-index]").after(tr2);
  1714. } else {
  1715. view1BodyInner.html("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody>" + tr1 + "</tbody></table>");
  1716. view2Body.html("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody>" + tr2 + "</tbody></table>");
  1717. }
  1718. } else {
  1719. view1.children("div.datagrid-body").find("tr[datagrid-row-index=" + (rowIndex + 1) + "]").before(tr1);
  1720. view2.children("div.datagrid-body").find("tr[datagrid-row-index=" + (rowIndex + 1) + "]").before(tr2);
  1721. }
  1722. data.total += 1;
  1723. data.rows.splice(rowIndex, 0, row);
  1724. this.refreshRow.call(this, target, rowIndex);
  1725. }, deleteRow: function (target, rowIndex) {
  1726. var opts = $.data(target, "datagrid").options;
  1727. var data = $.data(target, "datagrid").data;
  1728. var view = $(target).datagrid("getPanel").children("div.datagrid-view");
  1729. var view1 = view.children("div.datagrid-view1");
  1730. var view2 = view.children("div.datagrid-view2");
  1731. view1.children("div.datagrid-body").find("tr[datagrid-row-index=" + rowIndex + "]").remove();
  1732. view2.children("div.datagrid-body").find("tr[datagrid-row-index=" + rowIndex + "]").remove();
  1733. for (var i = rowIndex + 1; i < data.rows.length; i++) {
  1734. view2.children("div.datagrid-body").find("tr[datagrid-row-index=" + i + "]").attr("datagrid-row-index", i - 1);
  1735. var tr = view1.children("div.datagrid-body").find("tr[datagrid-row-index=" + i + "]").attr("datagrid-row-index", i - 1);
  1736. if (opts.rownumbers) {
  1737. tr.find("div.datagrid-cell-rownumber").html(i);
  1738. }
  1739. }
  1740. data.total -= 1;
  1741. data.rows.splice(rowIndex, 1);
  1742. }, onBeforeRender: function (target, rows) {
  1743. }, onAfterRender: function (target) {
  1744. var opts = $.data(target, "datagrid").options;
  1745. if (opts.showFooter) {
  1746. var footer = $(target).datagrid("getPanel").find("div.datagrid-footer");
  1747. footer.find("div.datagrid-cell-rownumber,div.datagrid-cell-check").css("visibility", "hidden");
  1748. }
  1749. }
  1750. };
  1751. $.fn.datagrid.defaults = $.extend({}, $.fn.panel.defaults, { frozenColumns: null, columns: null, fitColumns: false, toolbar: null, striped: false, method: "post", nowrap: true, idField: null, url: null, loadMsg: "Processing, please wait ...", rownumbers: false, singleSelect: false, pagination: false, pageNumber: 1, pageSize: 10, pageList: [10, 20, 30, 40, 50], queryParams: {}, sortName: null, sortOrder: "asc", remoteSort: true, showHeader: true, showFooter: false, scrollbarSize: 18, rowStyler: function (_1d1, _1d2) {
  1752. }, loadFilter: function (data) {
  1753. if (typeof data.length == "number" && typeof data.splice == "function") {
  1754. return { total: data.length, rows: data };
  1755. } else {
  1756. return data;
  1757. }
  1758. }, editors: editorsObj, editConfig: { getTr: function (target, index) {
  1759. return $(target).datagrid("getPanel").find("div.datagrid-body tr[datagrid-row-index=" + index + "]");
  1760. }, getRow: function (target, rowIndex) {
  1761. return $.data(target, "datagrid").data.rows[rowIndex];
  1762. }
  1763. }, view: renderView, onBeforeLoad: function (param) {
  1764. }, onLoadSuccess: function () {
  1765. }, onLoadError: function () {
  1766. }, onClickRow: function (rowIndex, rowData) {
  1767. }, onDblClickRow: function (rowIndex, rowData) {
  1768. }, onClickCell: function (rowIndex, filed, value) {
  1769. }, onDblClickCell: function (rowIndex, filed, value) {
  1770. }, onSortColumn: function (sort, order) {
  1771. }, onResizeColumn: function (filed, width) {
  1772. }, onSelect: function (rowIndex, rowData) {
  1773. }, onUnselect: function (rowIndex, rowData) {
  1774. }, onSelectAll: function (rows) {
  1775. }, onUnselectAll: function (rows) {
  1776. }, onBeforeEdit: function (rowIndex, rowData) {
  1777. }, onAfterEdit: function (rowIndex, rowData, changes) {
  1778. }, onCancelEdit: function (rowIndex, rowData) {
  1779. }, onHeaderContextMenu: function (e, filed) {
  1780. }, onRowContextMenu: function (e, rowIndex, rowData) {
  1781. }
  1782. });
  1783. })(jQuery);

  1784. END
复制代码
E

您需要登录后才可以回帖 登录 | 立即注册

Archiver|手机版|学JAVA网

GMT+8, 2018-10-20 01:14 , Processed in 0.163483 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Copyright © 2015-2018 xuejava网 / 鲁ICP备17054568号-1
回顶部