仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ imsetes/js/bootstrap-table/extensions/group-by-v2/bootstrap-table-group-by.js 7.0 KB · 277 行 原始文件
Z zfhsh first commit 2 个月前
1/**
2 * @author: Yura Knoxville
3 * @version: v1.1.0
4 */
5
6let initBodyCaller
7
8// it only does '%s', and return '' when arguments are undefined
9const sprintf = function (str) {
10 const args = arguments
11 let flag = true
12 let i = 1
13
14 str = str.replace(/%s/g, () => {
15 const arg = args[i++]
16
17 if (typeof arg === 'undefined') {
18 flag = false
19 return ''
20 }
21 return arg
22 })
23 return flag ? str : ''
24}
25
26const groupBy = (array, f) => {
27 const tmpGroups = {}
28 array.forEach(o => {
29 const groups = f(o)
30 tmpGroups[groups] = tmpGroups[groups] || []
31 tmpGroups[groups].push(o)
32 })
33
34 return tmpGroups
35}
36
37$.extend($.fn.bootstrapTable.defaults, {
38 groupBy: false,
39 groupByField: '',
40 groupByFormatter: undefined
41})
42
43const Utils = $.fn.bootstrapTable.utils
44const BootstrapTable = $.fn.bootstrapTable.Constructor
45const _initSort = BootstrapTable.prototype.initSort
46const _initBody = BootstrapTable.prototype.initBody
47const _updateSelected = BootstrapTable.prototype.updateSelected
48
49BootstrapTable.prototype.initSort = function (...args) {
50 _initSort.apply(this, Array.prototype.slice.apply(args))
51
52 const that = this
53 this.tableGroups = []
54
55 if ((this.options.groupBy) && (this.options.groupByField !== '')) {
56
57 if ((this.options.sortName !== this.options.groupByField)) {
58 if (this.options.customSort) {
59 Utils.calculateObjectValue(this.options, this.options.customSort, [
60 this.options.sortName,
61 this.options.sortOrder,
62 this.data
63 ])
64 } else {
65 this.data.sort((a, b) => {
66 const groupByFields = this.getGroupByFields()
67 const fieldValuesA = []
68 const fieldValuesB = []
69
70 $.each(groupByFields, (i, field) => {
71 fieldValuesA.push(a[field])
72 fieldValuesB.push(b[field])
73 })
74
75 a = fieldValuesA.join()
76 b = fieldValuesB.join()
77 return a.localeCompare(b, undefined, {numeric: true})
78 })
79 }
80 }
81
82 const groups = groupBy(that.data, (item) => {
83 const groupByFields = this.getGroupByFields()
84 const groupValues = []
85 $.each(groupByFields, (i, field) => {
86 groupValues.push(item[field])
87 })
88
89 return groupValues.join(', ')
90 })
91
92 let index = 0
93 $.each(groups, (key, value) => {
94 this.tableGroups.push({
95 id: index,
96 name: key,
97 data: value
98 })
99
100 value.forEach(item => {
101 if (!item._data) {
102 item._data = {}
103 }
104
105 item._data['parent-index'] = index
106 })
107
108 index++
109 })
110 }
111}
112
113BootstrapTable.prototype.initBody = function (...args) {
114 initBodyCaller = true
115
116 _initBody.apply(this, Array.prototype.slice.apply(args))
117
118 if ((this.options.groupBy) && (this.options.groupByField !== '')) {
119 const that = this
120 let checkBox = false
121 let visibleColumns = 0
122
123 this.columns.forEach(column => {
124 if (column.checkbox) {
125 checkBox = true
126 } else {
127 if (column.visible) {
128 visibleColumns += 1
129 }
130 }
131 })
132
133 if (this.options.detailView && !this.options.cardView) {
134 visibleColumns += 1
135 }
136
137 this.tableGroups.forEach(item => {
138 const html = []
139
140 html.push(sprintf('<tr class="info groupBy expanded" data-group-index="%s">', item.id))
141
142 if (that.options.detailView && !that.options.cardView) {
143 html.push('<td class="detail"></td>')
144 }
145
146 if (checkBox) {
147 html.push('<td class="bs-checkbox">',
148 '<input name="btSelectGroup" type="checkbox" />',
149 '</td>'
150 )
151 }
152 let formattedValue = item.name
153 if (typeof (that.options.groupByFormatter) === 'function') {
154 formattedValue = that.options.groupByFormatter(item.name, item.id, item.data)
155 }
156 html.push('<td',
157 sprintf(' colspan="%s"', visibleColumns),
158 '>', formattedValue, '</td>'
159 )
160
161 html.push('</tr>')
162
163 that.$body.find(`tr[data-parent-index=${item.id}]:first`).before($(html.join('')))
164 })
165
166 this.$selectGroup = []
167 this.$body.find('[name="btSelectGroup"]').each(function () {
168 const self = $(this)
169
170 that.$selectGroup.push({
171 group: self,
172 item: that.$selectItem.filter(function () {
173 return ($(this).closest('tr').data('parent-index') ===
174 self.closest('tr').data('group-index'))
175 })
176 })
177 })
178
179 this.$container.off('click', '.groupBy')
180 .on('click', '.groupBy', function () {
181 $(this).toggleClass('expanded')
182 that.$body.find(`tr[data-parent-index=${$(this).closest('tr').data('group-index')}]`).toggleClass('hidden')
183 })
184
185 this.$container.off('click', '[name="btSelectGroup"]')
186 .on('click', '[name="btSelectGroup"]', function (event) {
187 event.stopImmediatePropagation()
188
189 const self = $(this)
190 const checked = self.prop('checked')
191 that[checked ? 'checkGroup' : 'uncheckGroup']($(this).closest('tr').data('group-index'))
192 })
193 }
194
195 initBodyCaller = false
196 this.updateSelected()
197}
198
199BootstrapTable.prototype.updateSelected = function (...args) {
200 if (!initBodyCaller) {
201 _updateSelected.apply(this, Array.prototype.slice.apply(args))
202
203 if ((this.options.groupBy) && (this.options.groupByField !== '')) {
204 this.$selectGroup.forEach(item => {
205 const checkGroup = item.item.filter(':enabled').length ===
206 item.item.filter(':enabled').filter(':checked').length
207
208 item.group.prop('checked', checkGroup)
209 })
210 }
211 }
212}
213
214BootstrapTable.prototype.checkGroup = function (index) {
215 this.checkGroup_(index, true)
216}
217
218BootstrapTable.prototype.uncheckGroup = function (index) {
219 this.checkGroup_(index, false)
220}
221
222BootstrapTable.prototype.checkGroup_ = function (index, checked) {
223 const rowsBefore = this.getSelections()
224 let rows
225 const filter = function () {
226 return ($(this).closest('tr').data('parent-index') === index)
227 }
228
229 this.$selectItem.filter(filter).prop('checked', checked)
230
231 this.updateRows()
232 this.updateSelected()
233 const rowsAfter = this.getSelections()
234 if (checked) {
235 this.trigger('check-all', rowsAfter, rowsBefore)
236 return
237 }
238
239 this.trigger('uncheck-all', rowsAfter, rowsBefore)
240}
241
242BootstrapTable.prototype.getGroupByFields = function () {
243 let groupByFields = this.options.groupByField
244 if (!$.isArray(this.options.groupByField)) {
245 groupByFields = [this.options.groupByField]
246 }
247
248 return groupByFields
249}
250
251$.BootstrapTable = class extends $.BootstrapTable {
252 scrollTo (params) {
253 if (this.options.groupBy) {
254 let options = {unit: 'px', value: 0}
255 if (typeof params === 'object') {
256 options = Object.assign(options, params)
257 }
258
259 if (options.unit === 'rows') {
260 let scrollTo = 0
261 this.$body.find(`> tr:lt(${options.value})`).each((i, el) => {
262 scrollTo += $(el).outerHeight(true)
263 })
264
265 const $targetColumn = this.$body.find(`> tr:not(.groupBy):eq(${options.value})`)
266 $targetColumn.prevAll('.groupBy').each((i, el) => {
267 scrollTo += $(el).outerHeight(true)
268 })
269
270 this.$tableBody.scrollTop(scrollTo)
271 return
272 }
273 }
274
275 super.scrollTo(params)
276 }
277}