UNPKG

2.77 kBJavaScriptView Raw
1/**
2 * @author: YL
3 * @update: zhixin wen <wenzhixin2010@gmail.com>
4 */
5
6const Utils = $.fn.bootstrapTable.utils
7
8Object.assign($.fn.bootstrapTable.defaults, {
9 treeEnable: false,
10 treeShowField: null,
11 idField: 'id',
12 parentIdField: 'pid',
13 rootParentId: null
14})
15
16$.BootstrapTable = class extends $.BootstrapTable {
17
18 init (...args) {
19 this._rowStyle = this.options.rowStyle
20 super.init(...args)
21 }
22
23 initHeader (...args) {
24 super.initHeader(...args)
25 const treeShowField = this.options.treeShowField
26
27 if (treeShowField) {
28 for (const field of this.header.fields) {
29 if (treeShowField === field) {
30 this.treeEnable = true
31 break
32 }
33 }
34 }
35 }
36
37 initBody (...args) {
38 if (this.treeEnable) {
39 this.options.virtualScroll = false
40 }
41 super.initBody(...args)
42 }
43
44 initTr (item, idx, data, parentDom) {
45 const nodes = data.filter(it => item[this.options.idField] === it[this.options.parentIdField])
46
47 parentDom.append(super.initRow(item, idx, data, parentDom))
48
49 // init sub node
50 const len = nodes.length - 1
51
52 for (let i = 0; i <= len; i++) {
53 const node = nodes[i]
54 const defaultItem = Utils.extend(true, {}, item)
55
56 node._level = defaultItem._level + 1
57 node._parent = defaultItem
58 if (i === len) {
59 node._last = 1
60 }
61 // jquery.treegrid.js
62 this.options.rowStyle = (item, idx) => {
63 const res = this._rowStyle(item, idx)
64 const id = item[this.options.idField] ? item[this.options.idField] : 0
65 const pid = item[this.options.parentIdField] ? item[this.options.parentIdField] : 0
66
67 res.classes = [
68 res.classes || '',
69 `treegrid-${id}`,
70 `treegrid-parent-${pid}`
71 ].join(' ')
72 return res
73 }
74 this.initTr(node, $.inArray(node, data), data, parentDom)
75 }
76 }
77
78 initRow (item, idx, data, parentDom) {
79 if (this.treeEnable) {
80 if (
81 this.options.rootParentId === item[this.options.parentIdField] ||
82 !item[this.options.parentIdField]
83 ) {
84 if (item._level === undefined) {
85 item._level = 0
86 }
87 // jquery.treegrid.js
88 this.options.rowStyle = (item, idx) => {
89 const res = this._rowStyle(item, idx)
90 const x = item[this.options.idField] ? item[this.options.idField] : 0
91
92 res.classes = [
93 res.classes || '',
94 `treegrid-${x}`
95 ].join(' ')
96 return res
97 }
98 this.initTr(item, idx, data, parentDom)
99 return true
100 }
101 return false
102 }
103 return super.initRow(item, idx, data, parentDom)
104 }
105
106 destroy (...args) {
107 super.destroy(...args)
108 this.options.rowStyle = this._rowStyle
109 }
110}