UNPKG

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