UNPKG

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