bootstrap-table-treegrid.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define([], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory();
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory();
  11. global.bootstrapTableTreegrid = mod.exports;
  12. }
  13. })(this, function () {
  14. 'use strict';
  15. /**
  16. * @author: YL
  17. * @version: v1.0.0
  18. */
  19. !function ($) {
  20. 'use strict';
  21. $.extend($.fn.bootstrapTable.defaults, {
  22. treeShowField: null,
  23. idField: 'id',
  24. parentIdField: 'pid',
  25. rootParentId: null,
  26. onGetNodes: function onGetNodes(row, data) {
  27. var that = this;
  28. var nodes = [];
  29. $.each(data, function (i, item) {
  30. if (row[that.options.idField] === item[that.options.parentIdField]) {
  31. nodes.push(item);
  32. }
  33. });
  34. return nodes;
  35. },
  36. onCheckRoot: function onCheckRoot(row, data) {
  37. var that = this;
  38. return that.options.rootParentId === row[that.options.parentIdField] || !row[that.options.parentIdField];
  39. }
  40. });
  41. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  42. _init = BootstrapTable.prototype.init,
  43. _initRow = BootstrapTable.prototype.initRow,
  44. _initHeader = BootstrapTable.prototype.initHeader,
  45. _rowStyle = null;
  46. BootstrapTable.prototype.init = function () {
  47. _rowStyle = this.options.rowStyle;
  48. _init.apply(this, Array.prototype.slice.apply(arguments));
  49. };
  50. // td
  51. BootstrapTable.prototype.initHeader = function () {
  52. var that = this;
  53. _initHeader.apply(that, Array.prototype.slice.apply(arguments));
  54. var treeShowField = that.options.treeShowField;
  55. if (treeShowField) {
  56. $.each(this.header.fields, function (i, field) {
  57. if (treeShowField === field) {
  58. that.treeEnable = true;
  59. return false;
  60. }
  61. });
  62. }
  63. };
  64. var initTr = function initTr(item, idx, data, parentDom) {
  65. var that = this;
  66. var nodes = that.options.onGetNodes.apply(that, [item, data]);
  67. item._nodes = nodes;
  68. parentDom.append(_initRow.apply(that, [item, idx, data, parentDom]));
  69. // init sub node
  70. var len = nodes.length - 1;
  71. for (var i = 0; i <= len; i++) {
  72. var node = nodes[i];
  73. node._level = item._level + 1;
  74. node._parent = item;
  75. if (i === len) node._last = 1;
  76. // jquery.treegrid.js
  77. that.options.rowStyle = function (item, idx) {
  78. var res = _rowStyle.apply(that, Array.prototype.slice.apply(arguments));
  79. var id = item[that.options.idField] ? item[that.options.idField] : 0;
  80. var pid = item[that.options.parentIdField] ? item[that.options.parentIdField] : 0;
  81. res.classes = [res.classes || '', 'treegrid-' + id, 'treegrid-parent-' + pid].join(' ');
  82. return res;
  83. };
  84. initTr.apply(that, [node, $.inArray(node, data), data, parentDom]);
  85. }
  86. };
  87. // tr
  88. BootstrapTable.prototype.initRow = function (item, idx, data, parentDom) {
  89. var that = this;
  90. if (that.treeEnable) {
  91. // init root node
  92. if (that.options.onCheckRoot.apply(that, [item, data])) {
  93. if (item._level === undefined) {
  94. item._level = 0;
  95. }
  96. // jquery.treegrid.js
  97. that.options.rowStyle = function (item, idx) {
  98. var res = _rowStyle.apply(that, Array.prototype.slice.apply(arguments));
  99. var x = item[that.options.idField] ? item[that.options.idField] : 0;
  100. res.classes = [res.classes || '', 'treegrid-' + x].join(' ');
  101. return res;
  102. };
  103. initTr.apply(that, [item, idx, data, parentDom]);
  104. return true;
  105. }
  106. return false;
  107. }
  108. return _initRow.apply(that, Array.prototype.slice.apply(arguments));
  109. };
  110. }(jQuery);
  111. });