bootstrap-table-treegrid.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. onGetNodes: function onGetNodes(row, data) {
  26. var that = this;
  27. var nodes = [];
  28. $.each(data, function (i, item) {
  29. if (row[that.options.idField] === item[that.options.parentIdField]) {
  30. nodes.push(item);
  31. }
  32. });
  33. return nodes;
  34. },
  35. onCheckRoot: function onCheckRoot(row, data) {
  36. var that = this;
  37. return !row[that.options.parentIdField];
  38. }
  39. });
  40. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  41. _init = BootstrapTable.prototype.init,
  42. _initRow = BootstrapTable.prototype.initRow,
  43. _initHeader = BootstrapTable.prototype.initHeader,
  44. _rowStyle = null;
  45. BootstrapTable.prototype.init = function () {
  46. _rowStyle = this.options.rowStyle;
  47. _init.apply(this, Array.prototype.slice.apply(arguments));
  48. };
  49. // td
  50. BootstrapTable.prototype.initHeader = function () {
  51. var that = this;
  52. _initHeader.apply(that, Array.prototype.slice.apply(arguments));
  53. var treeShowField = that.options.treeShowField;
  54. if (treeShowField) {
  55. $.each(this.header.fields, function (i, field) {
  56. if (treeShowField === field) {
  57. that.treeEnable = true;
  58. return false;
  59. }
  60. });
  61. }
  62. };
  63. var initTr = function initTr(item, idx, data, parentDom) {
  64. var that = this;
  65. var nodes = that.options.onGetNodes.apply(that, [item, data]);
  66. item._nodes = nodes;
  67. parentDom.append(_initRow.apply(that, [item, idx, data, parentDom]));
  68. // init sub node
  69. var len = nodes.length - 1;
  70. for (var i = 0; i <= len; i++) {
  71. var node = nodes[i];
  72. node._level = item._level + 1;
  73. node._parent = item;
  74. if (i === len) node._last = 1;
  75. // jquery.treegrid.js
  76. that.options.rowStyle = function (item, idx) {
  77. var res = _rowStyle.apply(that, Array.prototype.slice.apply(arguments));
  78. var id = item[that.options.idField] ? item[that.options.idField] : 0;
  79. var pid = item[that.options.parentIdField] ? item[that.options.parentIdField] : 0;
  80. res.classes = [res.classes || '', 'treegrid-' + id, 'treegrid-parent-' + pid].join(' ');
  81. return res;
  82. };
  83. initTr.apply(that, [node, $.inArray(node, data), data, parentDom]);
  84. }
  85. };
  86. // tr
  87. BootstrapTable.prototype.initRow = function (item, idx, data, parentDom) {
  88. var that = this;
  89. if (that.treeEnable) {
  90. // init root node
  91. if (that.options.onCheckRoot.apply(that, [item, data])) {
  92. if (item._level === undefined) {
  93. item._level = 0;
  94. }
  95. // jquery.treegrid.js
  96. that.options.rowStyle = function (item, idx) {
  97. var res = _rowStyle.apply(that, Array.prototype.slice.apply(arguments));
  98. var x = item[that.options.idField] ? item[that.options.idField] : 0;
  99. res.classes = [res.classes || '', 'treegrid-' + x].join(' ');
  100. return res;
  101. };
  102. initTr.apply(that, [item, idx, data, parentDom]);
  103. return true;
  104. }
  105. return false;
  106. }
  107. return _initRow.apply(that, Array.prototype.slice.apply(arguments));
  108. };
  109. }(jQuery);
  110. });