UNPKG

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