bootstrap-table-tree-column.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.bootstrapTableTreeColumn = mod.exports;
  12. }
  13. })(this, function () {
  14. 'use strict';
  15. /**
  16. * @author: KingYang
  17. * @webSite: https://github.com/kingyang
  18. * @version: v1.0.0
  19. */
  20. !function ($) {
  21. 'use strict';
  22. $.extend($.fn.bootstrapTable.defaults, {
  23. treeShowField: null,
  24. idField: 'id',
  25. parentIdField: 'pid',
  26. treeVerticalcls: 'vertical',
  27. treeVerticalLastcls: 'vertical last',
  28. treeSpacecls: 'space',
  29. treeNodecls: 'node',
  30. treeCellcls: 'treenode',
  31. treeTextcls: 'text',
  32. onTreeFormatter: function onTreeFormatter(row) {
  33. var that = this,
  34. options = that.options,
  35. level = row._level || 0,
  36. plevel = row._parent && row._parent._level || 0,
  37. paddings = [];
  38. for (var i = 0; i < plevel; i++) {
  39. paddings.push('<i class="' + options.treeVerticalcls + '"></i>');
  40. paddings.push('<i class="' + options.treeSpacecls + '"></i>');
  41. }
  42. for (var i = plevel; i < level; i++) {
  43. if (row._last && i === level - 1) {
  44. paddings.push('<i class="' + options.treeVerticalLastcls + '"></i>');
  45. } else {
  46. paddings.push('<i class="' + options.treeVerticalcls + '"></i>');
  47. }
  48. paddings.push('<i class="' + options.treeNodecls + '"></i>');
  49. }
  50. return paddings.join('');
  51. }, onGetNodes: function onGetNodes(row, data) {
  52. var that = this;
  53. var nodes = [];
  54. $.each(data, function (i, item) {
  55. if (row[that.options.idField] === item[that.options.parentIdField]) {
  56. nodes.push(item);
  57. }
  58. });
  59. return nodes;
  60. },
  61. onCheckLeaf: function onCheckLeaf(row, data) {
  62. if (row.isLeaf !== undefined) {
  63. return row.isLeaf;
  64. }
  65. return !row._nodes || !row._nodes.length;
  66. }, onCheckRoot: function onCheckRoot(row, data) {
  67. var that = this;
  68. return !row[that.options.parentIdField];
  69. }
  70. });
  71. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  72. _initRow = BootstrapTable.prototype.initRow,
  73. _initHeader = BootstrapTable.prototype.initHeader;
  74. BootstrapTable.prototype.initHeader = function () {
  75. var that = this;
  76. _initHeader.apply(that, Array.prototype.slice.apply(arguments));
  77. var treeShowField = that.options.treeShowField;
  78. if (treeShowField) {
  79. $.each(this.header.fields, function (i, field) {
  80. if (treeShowField === field) {
  81. that.treeEnable = true;
  82. var _formatter = that.header.formatters[i];
  83. var _class = [that.options.treeCellcls];
  84. if (that.header.classes[i]) {
  85. _class.push(that.header.classes[i].split('"')[1] || '');
  86. }
  87. that.header.classes[i] = ' class="' + _class.join(' ') + '"';
  88. that.header.formatters[i] = function (value, row, index) {
  89. var colTree = [that.options.onTreeFormatter.apply(that, [row])];
  90. colTree.push('<span class="' + that.options.treeTextcls + '">');
  91. if (_formatter) {
  92. colTree.push(_formatter.apply(this, Array.prototype.slice.apply(arguments)));
  93. } else {
  94. colTree.push(value);
  95. }
  96. colTree.push('</span>');
  97. return colTree.join('');
  98. };
  99. return false;
  100. }
  101. });
  102. }
  103. };
  104. var initNode = function initNode(item, idx, data, parentDom) {
  105. var that = this;
  106. var nodes = that.options.onGetNodes.apply(that, [item, data]);
  107. item._nodes = nodes;
  108. parentDom.append(_initRow.apply(that, [item, idx, data, parentDom]));
  109. var len = nodes.length - 1;
  110. for (var i = 0; i <= len; i++) {
  111. var node = nodes[i];
  112. node._level = item._level + 1;
  113. node._parent = item;
  114. if (i === len) node._last = 1;
  115. initNode.apply(that, [node, $.inArray(node, data), data, parentDom]);
  116. }
  117. };
  118. BootstrapTable.prototype.initRow = function (item, idx, data, parentDom) {
  119. var that = this;
  120. if (that.treeEnable) {
  121. if (that.options.onCheckRoot.apply(that, [item, data])) {
  122. if (item._level === undefined) {
  123. item._level = 0;
  124. }
  125. initNode.apply(that, [item, idx, data, parentDom]);
  126. return true;
  127. }
  128. return false;
  129. }
  130. return _initRow.apply(that, Array.prototype.slice.apply(arguments));
  131. };
  132. }(jQuery);
  133. });