bootstrap-table-treegrid.js 3.9 KB

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