bootstrap-table-treegrid.js 3.8 KB

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