bootstrap-table-tree-column.js 4.8 KB

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