1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import Constants from './constants/index.js'
|
8 | import Utils from './utils/index.js'
|
9 | import VirtualScroll from './virtual-scroll/index.js'
|
10 |
|
11 | class BootstrapTable {
|
12 | constructor (el, options) {
|
13 | this.options = options
|
14 | this.$el = $(el)
|
15 | this.$el_ = this.$el.clone()
|
16 | this.timeoutId_ = 0
|
17 | this.timeoutFooter_ = 0
|
18 | }
|
19 |
|
20 | init () {
|
21 | this.initConstants()
|
22 | this.initLocale()
|
23 | this.initContainer()
|
24 | this.initTable()
|
25 | this.initHeader()
|
26 | this.initData()
|
27 | this.initHiddenRows()
|
28 | this.initToolbar()
|
29 | this.initPagination()
|
30 | this.initBody()
|
31 | this.initSearchText()
|
32 | this.initServer()
|
33 | }
|
34 |
|
35 | initConstants () {
|
36 | const opts = this.options
|
37 |
|
38 | this.constants = Constants.CONSTANTS
|
39 | this.constants.theme = $.fn.bootstrapTable.theme
|
40 | this.constants.dataToggle = this.constants.html.dataToggle || 'data-toggle'
|
41 |
|
42 |
|
43 | const iconsPrefix = Utils.getIconsPrefix($.fn.bootstrapTable.theme)
|
44 | const icons = Utils.getIcons(iconsPrefix)
|
45 |
|
46 | if (typeof opts.icons === 'string') {
|
47 | opts.icons = Utils.calculateObjectValue(null, opts.icons)
|
48 | }
|
49 |
|
50 | opts.iconsPrefix = opts.iconsPrefix || $.fn.bootstrapTable.defaults.iconsPrefix || iconsPrefix
|
51 | opts.icons = Object.assign(icons, $.fn.bootstrapTable.defaults.icons, opts.icons)
|
52 |
|
53 |
|
54 | const buttonsPrefix = opts.buttonsPrefix ? `${opts.buttonsPrefix}-` : ''
|
55 |
|
56 | this.constants.buttonsClass = [
|
57 | opts.buttonsPrefix,
|
58 | buttonsPrefix + opts.buttonsClass,
|
59 | Utils.sprintf(`${buttonsPrefix}%s`, opts.iconSize)
|
60 | ].join(' ').trim()
|
61 |
|
62 | this.buttons = Utils.calculateObjectValue(this, opts.buttons, [], {})
|
63 | if (typeof this.buttons !== 'object') {
|
64 | this.buttons = {}
|
65 | }
|
66 | }
|
67 |
|
68 | initLocale () {
|
69 | if (this.options.locale) {
|
70 | const locales = $.fn.bootstrapTable.locales
|
71 | const parts = this.options.locale.split(/-|_/)
|
72 |
|
73 | parts[0] = parts[0].toLowerCase()
|
74 | if (parts[1]) {
|
75 | parts[1] = parts[1].toUpperCase()
|
76 | }
|
77 |
|
78 | let localesToExtend = {}
|
79 |
|
80 | if (locales[this.options.locale]) {
|
81 | localesToExtend = locales[this.options.locale]
|
82 | } else if (locales[parts.join('-')]) {
|
83 | localesToExtend = locales[parts.join('-')]
|
84 | } else if (locales[parts[0]]) {
|
85 | localesToExtend = locales[parts[0]]
|
86 | }
|
87 |
|
88 | for (const [formatName, func] of Object.entries(localesToExtend)) {
|
89 | if (this.options[formatName] !== BootstrapTable.DEFAULTS[formatName]) {
|
90 | continue
|
91 | }
|
92 |
|
93 | this.options[formatName] = func
|
94 | }
|
95 | }
|
96 | }
|
97 |
|
98 | initContainer () {
|
99 | const topPagination = ['top', 'both'].includes(this.options.paginationVAlign) ?
|
100 | '<div class="fixed-table-pagination clearfix"></div>' : ''
|
101 | const bottomPagination = ['bottom', 'both'].includes(this.options.paginationVAlign) ?
|
102 | '<div class="fixed-table-pagination"></div>' : ''
|
103 | const loadingTemplate = Utils.calculateObjectValue(this.options,
|
104 | this.options.loadingTemplate, [this.options.formatLoadingMessage()])
|
105 |
|
106 | this.$container = $(`
|
107 | <div class="bootstrap-table ${this.constants.theme}">
|
108 | <div class="fixed-table-toolbar"></div>
|
109 | ${topPagination}
|
110 | <div class="fixed-table-container">
|
111 | <div class="fixed-table-header"><table></table></div>
|
112 | <div class="fixed-table-body">
|
113 | <div class="fixed-table-loading">
|
114 | ${loadingTemplate}
|
115 | </div>
|
116 | </div>
|
117 | <div class="fixed-table-footer"></div>
|
118 | </div>
|
119 | ${bottomPagination}
|
120 | </div>
|
121 | `)
|
122 |
|
123 | this.$container.insertAfter(this.$el)
|
124 | this.$tableContainer = this.$container.find('.fixed-table-container')
|
125 | this.$tableHeader = this.$container.find('.fixed-table-header')
|
126 | this.$tableBody = this.$container.find('.fixed-table-body')
|
127 | this.$tableLoading = this.$container.find('.fixed-table-loading')
|
128 | this.$tableFooter = this.$el.find('tfoot')
|
129 |
|
130 | if (this.options.buttonsToolbar) {
|
131 | this.$toolbar = $('body').find(this.options.buttonsToolbar)
|
132 | } else {
|
133 | this.$toolbar = this.$container.find('.fixed-table-toolbar')
|
134 | }
|
135 | this.$pagination = this.$container.find('.fixed-table-pagination')
|
136 |
|
137 | this.$tableBody.append(this.$el)
|
138 | this.$container.after('<div class="clearfix"></div>')
|
139 |
|
140 | this.$el.addClass(this.options.classes)
|
141 | this.$tableLoading.addClass(this.options.classes)
|
142 |
|
143 | if (this.options.height) {
|
144 | this.$tableContainer.addClass('fixed-height')
|
145 |
|
146 | if (this.options.showFooter) {
|
147 | this.$tableContainer.addClass('has-footer')
|
148 | }
|
149 |
|
150 | if (this.options.classes.split(' ').includes('table-bordered')) {
|
151 | this.$tableBody.append('<div class="fixed-table-border"></div>')
|
152 | this.$tableBorder = this.$tableBody.find('.fixed-table-border')
|
153 | this.$tableLoading.addClass('fixed-table-border')
|
154 | }
|
155 |
|
156 | this.$tableFooter = this.$container.find('.fixed-table-footer')
|
157 | }
|
158 | }
|
159 |
|
160 | initTable () {
|
161 | const columns = []
|
162 |
|
163 | this.$header = this.$el.find('>thead')
|
164 | if (!this.$header.length) {
|
165 | this.$header = $(`<thead class="${this.options.theadClasses}"></thead>`).appendTo(this.$el)
|
166 | } else if (this.options.theadClasses) {
|
167 | this.$header.addClass(this.options.theadClasses)
|
168 | }
|
169 |
|
170 | this._headerTrClasses = []
|
171 | this._headerTrStyles = []
|
172 | this.$header.find('tr').each((i, el) => {
|
173 | const $tr = $(el)
|
174 | const column = []
|
175 |
|
176 | $tr.find('th').each((i, el) => {
|
177 | const $th = $(el)
|
178 |
|
179 |
|
180 | if (typeof $th.data('field') !== 'undefined') {
|
181 | $th.data('field', `${$th.data('field')}`)
|
182 | }
|
183 | column.push($.extend({}, {
|
184 | title: $th.html(),
|
185 | class: $th.attr('class'),
|
186 | titleTooltip: $th.attr('title'),
|
187 | rowspan: $th.attr('rowspan') ? +$th.attr('rowspan') : undefined,
|
188 | colspan: $th.attr('colspan') ? +$th.attr('colspan') : undefined
|
189 | }, $th.data()))
|
190 | })
|
191 | columns.push(column)
|
192 |
|
193 | if ($tr.attr('class')) {
|
194 | this._headerTrClasses.push($tr.attr('class'))
|
195 | }
|
196 | if ($tr.attr('style')) {
|
197 | this._headerTrStyles.push($tr.attr('style'))
|
198 | }
|
199 | })
|
200 |
|
201 | if (!Array.isArray(this.options.columns[0])) {
|
202 | this.options.columns = [this.options.columns]
|
203 | }
|
204 |
|
205 | this.options.columns = $.extend(true, [], columns, this.options.columns)
|
206 | this.columns = []
|
207 | this.fieldsColumnsIndex = []
|
208 |
|
209 | Utils.setFieldIndex(this.options.columns)
|
210 |
|
211 | this.options.columns.forEach((columns, i) => {
|
212 | columns.forEach((_column, j) => {
|
213 | const column = $.extend({}, BootstrapTable.COLUMN_DEFAULTS, _column)
|
214 |
|
215 | if (typeof column.fieldIndex !== 'undefined') {
|
216 | this.columns[column.fieldIndex] = column
|
217 | this.fieldsColumnsIndex[column.field] = column.fieldIndex
|
218 | }
|
219 |
|
220 | this.options.columns[i][j] = column
|
221 | })
|
222 | })
|
223 |
|
224 |
|
225 | if (!this.options.data.length) {
|
226 | const htmlData = Utils.trToData(this.columns, this.$el.find('>tbody>tr'))
|
227 |
|
228 | if (htmlData.length) {
|
229 | this.options.data = htmlData
|
230 | this.fromHtml = true
|
231 | }
|
232 | }
|
233 |
|
234 | if (!(this.options.pagination && this.options.sidePagination !== 'server')) {
|
235 | this.footerData = Utils.trToData(this.columns, this.$el.find('>tfoot>tr'))
|
236 | }
|
237 |
|
238 | if (this.footerData) {
|
239 | this.$el.find('tfoot').html('<tr></tr>')
|
240 | }
|
241 |
|
242 | if (!this.options.showFooter || this.options.cardView) {
|
243 | this.$tableFooter.hide()
|
244 | } else {
|
245 | this.$tableFooter.show()
|
246 | }
|
247 | }
|
248 |
|
249 | initHeader () {
|
250 | const visibleColumns = {}
|
251 | const headerHtml = []
|
252 |
|
253 | this.header = {
|
254 | fields: [],
|
255 | styles: [],
|
256 | classes: [],
|
257 | formatters: [],
|
258 | detailFormatters: [],
|
259 | events: [],
|
260 | sorters: [],
|
261 | sortNames: [],
|
262 | cellStyles: [],
|
263 | searchables: []
|
264 | }
|
265 |
|
266 | Utils.updateFieldGroup(this.options.columns, this.columns)
|
267 |
|
268 | this.options.columns.forEach((columns, i) => {
|
269 | const html = []
|
270 |
|
271 | html.push(`<tr${Utils.sprintf(' class="%s"', this._headerTrClasses[i])} ${Utils.sprintf(' style="%s"', this._headerTrStyles[i])}>`)
|
272 |
|
273 | let detailViewTemplate = ''
|
274 |
|
275 | if (i === 0 && Utils.hasDetailViewIcon(this.options)) {
|
276 | const rowspan = this.options.columns.length > 1 ?
|
277 | ` rowspan="${this.options.columns.length}"` : ''
|
278 |
|
279 | detailViewTemplate = `<th class="detail"${rowspan}>
|
280 | <div class="fht-cell"></div>
|
281 | </th>`
|
282 | }
|
283 |
|
284 | if (detailViewTemplate && this.options.detailViewAlign !== 'right') {
|
285 | html.push(detailViewTemplate)
|
286 | }
|
287 |
|
288 | columns.forEach((column, j) => {
|
289 | const class_ = Utils.sprintf(' class="%s"', column['class'])
|
290 | const unitWidth = column.widthUnit
|
291 | const width = parseFloat(column.width)
|
292 |
|
293 | const halign = Utils.sprintf('text-align: %s; ', column.halign ? column.halign : column.align)
|
294 | const align = Utils.sprintf('text-align: %s; ', column.align)
|
295 | let style = Utils.sprintf('vertical-align: %s; ', column.valign)
|
296 |
|
297 | style += Utils.sprintf('width: %s; ', (column.checkbox || column.radio) && !width ?
|
298 | (!column.showSelectTitle ? '36px' : undefined) :
|
299 | (width ? width + unitWidth : undefined))
|
300 |
|
301 | if (typeof column.fieldIndex === 'undefined' && !column.visible) {
|
302 | return
|
303 | }
|
304 |
|
305 | const headerStyle = Utils.calculateObjectValue(null, this.options.headerStyle, [column])
|
306 | const csses = []
|
307 | let classes = ''
|
308 |
|
309 | if (headerStyle && headerStyle.css) {
|
310 | for (const [key, value] of Object.entries(headerStyle.css)) {
|
311 | csses.push(`${key}: ${value}`)
|
312 | }
|
313 | }
|
314 | if (headerStyle && headerStyle.classes) {
|
315 | classes = Utils.sprintf(' class="%s"', column['class'] ?
|
316 | [column['class'], headerStyle.classes].join(' ') : headerStyle.classes)
|
317 | }
|
318 |
|
319 | if (typeof column.fieldIndex !== 'undefined') {
|
320 | this.header.fields[column.fieldIndex] = column.field
|
321 | this.header.styles[column.fieldIndex] = align + style
|
322 | this.header.classes[column.fieldIndex] = class_
|
323 | this.header.formatters[column.fieldIndex] = column.formatter
|
324 | this.header.detailFormatters[column.fieldIndex] = column.detailFormatter
|
325 | this.header.events[column.fieldIndex] = column.events
|
326 | this.header.sorters[column.fieldIndex] = column.sorter
|
327 | this.header.sortNames[column.fieldIndex] = column.sortName
|
328 | this.header.cellStyles[column.fieldIndex] = column.cellStyle
|
329 | this.header.searchables[column.fieldIndex] = column.searchable
|
330 |
|
331 | if (!column.visible) {
|
332 | return
|
333 | }
|
334 |
|
335 | if (this.options.cardView && (!column.cardVisible)) {
|
336 | return
|
337 | }
|
338 |
|
339 | visibleColumns[column.field] = column
|
340 | }
|
341 |
|
342 | html.push(`<th${Utils.sprintf(' title="%s"', column.titleTooltip)}`,
|
343 | column.checkbox || column.radio ?
|
344 | Utils.sprintf(' class="bs-checkbox %s"', column['class'] || '') :
|
345 | classes || class_,
|
346 | Utils.sprintf(' style="%s"', halign + style + csses.join('; ')),
|
347 | Utils.sprintf(' rowspan="%s"', column.rowspan),
|
348 | Utils.sprintf(' colspan="%s"', column.colspan),
|
349 | Utils.sprintf(' data-field="%s"', column.field),
|
350 |
|
351 | j === 0 && i > 0 ? ' data-not-first-th' : '',
|
352 | '>')
|
353 |
|
354 | html.push(Utils.sprintf('<div class="th-inner %s">', this.options.sortable && column.sortable ?
|
355 | 'sortable both' : ''))
|
356 |
|
357 | let text = this.options.escape ? Utils.escapeHTML(column.title) : column.title
|
358 |
|
359 | const title = text
|
360 |
|
361 | if (column.checkbox) {
|
362 | text = ''
|
363 | if (!this.options.singleSelect && this.options.checkboxHeader) {
|
364 | text = '<label><input name="btSelectAll" type="checkbox" /><span></span></label>'
|
365 | }
|
366 | this.header.stateField = column.field
|
367 | }
|
368 | if (column.radio) {
|
369 | text = ''
|
370 | this.header.stateField = column.field
|
371 | }
|
372 | if (!text && column.showSelectTitle) {
|
373 | text += title
|
374 | }
|
375 |
|
376 | html.push(text)
|
377 | html.push('</div>')
|
378 | html.push('<div class="fht-cell"></div>')
|
379 | html.push('</div>')
|
380 | html.push('</th>')
|
381 | })
|
382 |
|
383 | if (detailViewTemplate && this.options.detailViewAlign === 'right') {
|
384 | html.push(detailViewTemplate)
|
385 | }
|
386 |
|
387 | html.push('</tr>')
|
388 |
|
389 | if (html.length > 3) {
|
390 | headerHtml.push(html.join(''))
|
391 | }
|
392 | })
|
393 |
|
394 | this.$header.html(headerHtml.join(''))
|
395 | this.$header.find('th[data-field]').each((i, el) => {
|
396 | $(el).data(visibleColumns[$(el).data('field')])
|
397 | })
|
398 | this.$container.off('click', '.th-inner').on('click', '.th-inner', e => {
|
399 | const $this = $(e.currentTarget)
|
400 |
|
401 | if (this.options.detailView && !$this.parent().hasClass('bs-checkbox')) {
|
402 | if ($this.closest('.bootstrap-table')[0] !== this.$container[0]) {
|
403 | return false
|
404 | }
|
405 | }
|
406 |
|
407 | if (this.options.sortable && $this.parent().data().sortable) {
|
408 | this.onSort(e)
|
409 | }
|
410 | })
|
411 |
|
412 | const resizeEvent = Utils.getEventName('resize.bootstrap-table', this.$el.attr('id'))
|
413 |
|
414 | $(window).off(resizeEvent)
|
415 | if (!this.options.showHeader || this.options.cardView) {
|
416 | this.$header.hide()
|
417 | this.$tableHeader.hide()
|
418 | this.$tableLoading.css('top', 0)
|
419 | } else {
|
420 | this.$header.show()
|
421 | this.$tableHeader.show()
|
422 | this.$tableLoading.css('top', this.$header.outerHeight() + 1)
|
423 |
|
424 | this.getCaret()
|
425 | $(window).on(resizeEvent, () => this.resetView())
|
426 | }
|
427 |
|
428 | this.$selectAll = this.$header.find('[name="btSelectAll"]')
|
429 | this.$selectAll.off('click').on('click', e => {
|
430 | e.stopPropagation()
|
431 | const checked = $(e.currentTarget).prop('checked')
|
432 |
|
433 | this[checked ? 'checkAll' : 'uncheckAll']()
|
434 | this.updateSelected()
|
435 | })
|
436 | }
|
437 |
|
438 | initData (data, type) {
|
439 | if (type === 'append') {
|
440 | this.options.data = this.options.data.concat(data)
|
441 | } else if (type === 'prepend') {
|
442 | this.options.data = [].concat(data).concat(this.options.data)
|
443 | } else {
|
444 | data = data || Utils.deepCopy(this.options.data)
|
445 | this.options.data = Array.isArray(data) ? data : data[this.options.dataField]
|
446 | }
|
447 |
|
448 | this.data = [...this.options.data]
|
449 |
|
450 | if (this.options.sortReset) {
|
451 | this.unsortedData = [...this.data]
|
452 | }
|
453 |
|
454 | if (this.options.sidePagination === 'server') {
|
455 | return
|
456 | }
|
457 | this.initSort()
|
458 | }
|
459 |
|
460 | initSort () {
|
461 | let name = this.options.sortName
|
462 | const order = this.options.sortOrder === 'desc' ? -1 : 1
|
463 | const index = this.header.fields.indexOf(this.options.sortName)
|
464 | let timeoutId = 0
|
465 |
|
466 | if (index !== -1) {
|
467 | if (this.options.sortStable) {
|
468 | this.data.forEach((row, i) => {
|
469 | if (!row.hasOwnProperty('_position')) {
|
470 | row._position = i
|
471 | }
|
472 | })
|
473 | }
|
474 |
|
475 | if (this.options.customSort) {
|
476 | Utils.calculateObjectValue(this.options, this.options.customSort, [
|
477 | this.options.sortName,
|
478 | this.options.sortOrder,
|
479 | this.data
|
480 | ])
|
481 | } else {
|
482 | this.data.sort((a, b) => {
|
483 | if (this.header.sortNames[index]) {
|
484 | name = this.header.sortNames[index]
|
485 | }
|
486 | const aa = Utils.getItemField(a, name, this.options.escape)
|
487 | const bb = Utils.getItemField(b, name, this.options.escape)
|
488 | const value = Utils.calculateObjectValue(this.header, this.header.sorters[index], [aa, bb, a, b])
|
489 |
|
490 | if (value !== undefined) {
|
491 | if (this.options.sortStable && value === 0) {
|
492 | return order * (a._position - b._position)
|
493 | }
|
494 | return order * value
|
495 | }
|
496 |
|
497 | return Utils.sort(aa, bb, order, this.options.sortStable,
|
498 | a._position, b._position)
|
499 | })
|
500 | }
|
501 |
|
502 | if (this.options.sortClass !== undefined) {
|
503 | clearTimeout(timeoutId)
|
504 | timeoutId = setTimeout(() => {
|
505 | this.$el.removeClass(this.options.sortClass)
|
506 | const index = this.$header.find(`[data-field="${this.options.sortName}"]`).index()
|
507 |
|
508 | this.$el.find(`tr td:nth-child(${index + 1})`).addClass(this.options.sortClass)
|
509 | }, 250)
|
510 | }
|
511 | } else if (this.options.sortReset) {
|
512 | this.data = [...this.unsortedData]
|
513 | }
|
514 | }
|
515 |
|
516 | onSort ({ type, currentTarget }) {
|
517 | const $this = type === 'keypress' ? $(currentTarget) : $(currentTarget).parent()
|
518 | const $this_ = this.$header.find('th').eq($this.index())
|
519 |
|
520 | this.$header.add(this.$header_).find('span.order').remove()
|
521 |
|
522 | if (this.options.sortName === $this.data('field')) {
|
523 | const currentSortOrder = this.options.sortOrder
|
524 |
|
525 | if (currentSortOrder === undefined) {
|
526 | this.options.sortOrder = 'asc'
|
527 | } else if (currentSortOrder === 'asc') {
|
528 | this.options.sortOrder = 'desc'
|
529 | } else if (this.options.sortOrder === 'desc') {
|
530 | this.options.sortOrder = this.options.sortReset ? undefined : 'asc'
|
531 | }
|
532 |
|
533 | if (this.options.sortOrder === undefined) {
|
534 | this.options.sortName = undefined
|
535 | }
|
536 | } else {
|
537 | this.options.sortName = $this.data('field')
|
538 | if (this.options.rememberOrder) {
|
539 | this.options.sortOrder = $this.data('order') === 'asc' ? 'desc' : 'asc'
|
540 | } else {
|
541 | this.options.sortOrder = this.columns[this.fieldsColumnsIndex[$this.data('field')]].sortOrder ||
|
542 | this.columns[this.fieldsColumnsIndex[$this.data('field')]].order
|
543 | }
|
544 | }
|
545 | this.trigger('sort', this.options.sortName, this.options.sortOrder)
|
546 |
|
547 | $this.add($this_).data('order', this.options.sortOrder)
|
548 |
|
549 |
|
550 | this.getCaret()
|
551 |
|
552 | if (this.options.sidePagination === 'server' && this.options.serverSort) {
|
553 | this.options.pageNumber = 1
|
554 | this.initServer(this.options.silentSort)
|
555 | return
|
556 | }
|
557 |
|
558 | this.initSort()
|
559 | this.initBody()
|
560 | }
|
561 |
|
562 | initToolbar () {
|
563 | const opts = this.options
|
564 | let html = []
|
565 | let timeoutId = 0
|
566 | let $keepOpen
|
567 | let switchableCount = 0
|
568 |
|
569 | if (this.$toolbar.find('.bs-bars').children().length) {
|
570 | $('body').append($(opts.toolbar))
|
571 | }
|
572 | this.$toolbar.html('')
|
573 |
|
574 | if (typeof opts.toolbar === 'string' || typeof opts.toolbar === 'object') {
|
575 | $(Utils.sprintf('<div class="bs-bars %s-%s"></div>', this.constants.classes.pull, opts.toolbarAlign))
|
576 | .appendTo(this.$toolbar)
|
577 | .append($(opts.toolbar))
|
578 | }
|
579 |
|
580 |
|
581 | html = [`<div class="${[
|
582 | 'columns',
|
583 | `columns-${opts.buttonsAlign}`,
|
584 | this.constants.classes.buttonsGroup,
|
585 | `${this.constants.classes.pull}-${opts.buttonsAlign}`
|
586 | ].join(' ')}">`]
|
587 |
|
588 | if (typeof opts.buttonsOrder === 'string') {
|
589 | opts.buttonsOrder = opts.buttonsOrder.replace(/\[|\]| |'/g, '').split(',')
|
590 | }
|
591 |
|
592 | this.buttons = Object.assign(this.buttons, {
|
593 | paginationSwitch: {
|
594 | text: opts.pagination ? opts.formatPaginationSwitchUp() : opts.formatPaginationSwitchDown(),
|
595 | icon: opts.pagination ? opts.icons.paginationSwitchDown : opts.icons.paginationSwitchUp,
|
596 | render: false,
|
597 | event: this.togglePagination,
|
598 | attributes: {
|
599 | 'aria-label': opts.formatPaginationSwitch(),
|
600 | title: opts.formatPaginationSwitch()
|
601 | }
|
602 | },
|
603 | refresh: {
|
604 | text: opts.formatRefresh(),
|
605 | icon: opts.icons.refresh,
|
606 | render: false,
|
607 | event: this.refresh,
|
608 | attributes: {
|
609 | 'aria-label': opts.formatRefresh(),
|
610 | title: opts.formatRefresh()
|
611 | }
|
612 | },
|
613 | toggle: {
|
614 | text: opts.formatToggle(),
|
615 | icon: opts.icons.toggleOff,
|
616 | render: false,
|
617 | event: this.toggleView,
|
618 | attributes: {
|
619 | 'aria-label': opts.formatToggleOn(),
|
620 | title: opts.formatToggleOn()
|
621 | }
|
622 | },
|
623 | fullscreen: {
|
624 | text: opts.formatFullscreen(),
|
625 | icon: opts.icons.fullscreen,
|
626 | render: false,
|
627 | event: this.toggleFullscreen,
|
628 | attributes: {
|
629 | 'aria-label': opts.formatFullscreen(),
|
630 | title: opts.formatFullscreen()
|
631 | }
|
632 | },
|
633 | columns: {
|
634 | render: false,
|
635 | html: (() => {
|
636 | const html = []
|
637 |
|
638 | html.push(`<div class="keep-open ${this.constants.classes.buttonsDropdown}" title="${opts.formatColumns()}">
|
639 | <button class="${this.constants.buttonsClass} dropdown-toggle" type="button" ${this.constants.dataToggle}="dropdown"
|
640 | aria-label="Columns" title="${opts.formatColumns()}">
|
641 | ${opts.showButtonIcons ? Utils.sprintf(this.constants.html.icon, opts.iconsPrefix, opts.icons.columns) : ''}
|
642 | ${opts.showButtonText ? opts.formatColumns() : ''}
|
643 | ${this.constants.html.dropdownCaret}
|
644 | </button>
|
645 | ${this.constants.html.toolbarDropdown[0]}`)
|
646 |
|
647 | if (opts.showColumnsSearch) {
|
648 | html.push(
|
649 | Utils.sprintf(this.constants.html.toolbarDropdownItem,
|
650 | Utils.sprintf('<input type="text" class="%s" name="columnsSearch" placeholder="%s" autocomplete="off">', this.constants.classes.input, opts.formatSearch())
|
651 | )
|
652 | )
|
653 | html.push(this.constants.html.toolbarDropdownSeparator)
|
654 | }
|
655 |
|
656 | if (opts.showColumnsToggleAll) {
|
657 | const allFieldsVisible = this.getVisibleColumns().length === this.columns.filter(column => !this.isSelectionColumn(column)).length
|
658 |
|
659 | html.push(
|
660 | Utils.sprintf(this.constants.html.toolbarDropdownItem,
|
661 | Utils.sprintf('<input type="checkbox" class="toggle-all" %s> <span>%s</span>',
|
662 | allFieldsVisible ? 'checked="checked"' : '', opts.formatColumnsToggleAll())
|
663 | )
|
664 | )
|
665 |
|
666 | html.push(this.constants.html.toolbarDropdownSeparator)
|
667 | }
|
668 |
|
669 | let visibleColumns = 0
|
670 |
|
671 | this.columns.forEach(column => {
|
672 | if (column.visible) {
|
673 | visibleColumns++
|
674 | }
|
675 | })
|
676 |
|
677 | this.columns.forEach((column, i) => {
|
678 | if (this.isSelectionColumn(column)) {
|
679 | return
|
680 | }
|
681 |
|
682 | if (opts.cardView && !column.cardVisible) {
|
683 | return
|
684 | }
|
685 |
|
686 | const checked = column.visible ? ' checked="checked"' : ''
|
687 | const disabled = (visibleColumns <= opts.minimumCountColumns) && checked ? ' disabled="disabled"' : ''
|
688 |
|
689 | if (column.switchable) {
|
690 | html.push(Utils.sprintf(this.constants.html.toolbarDropdownItem,
|
691 | Utils.sprintf('<input type="checkbox" data-field="%s" value="%s"%s%s> <span>%s</span>',
|
692 | column.field, i, checked, disabled, column.title)))
|
693 | switchableCount++
|
694 | }
|
695 | })
|
696 | html.push(this.constants.html.toolbarDropdown[1], '</div>')
|
697 | return html.join('')
|
698 | })
|
699 | }
|
700 | })
|
701 |
|
702 | const buttonsHtml = {}
|
703 |
|
704 | for (const [buttonName, buttonConfig] of Object.entries(this.buttons)) {
|
705 | let buttonHtml
|
706 |
|
707 | if (buttonConfig.hasOwnProperty('html')) {
|
708 | if (typeof buttonConfig.html === 'function') {
|
709 | buttonHtml = buttonConfig.html()
|
710 | } else if (typeof buttonConfig.html === 'string') {
|
711 | buttonHtml = buttonConfig.html
|
712 | }
|
713 | } else {
|
714 | buttonHtml = `<button class="${this.constants.buttonsClass}" type="button" name="${buttonName}"`
|
715 |
|
716 | if (buttonConfig.hasOwnProperty('attributes')) {
|
717 | for (const [attributeName, value] of Object.entries(buttonConfig.attributes)) {
|
718 | buttonHtml += ` ${attributeName}="${value}"`
|
719 | }
|
720 | }
|
721 |
|
722 | buttonHtml += '>'
|
723 |
|
724 | if (opts.showButtonIcons && buttonConfig.hasOwnProperty('icon')) {
|
725 | buttonHtml += `${Utils.sprintf(this.constants.html.icon, opts.iconsPrefix, buttonConfig.icon) } `
|
726 | }
|
727 |
|
728 | if (opts.showButtonText && buttonConfig.hasOwnProperty('text')) {
|
729 | buttonHtml += buttonConfig.text
|
730 | }
|
731 |
|
732 | buttonHtml += '</button>'
|
733 | }
|
734 |
|
735 | buttonsHtml[buttonName] = buttonHtml
|
736 | const optionName = `show${buttonName.charAt(0).toUpperCase()}${buttonName.substring(1)}`
|
737 | const showOption = opts[optionName]
|
738 |
|
739 | if ((
|
740 | !buttonConfig.hasOwnProperty('render') ||
|
741 | buttonConfig.hasOwnProperty('render') &&
|
742 | buttonConfig.render) &&
|
743 | (showOption === undefined || showOption === true)
|
744 | ) {
|
745 | opts[optionName] = true
|
746 | }
|
747 |
|
748 | if (!opts.buttonsOrder.includes(buttonName)) {
|
749 | opts.buttonsOrder.push(buttonName)
|
750 | }
|
751 | }
|
752 |
|
753 |
|
754 | for (const button of opts.buttonsOrder) {
|
755 | const showOption = opts[`show${button.charAt(0).toUpperCase()}${button.substring(1)}`]
|
756 |
|
757 | if (showOption) {
|
758 | html.push(buttonsHtml[button])
|
759 | }
|
760 | }
|
761 |
|
762 | html.push('</div>')
|
763 |
|
764 |
|
765 | if (this.showToolbar || html.length > 2) {
|
766 | this.$toolbar.append(html.join(''))
|
767 | }
|
768 |
|
769 | for (const [buttonName, buttonConfig] of Object.entries(this.buttons)) {
|
770 | if (buttonConfig.hasOwnProperty('event')) {
|
771 | if (typeof buttonConfig.event === 'function' || typeof buttonConfig.event === 'string') {
|
772 | const event = typeof buttonConfig.event === 'string' ? window[buttonConfig.event] : buttonConfig.event
|
773 |
|
774 | this.$toolbar.find(`button[name="${buttonName}"]`)
|
775 | .off('click')
|
776 | .on('click', () => event.call(this))
|
777 | continue
|
778 | }
|
779 |
|
780 | for (const [eventType, eventFunction] of Object.entries(buttonConfig.event)) {
|
781 | const event = typeof eventFunction === 'string' ? window[eventFunction] : eventFunction
|
782 |
|
783 | this.$toolbar.find(`button[name="${buttonName}"]`)
|
784 | .off(eventType)
|
785 | .on(eventType, () => event.call(this))
|
786 | }
|
787 | }
|
788 | }
|
789 |
|
790 | if (opts.showColumns) {
|
791 | $keepOpen = this.$toolbar.find('.keep-open')
|
792 | const $checkboxes = $keepOpen.find('input[type="checkbox"]:not(".toggle-all")')
|
793 | const $toggleAll = $keepOpen.find('input[type="checkbox"].toggle-all')
|
794 |
|
795 | if (switchableCount <= opts.minimumCountColumns) {
|
796 | $keepOpen.find('input').prop('disabled', true)
|
797 | }
|
798 |
|
799 | $keepOpen.find('li, label').off('click').on('click', e => {
|
800 | e.stopImmediatePropagation()
|
801 | })
|
802 |
|
803 | $checkboxes.off('click').on('click', ({ currentTarget }) => {
|
804 | const $this = $(currentTarget)
|
805 |
|
806 | this._toggleColumn($this.val(), $this.prop('checked'), false)
|
807 | this.trigger('column-switch', $this.data('field'), $this.prop('checked'))
|
808 | $toggleAll.prop('checked', $checkboxes.filter(':checked').length === this.columns.filter(column => !this.isSelectionColumn(column)).length)
|
809 | })
|
810 |
|
811 | $toggleAll.off('click').on('click', ({ currentTarget }) => {
|
812 | this._toggleAllColumns($(currentTarget).prop('checked'))
|
813 | this.trigger('column-switch-all', $(currentTarget).prop('checked'))
|
814 | })
|
815 |
|
816 | if (opts.showColumnsSearch) {
|
817 | const $columnsSearch = $keepOpen.find('[name="columnsSearch"]')
|
818 | const $listItems = $keepOpen.find('.dropdown-item-marker')
|
819 |
|
820 | $columnsSearch.on('keyup paste change', ({ currentTarget }) => {
|
821 | const $this = $(currentTarget)
|
822 | const searchValue = $this.val().toLowerCase()
|
823 |
|
824 | $listItems.show()
|
825 | $checkboxes.each((i, el) => {
|
826 | const $checkbox = $(el)
|
827 | const $listItem = $checkbox.parents('.dropdown-item-marker')
|
828 | const text = $listItem.text().toLowerCase()
|
829 |
|
830 | if (!text.includes(searchValue)) {
|
831 | $listItem.hide()
|
832 | }
|
833 | })
|
834 | })
|
835 | }
|
836 | }
|
837 | const handleInputEvent = $searchInput => {
|
838 | const eventTriggers = 'keyup drop blur mouseup'
|
839 |
|
840 | $searchInput.off(eventTriggers).on(eventTriggers, event => {
|
841 | if (opts.searchOnEnterKey && event.keyCode !== 13) {
|
842 | return
|
843 | }
|
844 |
|
845 | if ([37, 38, 39, 40].includes(event.keyCode)) {
|
846 | return
|
847 | }
|
848 |
|
849 | clearTimeout(timeoutId)
|
850 | timeoutId = setTimeout(() => {
|
851 | this.onSearch({ currentTarget: event.currentTarget })
|
852 | }, opts.searchTimeOut)
|
853 | })
|
854 | }
|
855 |
|
856 |
|
857 | if (
|
858 | (opts.search || this.showSearchClearButton) &&
|
859 | typeof opts.searchSelector !== 'string'
|
860 | ) {
|
861 | html = []
|
862 | const showSearchButton = Utils.sprintf(this.constants.html.searchButton,
|
863 | this.constants.buttonsClass,
|
864 | opts.formatSearch(),
|
865 | opts.showButtonIcons ? Utils.sprintf(this.constants.html.icon, opts.iconsPrefix, opts.icons.search) : '',
|
866 | opts.showButtonText ? opts.formatSearch() : ''
|
867 | )
|
868 | const showSearchClearButton = Utils.sprintf(this.constants.html.searchClearButton,
|
869 | this.constants.buttonsClass,
|
870 | opts.formatClearSearch(),
|
871 | opts.showButtonIcons ? Utils.sprintf(this.constants.html.icon, opts.iconsPrefix, opts.icons.clearSearch) : '',
|
872 | opts.showButtonText ? opts.formatClearSearch() : ''
|
873 | )
|
874 | const searchInputHtml = `<input class="${this.constants.classes.input}
|
875 | ${Utils.sprintf(' %s%s', this.constants.classes.inputPrefix, opts.iconSize)}
|
876 | search-input" type="search" placeholder="${opts.formatSearch()}" autocomplete="off">`
|
877 | let searchInputFinalHtml = searchInputHtml
|
878 |
|
879 | if (opts.showSearchButton || opts.showSearchClearButton) {
|
880 | const buttonsHtml = (opts.showSearchButton ? showSearchButton : '') +
|
881 | (opts.showSearchClearButton ? showSearchClearButton : '')
|
882 |
|
883 | searchInputFinalHtml = opts.search ? Utils.sprintf(this.constants.html.inputGroup,
|
884 | searchInputHtml, buttonsHtml) : buttonsHtml
|
885 | }
|
886 |
|
887 | html.push(Utils.sprintf(`
|
888 | <div class="${this.constants.classes.pull}-${opts.searchAlign} search ${this.constants.classes.inputGroup}">
|
889 | %s
|
890 | </div>
|
891 | `, searchInputFinalHtml))
|
892 |
|
893 | this.$toolbar.append(html.join(''))
|
894 | const $searchInput = Utils.getSearchInput(this)
|
895 |
|
896 | if (opts.showSearchButton) {
|
897 | this.$toolbar.find('.search button[name=search]').off('click').on('click', () => {
|
898 | clearTimeout(timeoutId)
|
899 | timeoutId = setTimeout(() => {
|
900 | this.onSearch({ currentTarget: $searchInput })
|
901 | }, opts.searchTimeOut)
|
902 | })
|
903 |
|
904 | if (opts.searchOnEnterKey) {
|
905 | handleInputEvent($searchInput)
|
906 | }
|
907 | } else {
|
908 | handleInputEvent($searchInput)
|
909 | }
|
910 |
|
911 | if (opts.showSearchClearButton) {
|
912 | this.$toolbar.find('.search button[name=clearSearch]').click(() => {
|
913 | this.resetSearch()
|
914 | })
|
915 | }
|
916 | } else if (typeof opts.searchSelector === 'string') {
|
917 | const $searchInput = Utils.getSearchInput(this)
|
918 |
|
919 | handleInputEvent($searchInput)
|
920 | }
|
921 | }
|
922 |
|
923 | onSearch ({ currentTarget, firedByInitSearchText } = {}, overwriteSearchText = true) {
|
924 | if (currentTarget !== undefined && $(currentTarget).length && overwriteSearchText) {
|
925 | const text = $(currentTarget).val().trim()
|
926 |
|
927 | if (this.options.trimOnSearch && $(currentTarget).val() !== text) {
|
928 | $(currentTarget).val(text)
|
929 | }
|
930 |
|
931 | if (this.searchText === text) {
|
932 | return
|
933 | }
|
934 |
|
935 | const $searchInput = Utils.getSearchInput(this)
|
936 | const $currentTarget = currentTarget instanceof jQuery ? currentTarget : $(currentTarget)
|
937 |
|
938 | if ($currentTarget.is($searchInput) || $currentTarget.hasClass('search-input')) {
|
939 | this.searchText = text
|
940 | this.options.searchText = text
|
941 | }
|
942 | }
|
943 |
|
944 | if (!firedByInitSearchText && !this.options.cookie) {
|
945 | this.options.pageNumber = 1
|
946 | }
|
947 | this.initSearch()
|
948 | if (firedByInitSearchText) {
|
949 | if (this.options.sidePagination === 'client') {
|
950 | this.updatePagination()
|
951 | }
|
952 | } else {
|
953 | this.updatePagination()
|
954 | }
|
955 | this.trigger('search', this.searchText)
|
956 | }
|
957 |
|
958 | initSearch () {
|
959 | this.filterOptions = this.filterOptions || this.options.filterOptions
|
960 | if (this.options.sidePagination !== 'server') {
|
961 | if (this.options.customSearch) {
|
962 | this.data = Utils.calculateObjectValue(this.options, this.options.customSearch,
|
963 | [this.options.data, this.searchText, this.filterColumns])
|
964 |
|
965 | if (this.options.sortReset) {
|
966 | this.unsortedData = [...this.data]
|
967 | }
|
968 | return
|
969 | }
|
970 |
|
971 | const rawSearchText = this.searchText && (this.fromHtml ? Utils.escapeHTML(this.searchText) : this.searchText)
|
972 | let searchText = rawSearchText ? rawSearchText.toLowerCase() : ''
|
973 | const f = Utils.isEmptyObject(this.filterColumns) ? null : this.filterColumns
|
974 |
|
975 | if (this.options.searchAccentNeutralise) {
|
976 | searchText = Utils.normalizeAccent(searchText)
|
977 | }
|
978 |
|
979 |
|
980 | if (typeof this.filterOptions.filterAlgorithm === 'function') {
|
981 | this.data = this.options.data.filter(item => this.filterOptions.filterAlgorithm.apply(null, [item, f]))
|
982 | } else if (typeof this.filterOptions.filterAlgorithm === 'string') {
|
983 | this.data = f ? this.options.data.filter(item => {
|
984 | const filterAlgorithm = this.filterOptions.filterAlgorithm
|
985 |
|
986 | if (filterAlgorithm === 'and') {
|
987 | for (const key in f) {
|
988 | if (
|
989 | (Array.isArray(f[key]) &&
|
990 | !f[key].includes(item[key])) ||
|
991 | (!Array.isArray(f[key]) &&
|
992 | item[key] !== f[key])
|
993 | ) {
|
994 | return false
|
995 | }
|
996 | }
|
997 | } else if (filterAlgorithm === 'or') {
|
998 | let match = false
|
999 |
|
1000 | for (const key in f) {
|
1001 | if (
|
1002 | (Array.isArray(f[key]) &&
|
1003 | f[key].includes(item[key])) ||
|
1004 | (!Array.isArray(f[key]) &&
|
1005 | item[key] === f[key])
|
1006 | ) {
|
1007 | match = true
|
1008 | }
|
1009 | }
|
1010 |
|
1011 | return match
|
1012 | }
|
1013 |
|
1014 | return true
|
1015 | }) : [...this.options.data]
|
1016 | }
|
1017 |
|
1018 | const visibleFields = this.getVisibleFields()
|
1019 |
|
1020 | this.data = searchText ? this.data.filter((item, i) => {
|
1021 | for (let j = 0; j < this.header.fields.length; j++) {
|
1022 | if (!this.header.searchables[j] || (this.options.visibleSearch && visibleFields.indexOf(this.header.fields[j]) === -1)) {
|
1023 | continue
|
1024 | }
|
1025 |
|
1026 | const key = Utils.isNumeric(this.header.fields[j]) ? parseInt(this.header.fields[j], 10) : this.header.fields[j]
|
1027 | const column = this.columns[this.fieldsColumnsIndex[key]]
|
1028 | let value
|
1029 |
|
1030 | if (typeof key === 'string') {
|
1031 | value = item
|
1032 | const props = key.split('.')
|
1033 |
|
1034 | for (let i = 0; i < props.length; i++) {
|
1035 | if (value[props[i]] !== null) {
|
1036 | value = value[props[i]]
|
1037 | }
|
1038 | }
|
1039 | } else {
|
1040 | value = item[key]
|
1041 | }
|
1042 |
|
1043 | if (this.options.searchAccentNeutralise) {
|
1044 | value = Utils.normalizeAccent(value)
|
1045 | }
|
1046 |
|
1047 |
|
1048 | if (column && column.searchFormatter) {
|
1049 | value = Utils.calculateObjectValue(column,
|
1050 | this.header.formatters[j], [value, item, i, column.field], value)
|
1051 | }
|
1052 |
|
1053 | if (typeof value === 'string' || typeof value === 'number') {
|
1054 | if (
|
1055 | this.options.strictSearch && (`${value}`).toLowerCase() === searchText ||
|
1056 | (this.options.regexSearch && Utils.regexCompare(value, rawSearchText))
|
1057 | ) {
|
1058 | return true
|
1059 | }
|
1060 |
|
1061 | const largerSmallerEqualsRegex = /(?:(<=|=>|=<|>=|>|<)(?:\s+)?(-?\d+)?|(-?\d+)?(\s+)?(<=|=>|=<|>=|>|<))/gm
|
1062 | const matches = largerSmallerEqualsRegex.exec(this.searchText)
|
1063 | let comparisonCheck = false
|
1064 |
|
1065 | if (matches) {
|
1066 | const operator = matches[1] || `${matches[5]}l`
|
1067 | const comparisonValue = matches[2] || matches[3]
|
1068 | const int = parseInt(value, 10)
|
1069 | const comparisonInt = parseInt(comparisonValue, 10)
|
1070 |
|
1071 | switch (operator) {
|
1072 | case '>':
|
1073 | case '<l':
|
1074 | comparisonCheck = int > comparisonInt
|
1075 | break
|
1076 | case '<':
|
1077 | case '>l':
|
1078 | comparisonCheck = int < comparisonInt
|
1079 | break
|
1080 | case '<=':
|
1081 | case '=<':
|
1082 | case '>=l':
|
1083 | case '=>l':
|
1084 | comparisonCheck = int <= comparisonInt
|
1085 | break
|
1086 | case '>=':
|
1087 | case '=>':
|
1088 | case '<=l':
|
1089 | case '=<l':
|
1090 | comparisonCheck = int >= comparisonInt
|
1091 | break
|
1092 | default:
|
1093 | break
|
1094 | }
|
1095 | }
|
1096 |
|
1097 | if (comparisonCheck || (`${value}`).toLowerCase().includes(searchText)) {
|
1098 | return true
|
1099 | }
|
1100 | }
|
1101 | }
|
1102 | return false
|
1103 | }) : this.data
|
1104 |
|
1105 | if (this.options.sortReset) {
|
1106 | this.unsortedData = [...this.data]
|
1107 | }
|
1108 |
|
1109 | this.initSort()
|
1110 | }
|
1111 | }
|
1112 |
|
1113 | initPagination () {
|
1114 | const opts = this.options
|
1115 |
|
1116 | if (!opts.pagination) {
|
1117 | this.$pagination.hide()
|
1118 | return
|
1119 | }
|
1120 | this.$pagination.show()
|
1121 |
|
1122 | const html = []
|
1123 | let allSelected = false
|
1124 | let i
|
1125 | let from
|
1126 | let to
|
1127 | let $pageList
|
1128 | let $pre
|
1129 | let $next
|
1130 | let $number
|
1131 | const data = this.getData({ includeHiddenRows: false })
|
1132 | let pageList = opts.pageList
|
1133 |
|
1134 | if (typeof pageList === 'string') {
|
1135 | pageList = pageList.replace(/\[|\]| /g, '').toLowerCase().split(',')
|
1136 | }
|
1137 |
|
1138 | pageList = pageList.map(value => {
|
1139 | if (typeof value === 'string') {
|
1140 | return (value.toLowerCase() === opts.formatAllRows().toLowerCase() ||
|
1141 | ['all', 'unlimited'].includes(value.toLowerCase())) ? opts.formatAllRows() : +value
|
1142 | }
|
1143 | return value
|
1144 | })
|
1145 |
|
1146 | this.paginationParts = opts.paginationParts
|
1147 | if (typeof this.paginationParts === 'string') {
|
1148 | this.paginationParts = this.paginationParts.replace(/\[|\]| |'/g, '').split(',')
|
1149 | }
|
1150 |
|
1151 | if (opts.sidePagination !== 'server') {
|
1152 | opts.totalRows = data.length
|
1153 | }
|
1154 |
|
1155 | this.totalPages = 0
|
1156 | if (opts.totalRows) {
|
1157 | if (opts.pageSize === opts.formatAllRows()) {
|
1158 | opts.pageSize = opts.totalRows
|
1159 | allSelected = true
|
1160 | }
|
1161 |
|
1162 | this.totalPages = ~~((opts.totalRows - 1) / opts.pageSize) + 1
|
1163 |
|
1164 | opts.totalPages = this.totalPages
|
1165 | }
|
1166 | if (this.totalPages > 0 && opts.pageNumber > this.totalPages) {
|
1167 | opts.pageNumber = this.totalPages
|
1168 | }
|
1169 |
|
1170 | this.pageFrom = (opts.pageNumber - 1) * opts.pageSize + 1
|
1171 | this.pageTo = opts.pageNumber * opts.pageSize
|
1172 | if (this.pageTo > opts.totalRows) {
|
1173 | this.pageTo = opts.totalRows
|
1174 | }
|
1175 |
|
1176 | if (this.options.pagination && this.options.sidePagination !== 'server') {
|
1177 | this.options.totalNotFiltered = this.options.data.length
|
1178 | }
|
1179 |
|
1180 | if (!this.options.showExtendedPagination) {
|
1181 | this.options.totalNotFiltered = undefined
|
1182 | }
|
1183 |
|
1184 | if (this.paginationParts.includes('pageInfo') || this.paginationParts.includes('pageInfoShort') || this.paginationParts.includes('pageSize')) {
|
1185 | html.push(`<div class="${this.constants.classes.pull}-${opts.paginationDetailHAlign} pagination-detail">`)
|
1186 | }
|
1187 |
|
1188 | if (this.paginationParts.includes('pageInfo') || this.paginationParts.includes('pageInfoShort')) {
|
1189 | const paginationInfo = this.paginationParts.includes('pageInfoShort') ? opts.formatDetailPagination(opts.totalRows) : opts.formatShowingRows(this.pageFrom, this.pageTo, opts.totalRows, opts.totalNotFiltered)
|
1190 |
|
1191 | html.push(`<span class="pagination-info">
|
1192 | ${paginationInfo}
|
1193 | </span>`)
|
1194 | }
|
1195 |
|
1196 | if (this.paginationParts.includes('pageSize')) {
|
1197 | html.push('<div class="page-list">')
|
1198 |
|
1199 | const pageNumber = [
|
1200 | `<div class="${this.constants.classes.paginationDropdown}">
|
1201 | <button class="${this.constants.buttonsClass} dropdown-toggle" type="button" ${this.constants.dataToggle}="dropdown">
|
1202 | <span class="page-size">
|
1203 | ${allSelected ? opts.formatAllRows() : opts.pageSize}
|
1204 | </span>
|
1205 | ${this.constants.html.dropdownCaret}
|
1206 | </button>
|
1207 | ${this.constants.html.pageDropdown[0]}`
|
1208 | ]
|
1209 |
|
1210 | pageList.forEach((page, i) => {
|
1211 | if (!opts.smartDisplay || i === 0 || pageList[i - 1] < opts.totalRows || page === opts.formatAllRows()) {
|
1212 | let active
|
1213 |
|
1214 | if (allSelected) {
|
1215 | active = page === opts.formatAllRows() ? this.constants.classes.dropdownActive : ''
|
1216 | } else {
|
1217 | active = page === opts.pageSize ? this.constants.classes.dropdownActive : ''
|
1218 | }
|
1219 | pageNumber.push(Utils.sprintf(this.constants.html.pageDropdownItem, active, page))
|
1220 | }
|
1221 | })
|
1222 | pageNumber.push(`${this.constants.html.pageDropdown[1]}</div>`)
|
1223 |
|
1224 | html.push(opts.formatRecordsPerPage(pageNumber.join('')))
|
1225 | }
|
1226 |
|
1227 | if (this.paginationParts.includes('pageInfo') || this.paginationParts.includes('pageInfoShort') || this.paginationParts.includes('pageSize')) {
|
1228 | html.push('</div></div>')
|
1229 | }
|
1230 |
|
1231 | if (this.paginationParts.includes('pageList')) {
|
1232 | html.push(`<div class="${this.constants.classes.pull}-${opts.paginationHAlign} pagination">`,
|
1233 | Utils.sprintf(this.constants.html.pagination[0], Utils.sprintf(' pagination-%s', opts.iconSize)),
|
1234 | Utils.sprintf(this.constants.html.paginationItem, ' page-pre', opts.formatSRPaginationPreText(), opts.paginationPreText))
|
1235 |
|
1236 | if (this.totalPages < opts.paginationSuccessivelySize) {
|
1237 | from = 1
|
1238 | to = this.totalPages
|
1239 | } else {
|
1240 | from = opts.pageNumber - opts.paginationPagesBySide
|
1241 | to = from + (opts.paginationPagesBySide * 2)
|
1242 | }
|
1243 |
|
1244 | if (opts.pageNumber < (opts.paginationSuccessivelySize - 1)) {
|
1245 | to = opts.paginationSuccessivelySize
|
1246 | }
|
1247 |
|
1248 | if (opts.paginationSuccessivelySize > this.totalPages - from) {
|
1249 | from = from - (opts.paginationSuccessivelySize - (this.totalPages - from)) + 1
|
1250 | }
|
1251 |
|
1252 | if (from < 1) {
|
1253 | from = 1
|
1254 | }
|
1255 |
|
1256 | if (to > this.totalPages) {
|
1257 | to = this.totalPages
|
1258 | }
|
1259 |
|
1260 | const middleSize = Math.round(opts.paginationPagesBySide / 2)
|
1261 | const pageItem = (i, classes = '') => Utils.sprintf(this.constants.html.paginationItem,
|
1262 | classes + (i === opts.pageNumber ? ` ${this.constants.classes.paginationActive}` : ''), opts.formatSRPaginationPageText(i), i)
|
1263 |
|
1264 | if (from > 1) {
|
1265 | let max = opts.paginationPagesBySide
|
1266 |
|
1267 | if (max >= from) max = from - 1
|
1268 | for (i = 1; i <= max; i++) {
|
1269 | html.push(pageItem(i))
|
1270 | }
|
1271 | if ((from - 1) === max + 1) {
|
1272 | i = from - 1
|
1273 | html.push(pageItem(i))
|
1274 | } else if ((from - 1) > max) {
|
1275 | if (
|
1276 | (from - opts.paginationPagesBySide * 2) > opts.paginationPagesBySide &&
|
1277 | opts.paginationUseIntermediate
|
1278 | ) {
|
1279 | i = Math.round(((from - middleSize) / 2) + middleSize)
|
1280 | html.push(pageItem(i, ' page-intermediate'))
|
1281 | } else {
|
1282 | html.push(Utils.sprintf(this.constants.html.paginationItem,
|
1283 | ' page-first-separator disabled', '', '...'))
|
1284 | }
|
1285 | }
|
1286 | }
|
1287 |
|
1288 | for (i = from; i <= to; i++) {
|
1289 | html.push(pageItem(i))
|
1290 | }
|
1291 |
|
1292 | if (this.totalPages > to) {
|
1293 | let min = this.totalPages - (opts.paginationPagesBySide - 1)
|
1294 |
|
1295 | if (to >= min) min = to + 1
|
1296 | if ((to + 1) === min - 1) {
|
1297 | i = to + 1
|
1298 | html.push(pageItem(i))
|
1299 | } else if (min > (to + 1)) {
|
1300 | if (
|
1301 | (this.totalPages - to) > opts.paginationPagesBySide * 2 &&
|
1302 | opts.paginationUseIntermediate
|
1303 | ) {
|
1304 | i = Math.round(((this.totalPages - middleSize - to) / 2) + to)
|
1305 | html.push(pageItem(i, ' page-intermediate'))
|
1306 | } else {
|
1307 | html.push(Utils.sprintf(this.constants.html.paginationItem,
|
1308 | ' page-last-separator disabled', '', '...'))
|
1309 | }
|
1310 | }
|
1311 |
|
1312 | for (i = min; i <= this.totalPages; i++) {
|
1313 | html.push(pageItem(i))
|
1314 | }
|
1315 | }
|
1316 |
|
1317 | html.push(Utils.sprintf(this.constants.html.paginationItem, ' page-next', opts.formatSRPaginationNextText(), opts.paginationNextText))
|
1318 | html.push(this.constants.html.pagination[1], '</div>')
|
1319 | }
|
1320 | this.$pagination.html(html.join(''))
|
1321 |
|
1322 | const dropupClass = ['bottom', 'both'].includes(opts.paginationVAlign) ?
|
1323 | ` ${this.constants.classes.dropup}` : ''
|
1324 |
|
1325 | this.$pagination.last().find('.page-list > div').addClass(dropupClass)
|
1326 |
|
1327 | if (!opts.onlyInfoPagination) {
|
1328 | $pageList = this.$pagination.find('.page-list a')
|
1329 | $pre = this.$pagination.find('.page-pre')
|
1330 | $next = this.$pagination.find('.page-next')
|
1331 | $number = this.$pagination.find('.page-item').not('.page-next, .page-pre, .page-last-separator, .page-first-separator')
|
1332 |
|
1333 | if (this.totalPages <= 1) {
|
1334 | this.$pagination.find('div.pagination').hide()
|
1335 | }
|
1336 |
|
1337 | if (opts.smartDisplay) {
|
1338 | if (pageList.length < 2 || opts.totalRows <= pageList[0]) {
|
1339 | this.$pagination.find('div.page-list').hide()
|
1340 | }
|
1341 | }
|
1342 |
|
1343 |
|
1344 | this.$pagination[this.getData().length ? 'show' : 'hide']()
|
1345 |
|
1346 | if (!opts.paginationLoop) {
|
1347 | if (opts.pageNumber === 1) {
|
1348 | $pre.addClass('disabled')
|
1349 | }
|
1350 | if (opts.pageNumber === this.totalPages) {
|
1351 | $next.addClass('disabled')
|
1352 | }
|
1353 | }
|
1354 |
|
1355 | if (allSelected) {
|
1356 | opts.pageSize = opts.formatAllRows()
|
1357 | }
|
1358 |
|
1359 | $pageList.off('click').on('click', e => this.onPageListChange(e))
|
1360 | $pre.off('click').on('click', e => this.onPagePre(e))
|
1361 | $next.off('click').on('click', e => this.onPageNext(e))
|
1362 | $number.off('click').on('click', e => this.onPageNumber(e))
|
1363 | }
|
1364 | }
|
1365 |
|
1366 | updatePagination (event) {
|
1367 |
|
1368 | if (event && $(event.currentTarget).hasClass('disabled')) {
|
1369 | return
|
1370 | }
|
1371 |
|
1372 | if (!this.options.maintainMetaData) {
|
1373 | this.resetRows()
|
1374 | }
|
1375 |
|
1376 | this.initPagination()
|
1377 |
|
1378 | this.trigger('page-change', this.options.pageNumber, this.options.pageSize)
|
1379 |
|
1380 | if (this.options.sidePagination === 'server') {
|
1381 | this.initServer()
|
1382 | } else {
|
1383 | this.initBody()
|
1384 | }
|
1385 | }
|
1386 |
|
1387 | onPageListChange (event) {
|
1388 | event.preventDefault()
|
1389 | const $this = $(event.currentTarget)
|
1390 |
|
1391 | $this.parent().addClass(this.constants.classes.dropdownActive)
|
1392 | .siblings().removeClass(this.constants.classes.dropdownActive)
|
1393 | this.options.pageSize = $this.text().toUpperCase() === this.options.formatAllRows().toUpperCase() ?
|
1394 | this.options.formatAllRows() : +$this.text()
|
1395 | this.$toolbar.find('.page-size').text(this.options.pageSize)
|
1396 |
|
1397 | this.updatePagination(event)
|
1398 | return false
|
1399 | }
|
1400 |
|
1401 | onPagePre (event) {
|
1402 | if ($(event.target).hasClass('disabled')) {
|
1403 | return
|
1404 | }
|
1405 | event.preventDefault()
|
1406 | if ((this.options.pageNumber - 1) === 0) {
|
1407 | this.options.pageNumber = this.options.totalPages
|
1408 | } else {
|
1409 | this.options.pageNumber--
|
1410 | }
|
1411 | this.updatePagination(event)
|
1412 | return false
|
1413 | }
|
1414 |
|
1415 | onPageNext (event) {
|
1416 | if ($(event.target).hasClass('disabled')) {
|
1417 | return
|
1418 | }
|
1419 | event.preventDefault()
|
1420 | if ((this.options.pageNumber + 1) > this.options.totalPages) {
|
1421 | this.options.pageNumber = 1
|
1422 | } else {
|
1423 | this.options.pageNumber++
|
1424 | }
|
1425 | this.updatePagination(event)
|
1426 | return false
|
1427 | }
|
1428 |
|
1429 | onPageNumber (event) {
|
1430 | event.preventDefault()
|
1431 | if (this.options.pageNumber === +$(event.currentTarget).text()) {
|
1432 | return
|
1433 | }
|
1434 | this.options.pageNumber = +$(event.currentTarget).text()
|
1435 | this.updatePagination(event)
|
1436 | return false
|
1437 | }
|
1438 |
|
1439 |
|
1440 | initRow (item, i, data, trFragments) {
|
1441 | const html = []
|
1442 | let style = {}
|
1443 | const csses = []
|
1444 | let data_ = ''
|
1445 | let attributes = {}
|
1446 | const htmlAttributes = []
|
1447 |
|
1448 | if (Utils.findIndex(this.hiddenRows, item) > -1) {
|
1449 | return
|
1450 | }
|
1451 |
|
1452 | style = Utils.calculateObjectValue(this.options, this.options.rowStyle, [item, i], style)
|
1453 |
|
1454 | if (style && style.css) {
|
1455 | for (const [key, value] of Object.entries(style.css)) {
|
1456 | csses.push(`${key}: ${value}`)
|
1457 | }
|
1458 | }
|
1459 |
|
1460 | attributes = Utils.calculateObjectValue(this.options,
|
1461 | this.options.rowAttributes, [item, i], attributes)
|
1462 |
|
1463 | if (attributes) {
|
1464 | for (const [key, value] of Object.entries(attributes)) {
|
1465 | htmlAttributes.push(`${key}="${Utils.escapeHTML(value)}"`)
|
1466 | }
|
1467 | }
|
1468 |
|
1469 | if (item._data && !Utils.isEmptyObject(item._data)) {
|
1470 | for (const [k, v] of Object.entries(item._data)) {
|
1471 |
|
1472 | if (k === 'index') {
|
1473 | return
|
1474 | }
|
1475 | data_ += ` data-${k}='${typeof v === 'object' ? JSON.stringify(v) : v}'`
|
1476 | }
|
1477 | }
|
1478 |
|
1479 | html.push('<tr',
|
1480 | Utils.sprintf(' %s', htmlAttributes.length ? htmlAttributes.join(' ') : undefined),
|
1481 | Utils.sprintf(' id="%s"', Array.isArray(item) ? undefined : item._id),
|
1482 | Utils.sprintf(' class="%s"', style.classes || (Array.isArray(item) ? undefined : item._class)),
|
1483 | Utils.sprintf(' style="%s"', Array.isArray(item) ? undefined : item._style),
|
1484 | ` data-index="${i}"`,
|
1485 | Utils.sprintf(' data-uniqueid="%s"', Utils.getItemField(item, this.options.uniqueId, false)),
|
1486 | Utils.sprintf(' data-has-detail-view="%s"', (this.options.detailView && Utils.calculateObjectValue(null, this.options.detailFilter, [i, item])) ? 'true' : undefined),
|
1487 | Utils.sprintf('%s', data_),
|
1488 | '>'
|
1489 | )
|
1490 |
|
1491 | if (this.options.cardView) {
|
1492 | html.push(`<td colspan="${this.header.fields.length}"><div class="card-views">`)
|
1493 | }
|
1494 |
|
1495 | let detailViewTemplate = ''
|
1496 |
|
1497 | if (Utils.hasDetailViewIcon(this.options)) {
|
1498 | detailViewTemplate = '<td>'
|
1499 |
|
1500 | if (Utils.calculateObjectValue(null, this.options.detailFilter, [i, item])) {
|
1501 | detailViewTemplate += `
|
1502 | <a class="detail-icon" href="#">
|
1503 | ${Utils.sprintf(this.constants.html.icon, this.options.iconsPrefix, this.options.icons.detailOpen)}
|
1504 | </a>
|
1505 | `
|
1506 | }
|
1507 |
|
1508 | detailViewTemplate += '</td>'
|
1509 | }
|
1510 |
|
1511 | if (detailViewTemplate && this.options.detailViewAlign !== 'right') {
|
1512 | html.push(detailViewTemplate)
|
1513 | }
|
1514 |
|
1515 | this.header.fields.forEach((field, j) => {
|
1516 | const column = this.columns[j]
|
1517 | let text = ''
|
1518 | let value_ = Utils.getItemField(item, field, this.options.escape, column.escape)
|
1519 | let value = ''
|
1520 | let type = ''
|
1521 | let cellStyle = {}
|
1522 | let id_ = ''
|
1523 | let class_ = this.header.classes[j]
|
1524 | let style_ = ''
|
1525 | let styleToAdd_ = ''
|
1526 | let data_ = ''
|
1527 | let rowspan_ = ''
|
1528 | let colspan_ = ''
|
1529 | let title_ = ''
|
1530 |
|
1531 | if ((this.fromHtml || this.autoMergeCells) && typeof value_ === 'undefined') {
|
1532 | if ((!column.checkbox) && (!column.radio)) {
|
1533 | return
|
1534 | }
|
1535 | }
|
1536 |
|
1537 | if (!column.visible) {
|
1538 | return
|
1539 | }
|
1540 |
|
1541 | if (this.options.cardView && (!column.cardVisible)) {
|
1542 | return
|
1543 | }
|
1544 |
|
1545 | if (column.escape) {
|
1546 | value_ = Utils.escapeHTML(value_)
|
1547 | }
|
1548 |
|
1549 |
|
1550 | if (csses.concat([this.header.styles[j]]).length) {
|
1551 | styleToAdd_ += `${csses.concat([this.header.styles[j]]).join('; ')}`
|
1552 | }
|
1553 | if (item[`_${field}_style`]) {
|
1554 | styleToAdd_ += `${item[`_${field}_style`]}`
|
1555 | }
|
1556 |
|
1557 | if (styleToAdd_) {
|
1558 | style_ = ` style="${styleToAdd_}"`
|
1559 | }
|
1560 |
|
1561 |
|
1562 |
|
1563 | if (item[`_${field}_id`]) {
|
1564 | id_ = Utils.sprintf(' id="%s"', item[`_${field}_id`])
|
1565 | }
|
1566 | if (item[`_${field}_class`]) {
|
1567 | class_ = Utils.sprintf(' class="%s"', item[`_${field}_class`])
|
1568 | }
|
1569 | if (item[`_${field}_rowspan`]) {
|
1570 | rowspan_ = Utils.sprintf(' rowspan="%s"', item[`_${field}_rowspan`])
|
1571 | }
|
1572 | if (item[`_${field}_colspan`]) {
|
1573 | colspan_ = Utils.sprintf(' colspan="%s"', item[`_${field}_colspan`])
|
1574 | }
|
1575 | if (item[`_${field}_title`]) {
|
1576 | title_ = Utils.sprintf(' title="%s"', item[`_${field}_title`])
|
1577 | }
|
1578 | cellStyle = Utils.calculateObjectValue(this.header,
|
1579 | this.header.cellStyles[j], [value_, item, i, field], cellStyle)
|
1580 | if (cellStyle.classes) {
|
1581 | class_ = ` class="${cellStyle.classes}"`
|
1582 | }
|
1583 | if (cellStyle.css) {
|
1584 | const csses_ = []
|
1585 |
|
1586 | for (const [key, value] of Object.entries(cellStyle.css)) {
|
1587 | csses_.push(`${key}: ${value}`)
|
1588 | }
|
1589 | style_ = ` style="${csses_.concat(this.header.styles[j]).join('; ')}"`
|
1590 | }
|
1591 |
|
1592 | value = Utils.calculateObjectValue(column,
|
1593 | this.header.formatters[j], [value_, item, i, field], value_)
|
1594 |
|
1595 | if (!(column.checkbox || column.radio)) {
|
1596 | value = typeof value === 'undefined' || value === null ?
|
1597 | this.options.undefinedText : value
|
1598 | }
|
1599 |
|
1600 | if (column.searchable && this.searchText && this.options.searchHighlight && !(column.checkbox || column.radio)) {
|
1601 | let defValue = ''
|
1602 | const regExp = new RegExp(`(${ this.searchText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') })`, 'gim')
|
1603 | const marker = '<mark>$1</mark>'
|
1604 | const isHTML = value && /<(?=.*? .*?\/ ?>|br|hr|input|!--|wbr)[a-z]+.*?>|<([a-z]+).*?<\/\1>/i.test(value)
|
1605 |
|
1606 | if (isHTML) {
|
1607 |
|
1608 | let textContent = new DOMParser().parseFromString(value.toString(), 'text/html').documentElement.textContent
|
1609 | const textReplaced = textContent.replace(regExp, marker)
|
1610 |
|
1611 | textContent = textContent.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
1612 | defValue = value.replace(new RegExp(`(>\\s*)(${textContent})(\\s*)`, 'gm'), `$1${textReplaced}$3`)
|
1613 | } else {
|
1614 |
|
1615 | defValue = value.toString().replace(regExp, marker)
|
1616 | }
|
1617 | value = Utils.calculateObjectValue(column, column.searchHighlightFormatter, [value, this.searchText], defValue)
|
1618 | }
|
1619 |
|
1620 | if (item[`_${field}_data`] && !Utils.isEmptyObject(item[`_${field}_data`])) {
|
1621 | for (const [k, v] of Object.entries(item[`_${field}_data`])) {
|
1622 |
|
1623 | if (k === 'index') {
|
1624 | return
|
1625 | }
|
1626 | data_ += ` data-${k}="${v}"`
|
1627 | }
|
1628 | }
|
1629 |
|
1630 | if (column.checkbox || column.radio) {
|
1631 | type = column.checkbox ? 'checkbox' : type
|
1632 | type = column.radio ? 'radio' : type
|
1633 |
|
1634 | const c = column['class'] || ''
|
1635 | const isChecked = Utils.isObject(value) && value.hasOwnProperty('checked') ?
|
1636 | value.checked : (value === true || value_) && value !== false
|
1637 | const isDisabled = !column.checkboxEnabled || (value && value.disabled)
|
1638 |
|
1639 | text = [
|
1640 | this.options.cardView ?
|
1641 | `<div class="card-view ${c}">` :
|
1642 | `<td class="bs-checkbox ${c}"${class_}${style_}>`,
|
1643 | `<label>
|
1644 | <input
|
1645 | data-index="${i}"
|
1646 | name="${this.options.selectItemName}"
|
1647 | type="${type}"
|
1648 | ${Utils.sprintf('value="%s"', item[this.options.idField])}
|
1649 | ${Utils.sprintf('checked="%s"', isChecked ? 'checked' : undefined)}
|
1650 | ${Utils.sprintf('disabled="%s"', isDisabled ? 'disabled' : undefined)} />
|
1651 | <span></span>
|
1652 | </label>`,
|
1653 | this.header.formatters[j] && typeof value === 'string' ? value : '',
|
1654 | this.options.cardView ? '</div>' : '</td>'
|
1655 | ].join('')
|
1656 |
|
1657 | item[this.header.stateField] = value === true || (!!value_ || (value && value.checked))
|
1658 | } else if (this.options.cardView) {
|
1659 | const cardTitle = this.options.showHeader ?
|
1660 | `<span class="card-view-title ${cellStyle.classes || ''}"${style_}>${Utils.getFieldTitle(this.columns, field)}</span>` : ''
|
1661 |
|
1662 | text = `<div class="card-view">${cardTitle}<span class="card-view-value ${cellStyle.classes || ''}"${style_}>${value}</span></div>`
|
1663 |
|
1664 | if (this.options.smartDisplay && value === '') {
|
1665 | text = '<div class="card-view"></div>'
|
1666 | }
|
1667 | } else {
|
1668 | text = `<td${id_}${class_}${style_}${data_}${rowspan_}${colspan_}${title_}>${value}</td>`
|
1669 | }
|
1670 |
|
1671 | html.push(text)
|
1672 | })
|
1673 |
|
1674 | if (detailViewTemplate && this.options.detailViewAlign === 'right') {
|
1675 | html.push(detailViewTemplate)
|
1676 | }
|
1677 |
|
1678 | if (this.options.cardView) {
|
1679 | html.push('</div></td>')
|
1680 | }
|
1681 | html.push('</tr>')
|
1682 |
|
1683 | return html.join('')
|
1684 | }
|
1685 |
|
1686 | initBody (fixedScroll, updatedUid) {
|
1687 | const data = this.getData()
|
1688 |
|
1689 | this.trigger('pre-body', data)
|
1690 |
|
1691 | this.$body = this.$el.find('>tbody')
|
1692 | if (!this.$body.length) {
|
1693 | this.$body = $('<tbody></tbody>').appendTo(this.$el)
|
1694 | }
|
1695 |
|
1696 |
|
1697 | if (!this.options.pagination || this.options.sidePagination === 'server') {
|
1698 | this.pageFrom = 1
|
1699 | this.pageTo = data.length
|
1700 | }
|
1701 |
|
1702 | const rows = []
|
1703 | const trFragments = $(document.createDocumentFragment())
|
1704 | let hasTr = false
|
1705 | const toExpand = []
|
1706 |
|
1707 | this.autoMergeCells = Utils.checkAutoMergeCells(data.slice(this.pageFrom - 1, this.pageTo))
|
1708 |
|
1709 | for (let i = this.pageFrom - 1; i < this.pageTo; i++) {
|
1710 | const item = data[i]
|
1711 | let tr = this.initRow(item, i, data, trFragments)
|
1712 |
|
1713 | hasTr = hasTr || !!tr
|
1714 | if (tr && typeof tr === 'string') {
|
1715 |
|
1716 | const uniqueId = this.options.uniqueId
|
1717 |
|
1718 | if (uniqueId && item.hasOwnProperty(uniqueId)) {
|
1719 | const itemUniqueId = item[uniqueId]
|
1720 |
|
1721 | const oldTr = this.$body.find(Utils.sprintf('> tr[data-uniqueid="%s"][data-has-detail-view]', itemUniqueId))
|
1722 | const oldTrNext = oldTr.next()
|
1723 |
|
1724 | if (oldTrNext.is('tr.detail-view')) {
|
1725 |
|
1726 | toExpand.push(i)
|
1727 |
|
1728 | if (!updatedUid || itemUniqueId !== updatedUid) {
|
1729 | tr += oldTrNext[0].outerHTML
|
1730 | }
|
1731 | }
|
1732 | }
|
1733 |
|
1734 | if (!this.options.virtualScroll) {
|
1735 | trFragments.append(tr)
|
1736 | } else {
|
1737 | rows.push(tr)
|
1738 | }
|
1739 | }
|
1740 | }
|
1741 |
|
1742 |
|
1743 | if (!hasTr) {
|
1744 | this.$body.html(`<tr class="no-records-found">${Utils.sprintf('<td colspan="%s">%s</td>',
|
1745 | this.getVisibleFields().length + Utils.getDetailViewIndexOffset(this.options),
|
1746 | this.options.formatNoMatches())}</tr>`)
|
1747 | } else if (!this.options.virtualScroll) {
|
1748 | this.$body.html(trFragments)
|
1749 | } else {
|
1750 | if (this.virtualScroll) {
|
1751 | this.virtualScroll.destroy()
|
1752 | }
|
1753 | this.virtualScroll = new VirtualScroll({
|
1754 | rows,
|
1755 | fixedScroll,
|
1756 | scrollEl: this.$tableBody[0],
|
1757 | contentEl: this.$body[0],
|
1758 | itemHeight: this.options.virtualScrollItemHeight,
|
1759 | callback: (startIndex, endIndex) => {
|
1760 | this.fitHeader()
|
1761 | this.initBodyEvent()
|
1762 | this.trigger('virtual-scroll', startIndex, endIndex)
|
1763 | }
|
1764 | })
|
1765 | }
|
1766 |
|
1767 | toExpand.forEach(index => { this.expandRow(index) })
|
1768 |
|
1769 | if (!fixedScroll) {
|
1770 | this.scrollTo(0)
|
1771 | }
|
1772 |
|
1773 | this.initBodyEvent()
|
1774 | this.initFooter()
|
1775 | this.resetView()
|
1776 | this.updateSelected()
|
1777 |
|
1778 | if (this.options.sidePagination !== 'server') {
|
1779 | this.options.totalRows = data.length
|
1780 | }
|
1781 |
|
1782 | this.trigger('post-body', data)
|
1783 | }
|
1784 |
|
1785 | initBodyEvent () {
|
1786 |
|
1787 | this.$body.find('> tr[data-index] > td').off('click dblclick').on('click dblclick', e => {
|
1788 | const $td = $(e.currentTarget)
|
1789 |
|
1790 | if ($td.find('.detail-icon').length) {
|
1791 | return
|
1792 | }
|
1793 |
|
1794 | const $tr = $td.parent()
|
1795 | const $cardViewArr = $(e.target).parents('.card-views').children()
|
1796 | const $cardViewTarget = $(e.target).parents('.card-view')
|
1797 | const rowIndex = $tr.data('index')
|
1798 | const item = this.data[rowIndex]
|
1799 | const index = this.options.cardView ? $cardViewArr.index($cardViewTarget) : $td[0].cellIndex
|
1800 | const fields = this.getVisibleFields()
|
1801 | const field = fields[index - Utils.getDetailViewIndexOffset(this.options)]
|
1802 | const column = this.columns[this.fieldsColumnsIndex[field]]
|
1803 | const value = Utils.getItemField(item, field, this.options.escape, column.escape)
|
1804 |
|
1805 | this.trigger(e.type === 'click' ? 'click-cell' : 'dbl-click-cell', field, value, item, $td)
|
1806 | this.trigger(e.type === 'click' ? 'click-row' : 'dbl-click-row', item, $tr, field)
|
1807 |
|
1808 |
|
1809 | if (
|
1810 | e.type === 'click' &&
|
1811 | this.options.clickToSelect &&
|
1812 | column.clickToSelect &&
|
1813 | !Utils.calculateObjectValue(this.options, this.options.ignoreClickToSelectOn, [e.target])
|
1814 | ) {
|
1815 | const $selectItem = $tr.find(Utils.sprintf('[name="%s"]', this.options.selectItemName))
|
1816 |
|
1817 | if ($selectItem.length) {
|
1818 | $selectItem[0].click()
|
1819 | }
|
1820 | }
|
1821 |
|
1822 | if (e.type === 'click' && this.options.detailViewByClick) {
|
1823 | this.toggleDetailView(rowIndex, this.header.detailFormatters[this.fieldsColumnsIndex[field]])
|
1824 | }
|
1825 | }).off('mousedown').on('mousedown', e => {
|
1826 |
|
1827 | this.multipleSelectRowCtrlKey = e.ctrlKey || e.metaKey
|
1828 | this.multipleSelectRowShiftKey = e.shiftKey
|
1829 | })
|
1830 |
|
1831 | this.$body.find('> tr[data-index] > td > .detail-icon').off('click').on('click', e => {
|
1832 | e.preventDefault()
|
1833 | this.toggleDetailView($(e.currentTarget).parent().parent().data('index'))
|
1834 | return false
|
1835 | })
|
1836 |
|
1837 | this.$selectItem = this.$body.find(Utils.sprintf('[name="%s"]', this.options.selectItemName))
|
1838 | this.$selectItem.off('click').on('click', e => {
|
1839 | e.stopImmediatePropagation()
|
1840 |
|
1841 | const $this = $(e.currentTarget)
|
1842 |
|
1843 | this._toggleCheck($this.prop('checked'), $this.data('index'))
|
1844 | })
|
1845 |
|
1846 | this.header.events.forEach((_events, i) => {
|
1847 | let events = _events
|
1848 |
|
1849 | if (!events) {
|
1850 | return
|
1851 | }
|
1852 |
|
1853 | if (typeof events === 'string') {
|
1854 | events = Utils.calculateObjectValue(null, events)
|
1855 | }
|
1856 |
|
1857 | if (!events) {
|
1858 | throw new Error(`Unknown event in the scope: ${_events}`)
|
1859 | }
|
1860 |
|
1861 | const field = this.header.fields[i]
|
1862 | let fieldIndex = this.getVisibleFields().indexOf(field)
|
1863 |
|
1864 | if (fieldIndex === -1) {
|
1865 | return
|
1866 | }
|
1867 |
|
1868 | fieldIndex += Utils.getDetailViewIndexOffset(this.options)
|
1869 |
|
1870 | for (const key in events) {
|
1871 | if (!events.hasOwnProperty(key)) {
|
1872 | continue
|
1873 | }
|
1874 | const event = events[key]
|
1875 |
|
1876 | this.$body.find('>tr:not(.no-records-found)').each((i, tr) => {
|
1877 | const $tr = $(tr)
|
1878 | const $td = $tr.find(this.options.cardView ? '.card-views>.card-view' : '>td').eq(fieldIndex)
|
1879 | const index = key.indexOf(' ')
|
1880 | const name = key.substring(0, index)
|
1881 | const el = key.substring(index + 1)
|
1882 |
|
1883 | $td.find(el).off(name).on(name, e => {
|
1884 | const index = $tr.data('index')
|
1885 | const row = this.data[index]
|
1886 | const value = row[field]
|
1887 |
|
1888 | event.apply(this, [e, value, row, index])
|
1889 | })
|
1890 | })
|
1891 | }
|
1892 | })
|
1893 | }
|
1894 |
|
1895 | initServer (silent, query, url) {
|
1896 | let data = {}
|
1897 | const index = this.header.fields.indexOf(this.options.sortName)
|
1898 |
|
1899 | let params = {
|
1900 | searchText: this.searchText,
|
1901 | sortName: this.options.sortName,
|
1902 | sortOrder: this.options.sortOrder
|
1903 | }
|
1904 |
|
1905 | if (this.header.sortNames[index]) {
|
1906 | params.sortName = this.header.sortNames[index]
|
1907 | }
|
1908 |
|
1909 | if (this.options.pagination && this.options.sidePagination === 'server') {
|
1910 | params.pageSize = this.options.pageSize === this.options.formatAllRows() ?
|
1911 | this.options.totalRows : this.options.pageSize
|
1912 | params.pageNumber = this.options.pageNumber
|
1913 | }
|
1914 |
|
1915 | if (!(url || this.options.url) && !this.options.ajax) {
|
1916 | return
|
1917 | }
|
1918 |
|
1919 | if (this.options.queryParamsType === 'limit') {
|
1920 | params = {
|
1921 | search: params.searchText,
|
1922 | sort: params.sortName,
|
1923 | order: params.sortOrder
|
1924 | }
|
1925 |
|
1926 | if (this.options.pagination && this.options.sidePagination === 'server') {
|
1927 | params.offset = this.options.pageSize === this.options.formatAllRows() ?
|
1928 | 0 : this.options.pageSize * (this.options.pageNumber - 1)
|
1929 | params.limit = this.options.pageSize
|
1930 | if (params.limit === 0 || this.options.pageSize === this.options.formatAllRows()) {
|
1931 | delete params.limit
|
1932 | }
|
1933 | }
|
1934 | }
|
1935 |
|
1936 | if (
|
1937 | this.options.search &&
|
1938 | this.options.sidePagination === 'server' &&
|
1939 | this.columns.filter(column => !column.searchable).length
|
1940 | ) {
|
1941 | params.searchable = []
|
1942 |
|
1943 | for (const column of this.columns) {
|
1944 | if (
|
1945 | !column.checkbox &&
|
1946 | column.searchable &&
|
1947 | (
|
1948 | this.options.visibleSearch &&
|
1949 | column.visible ||
|
1950 | !this.options.visibleSearch
|
1951 | )
|
1952 | ) {
|
1953 | params.searchable.push(column.field)
|
1954 | }
|
1955 | }
|
1956 | }
|
1957 |
|
1958 | if (!(Utils.isEmptyObject(this.filterColumnsPartial))) {
|
1959 | params.filter = JSON.stringify(this.filterColumnsPartial, null)
|
1960 | }
|
1961 |
|
1962 | $.extend(params, query || {})
|
1963 |
|
1964 | data = Utils.calculateObjectValue(this.options, this.options.queryParams, [params], data)
|
1965 |
|
1966 |
|
1967 | if (data === false) {
|
1968 | return
|
1969 | }
|
1970 |
|
1971 | if (!silent) {
|
1972 | this.showLoading()
|
1973 | }
|
1974 | const request = $.extend({}, Utils.calculateObjectValue(null, this.options.ajaxOptions), {
|
1975 | type: this.options.method,
|
1976 | url: url || this.options.url,
|
1977 | data: this.options.contentType === 'application/json' && this.options.method === 'post' ?
|
1978 | JSON.stringify(data) : data,
|
1979 | cache: this.options.cache,
|
1980 | contentType: this.options.contentType,
|
1981 | dataType: this.options.dataType,
|
1982 | success: (_res, textStatus, jqXHR) => {
|
1983 | const res = Utils.calculateObjectValue(this.options,
|
1984 | this.options.responseHandler, [_res, jqXHR], _res)
|
1985 |
|
1986 | this.load(res)
|
1987 | this.trigger('load-success', res, jqXHR && jqXHR.status, jqXHR)
|
1988 | if (!silent) {
|
1989 | this.hideLoading()
|
1990 | }
|
1991 |
|
1992 | if (
|
1993 | this.options.sidePagination === 'server' &&
|
1994 | this.options.pageNumber > 1 &&
|
1995 | res[this.options.totalField] > 0 &&
|
1996 | !res[this.options.dataField].length
|
1997 | ) {
|
1998 | this.updatePagination()
|
1999 | }
|
2000 | },
|
2001 | error: jqXHR => {
|
2002 |
|
2003 | if (jqXHR && jqXHR.status === 0 && this._xhrAbort) {
|
2004 | this._xhrAbort = false
|
2005 | return
|
2006 | }
|
2007 |
|
2008 | let data = []
|
2009 |
|
2010 | if (this.options.sidePagination === 'server') {
|
2011 | data = {}
|
2012 | data[this.options.totalField] = 0
|
2013 | data[this.options.dataField] = []
|
2014 | }
|
2015 | this.load(data)
|
2016 | this.trigger('load-error', jqXHR && jqXHR.status, jqXHR)
|
2017 | if (!silent) {
|
2018 | this.hideLoading()
|
2019 | }
|
2020 | }
|
2021 | })
|
2022 |
|
2023 | if (this.options.ajax) {
|
2024 | Utils.calculateObjectValue(this, this.options.ajax, [request], null)
|
2025 | } else {
|
2026 | if (this._xhr && this._xhr.readyState !== 4) {
|
2027 | this._xhrAbort = true
|
2028 | this._xhr.abort()
|
2029 | }
|
2030 | this._xhr = $.ajax(request)
|
2031 | }
|
2032 |
|
2033 | return data
|
2034 | }
|
2035 |
|
2036 | initSearchText () {
|
2037 | if (this.options.search) {
|
2038 | this.searchText = ''
|
2039 | if (this.options.searchText !== '') {
|
2040 | const $search = Utils.getSearchInput(this)
|
2041 |
|
2042 | $search.val(this.options.searchText)
|
2043 | this.onSearch({ currentTarget: $search, firedByInitSearchText: true })
|
2044 | }
|
2045 | }
|
2046 | }
|
2047 |
|
2048 | getCaret () {
|
2049 | this.$header.find('th').each((i, th) => {
|
2050 | $(th).find('.sortable').removeClass('desc asc')
|
2051 | .addClass($(th).data('field') === this.options.sortName ?
|
2052 | this.options.sortOrder : 'both')
|
2053 | })
|
2054 | }
|
2055 |
|
2056 | updateSelected () {
|
2057 | const checkAll = this.$selectItem.filter(':enabled').length &&
|
2058 | this.$selectItem.filter(':enabled').length ===
|
2059 | this.$selectItem.filter(':enabled').filter(':checked').length
|
2060 |
|
2061 | this.$selectAll.add(this.$selectAll_).prop('checked', checkAll)
|
2062 |
|
2063 | this.$selectItem.each((i, el) => {
|
2064 | $(el).closest('tr')[$(el).prop('checked') ? 'addClass' : 'removeClass']('selected')
|
2065 | })
|
2066 | }
|
2067 |
|
2068 | updateRows () {
|
2069 | this.$selectItem.each((i, el) => {
|
2070 | this.data[$(el).data('index')][this.header.stateField] = $(el).prop('checked')
|
2071 | })
|
2072 | }
|
2073 |
|
2074 | resetRows () {
|
2075 | for (const row of this.data) {
|
2076 | this.$selectAll.prop('checked', false)
|
2077 | this.$selectItem.prop('checked', false)
|
2078 | if (this.header.stateField) {
|
2079 | row[this.header.stateField] = false
|
2080 | }
|
2081 | }
|
2082 | this.initHiddenRows()
|
2083 | }
|
2084 |
|
2085 | trigger (_name, ...args) {
|
2086 | const name = `${_name}.bs.table`
|
2087 |
|
2088 | this.options[BootstrapTable.EVENTS[name]](...[...args, this])
|
2089 | this.$el.trigger($.Event(name, { sender: this }), args)
|
2090 |
|
2091 | this.options.onAll(name, ...[...args, this])
|
2092 | this.$el.trigger($.Event('all.bs.table', { sender: this }), [name, args])
|
2093 | }
|
2094 |
|
2095 | resetHeader () {
|
2096 |
|
2097 |
|
2098 | clearTimeout(this.timeoutId_)
|
2099 | this.timeoutId_ = setTimeout(() => this.fitHeader(), this.$el.is(':hidden') ? 100 : 0)
|
2100 | }
|
2101 |
|
2102 | fitHeader () {
|
2103 | if (this.$el.is(':hidden')) {
|
2104 | this.timeoutId_ = setTimeout(() => this.fitHeader(), 100)
|
2105 | return
|
2106 | }
|
2107 |
|
2108 | const fixedBody = this.$tableBody.get(0)
|
2109 | const scrollWidth = this.hasScrollBar &&
|
2110 | fixedBody.scrollHeight > fixedBody.clientHeight + this.$header.outerHeight() ?
|
2111 | Utils.getScrollBarWidth() : 0
|
2112 |
|
2113 | this.$el.css('margin-top', -this.$header.outerHeight())
|
2114 |
|
2115 | const focused = $(':focus')
|
2116 |
|
2117 | if (focused.length > 0) {
|
2118 | const $th = focused.parents('th')
|
2119 |
|
2120 | if ($th.length > 0) {
|
2121 | const dataField = $th.attr('data-field')
|
2122 |
|
2123 | if (dataField !== undefined) {
|
2124 | const $headerTh = this.$header.find(`[data-field='${dataField}']`)
|
2125 |
|
2126 | if ($headerTh.length > 0) {
|
2127 | $headerTh.find(':input').addClass('focus-temp')
|
2128 | }
|
2129 | }
|
2130 | }
|
2131 | }
|
2132 |
|
2133 | this.$header_ = this.$header.clone(true, true)
|
2134 | this.$selectAll_ = this.$header_.find('[name="btSelectAll"]')
|
2135 | this.$tableHeader
|
2136 | .css('margin-right', scrollWidth)
|
2137 | .find('table').css('width', this.$el.outerWidth())
|
2138 | .html('').attr('class', this.$el.attr('class'))
|
2139 | .append(this.$header_)
|
2140 |
|
2141 | this.$tableLoading.css('width', this.$el.outerWidth())
|
2142 |
|
2143 | const focusedTemp = $('.focus-temp:visible:eq(0)')
|
2144 |
|
2145 | if (focusedTemp.length > 0) {
|
2146 | focusedTemp.focus()
|
2147 | this.$header.find('.focus-temp').removeClass('focus-temp')
|
2148 | }
|
2149 |
|
2150 |
|
2151 | this.$header.find('th[data-field]').each((i, el) => {
|
2152 | this.$header_.find(Utils.sprintf('th[data-field="%s"]', $(el).data('field'))).data($(el).data())
|
2153 | })
|
2154 |
|
2155 | const visibleFields = this.getVisibleFields()
|
2156 | const $ths = this.$header_.find('th')
|
2157 | let $tr = this.$body.find('>tr:not(.no-records-found,.virtual-scroll-top)').eq(0)
|
2158 |
|
2159 | while ($tr.length && $tr.find('>td[colspan]:not([colspan="1"])').length) {
|
2160 | $tr = $tr.next()
|
2161 | }
|
2162 |
|
2163 | const trLength = $tr.find('> *').length
|
2164 |
|
2165 | $tr.find('> *').each((i, el) => {
|
2166 | const $this = $(el)
|
2167 |
|
2168 | if (Utils.hasDetailViewIcon(this.options)) {
|
2169 | if (
|
2170 | i === 0 && this.options.detailViewAlign !== 'right' ||
|
2171 | i === trLength - 1 && this.options.detailViewAlign === 'right'
|
2172 | ) {
|
2173 | const $thDetail = $ths.filter('.detail')
|
2174 | const zoomWidth = $thDetail.innerWidth() - $thDetail.find('.fht-cell').width()
|
2175 |
|
2176 | $thDetail.find('.fht-cell').width($this.innerWidth() - zoomWidth)
|
2177 | return
|
2178 | }
|
2179 | }
|
2180 |
|
2181 | const index = i - Utils.getDetailViewIndexOffset(this.options)
|
2182 | let $th = this.$header_.find(Utils.sprintf('th[data-field="%s"]', visibleFields[index]))
|
2183 |
|
2184 | if ($th.length > 1) {
|
2185 | $th = $($ths[$this[0].cellIndex])
|
2186 | }
|
2187 |
|
2188 | const zoomWidth = $th.innerWidth() - $th.find('.fht-cell').width()
|
2189 |
|
2190 | $th.find('.fht-cell').width($this.innerWidth() - zoomWidth)
|
2191 | })
|
2192 |
|
2193 | this.horizontalScroll()
|
2194 | this.trigger('post-header')
|
2195 | }
|
2196 |
|
2197 | initFooter () {
|
2198 | if (!this.options.showFooter || this.options.cardView) {
|
2199 | return
|
2200 | }
|
2201 |
|
2202 | const data = this.getData()
|
2203 | const html = []
|
2204 | let detailTemplate = ''
|
2205 |
|
2206 | if (Utils.hasDetailViewIcon(this.options)) {
|
2207 | detailTemplate = '<th class="detail"><div class="th-inner"></div><div class="fht-cell"></div></th>'
|
2208 | }
|
2209 |
|
2210 | if (detailTemplate && this.options.detailViewAlign !== 'right') {
|
2211 | html.push(detailTemplate)
|
2212 | }
|
2213 |
|
2214 | for (const column of this.columns) {
|
2215 | let falign = ''
|
2216 | let valign = ''
|
2217 | const csses = []
|
2218 | let style = {}
|
2219 | let class_ = Utils.sprintf(' class="%s"', column['class'])
|
2220 |
|
2221 | if (
|
2222 | !column.visible ||
|
2223 | (this.footerData && this.footerData.length > 0 && !(column.field in this.footerData[0]))
|
2224 | ) {
|
2225 | continue
|
2226 | }
|
2227 |
|
2228 | if (this.options.cardView && (!column.cardVisible)) {
|
2229 | return
|
2230 | }
|
2231 |
|
2232 | falign = Utils.sprintf('text-align: %s; ', column.falign ? column.falign : column.align)
|
2233 | valign = Utils.sprintf('vertical-align: %s; ', column.valign)
|
2234 |
|
2235 | style = Utils.calculateObjectValue(null, this.options.footerStyle, [column])
|
2236 |
|
2237 | if (style && style.css) {
|
2238 | for (const [key, value] of Object.entries(style.css)) {
|
2239 | csses.push(`${key}: ${value}`)
|
2240 | }
|
2241 | }
|
2242 | if (style && style.classes) {
|
2243 | class_ = Utils.sprintf(' class="%s"', column['class'] ?
|
2244 | [column['class'], style.classes].join(' ') : style.classes)
|
2245 | }
|
2246 |
|
2247 | html.push('<th', class_, Utils.sprintf(' style="%s"', falign + valign + csses.concat().join('; ')))
|
2248 | let colspan = 0
|
2249 |
|
2250 | if (this.footerData && this.footerData.length > 0) {
|
2251 | colspan = this.footerData[0][`_${ column.field }_colspan`] || 0
|
2252 | }
|
2253 | if (colspan) {
|
2254 | html.push(` colspan="${colspan}" `)
|
2255 | }
|
2256 |
|
2257 | html.push('>')
|
2258 | html.push('<div class="th-inner">')
|
2259 |
|
2260 | let value = ''
|
2261 |
|
2262 | if (this.footerData && this.footerData.length > 0) {
|
2263 | value = this.footerData[0][column.field] || ''
|
2264 | }
|
2265 | html.push(Utils.calculateObjectValue(column, column.footerFormatter,
|
2266 | [data, value], value))
|
2267 |
|
2268 | html.push('</div>')
|
2269 | html.push('<div class="fht-cell"></div>')
|
2270 | html.push('</div>')
|
2271 | html.push('</th>')
|
2272 | }
|
2273 |
|
2274 | if (detailTemplate && this.options.detailViewAlign === 'right') {
|
2275 | html.push(detailTemplate)
|
2276 | }
|
2277 |
|
2278 | if (!this.options.height && !this.$tableFooter.length) {
|
2279 | this.$el.append('<tfoot><tr></tr></tfoot>')
|
2280 | this.$tableFooter = this.$el.find('tfoot')
|
2281 | }
|
2282 |
|
2283 | if (!this.$tableFooter.find('tr').length) {
|
2284 | this.$tableFooter.html('<table><thead><tr></tr></thead></table>')
|
2285 | }
|
2286 |
|
2287 | this.$tableFooter.find('tr').html(html.join(''))
|
2288 |
|
2289 | this.trigger('post-footer', this.$tableFooter)
|
2290 | }
|
2291 |
|
2292 | fitFooter () {
|
2293 | if (this.$el.is(':hidden')) {
|
2294 | setTimeout(() => this.fitFooter(), 100)
|
2295 | return
|
2296 | }
|
2297 |
|
2298 | const fixedBody = this.$tableBody.get(0)
|
2299 | const scrollWidth = this.hasScrollBar &&
|
2300 | fixedBody.scrollHeight > fixedBody.clientHeight + this.$header.outerHeight() ?
|
2301 | Utils.getScrollBarWidth() : 0
|
2302 |
|
2303 | this.$tableFooter
|
2304 | .css('margin-right', scrollWidth)
|
2305 | .find('table').css('width', this.$el.outerWidth())
|
2306 | .attr('class', this.$el.attr('class'))
|
2307 |
|
2308 | const $ths = this.$tableFooter.find('th')
|
2309 | let $tr = this.$body.find('>tr:first-child:not(.no-records-found)')
|
2310 |
|
2311 | $ths.find('.fht-cell').width('auto')
|
2312 |
|
2313 | while ($tr.length && $tr.find('>td[colspan]:not([colspan="1"])').length) {
|
2314 | $tr = $tr.next()
|
2315 | }
|
2316 |
|
2317 | const trLength = $tr.find('> *').length
|
2318 |
|
2319 | $tr.find('> *').each((i, el) => {
|
2320 | const $this = $(el)
|
2321 |
|
2322 | if (Utils.hasDetailViewIcon(this.options)) {
|
2323 | if (
|
2324 | i === 0 && this.options.detailViewAlign === 'left' ||
|
2325 | i === trLength - 1 && this.options.detailViewAlign === 'right'
|
2326 | ) {
|
2327 | const $thDetail = $ths.filter('.detail')
|
2328 | const zoomWidth = $thDetail.innerWidth() - $thDetail.find('.fht-cell').width()
|
2329 |
|
2330 | $thDetail.find('.fht-cell').width($this.innerWidth() - zoomWidth)
|
2331 | return
|
2332 | }
|
2333 | }
|
2334 |
|
2335 | const $th = $ths.eq(i)
|
2336 | const zoomWidth = $th.innerWidth() - $th.find('.fht-cell').width()
|
2337 |
|
2338 | $th.find('.fht-cell').width($this.innerWidth() - zoomWidth)
|
2339 | })
|
2340 |
|
2341 | this.horizontalScroll()
|
2342 | }
|
2343 |
|
2344 | horizontalScroll () {
|
2345 |
|
2346 |
|
2347 | this.$tableBody.off('scroll').on('scroll', () => {
|
2348 | const scrollLeft = this.$tableBody.scrollLeft()
|
2349 |
|
2350 | if (this.options.showHeader && this.options.height) {
|
2351 | this.$tableHeader.scrollLeft(scrollLeft)
|
2352 | }
|
2353 |
|
2354 | if (this.options.showFooter && !this.options.cardView) {
|
2355 | this.$tableFooter.scrollLeft(scrollLeft)
|
2356 | }
|
2357 |
|
2358 | this.trigger('scroll-body', this.$tableBody)
|
2359 | })
|
2360 | }
|
2361 |
|
2362 | getVisibleFields () {
|
2363 | const visibleFields = []
|
2364 |
|
2365 | for (const field of this.header.fields) {
|
2366 | const column = this.columns[this.fieldsColumnsIndex[field]]
|
2367 |
|
2368 | if (!column || !column.visible || (this.options.cardView && !column.cardVisible)) {
|
2369 | continue
|
2370 | }
|
2371 | visibleFields.push(field)
|
2372 | }
|
2373 | return visibleFields
|
2374 | }
|
2375 |
|
2376 | initHiddenRows () {
|
2377 | this.hiddenRows = []
|
2378 | }
|
2379 |
|
2380 |
|
2381 |
|
2382 |
|
2383 | getOptions () {
|
2384 |
|
2385 | const options = $.extend({}, this.options)
|
2386 |
|
2387 | delete options.data
|
2388 | return $.extend(true, {}, options)
|
2389 | }
|
2390 |
|
2391 | refreshOptions (options) {
|
2392 |
|
2393 | if (Utils.compareObjects(this.options, options, true)) {
|
2394 | return
|
2395 | }
|
2396 | this.options = $.extend(this.options, options)
|
2397 | this.trigger('refresh-options', this.options)
|
2398 | this.destroy()
|
2399 | this.init()
|
2400 | }
|
2401 |
|
2402 | getData (params) {
|
2403 | let data = this.options.data
|
2404 |
|
2405 | if (
|
2406 | (
|
2407 | this.searchText ||
|
2408 | this.options.customSearch ||
|
2409 | this.options.sortName !== undefined ||
|
2410 | this.enableCustomSort ||
|
2411 | !Utils.isEmptyObject(this.filterColumns) ||
|
2412 | typeof this.options.filterOptions.filterAlgorithm === 'function' ||
|
2413 | !Utils.isEmptyObject(this.filterColumnsPartial)
|
2414 | ) && (!params || !params.unfiltered)
|
2415 | ) {
|
2416 | data = this.data
|
2417 | }
|
2418 |
|
2419 | if (params && params.useCurrentPage) {
|
2420 | data = data.slice(this.pageFrom - 1, this.pageTo)
|
2421 | }
|
2422 |
|
2423 | if (params && !params.includeHiddenRows) {
|
2424 | const hiddenRows = this.getHiddenRows()
|
2425 |
|
2426 | data = data.filter(row => Utils.findIndex(hiddenRows, row) === -1)
|
2427 | }
|
2428 |
|
2429 | if (params && params.formatted) {
|
2430 | data.forEach(row => {
|
2431 | for (const [key, value] of Object.entries(row)) {
|
2432 | const column = this.columns[this.fieldsColumnsIndex[key]]
|
2433 |
|
2434 | if (!column) {
|
2435 | return
|
2436 | }
|
2437 |
|
2438 | row[key] = Utils.calculateObjectValue(column, this.header.formatters[column.fieldIndex], [value, row, row.index, column.field], value)
|
2439 | }
|
2440 | })
|
2441 | }
|
2442 |
|
2443 | return data
|
2444 | }
|
2445 |
|
2446 | getSelections () {
|
2447 | return (this.options.maintainMetaData ? this.options.data : this.data)
|
2448 | .filter(row => row[this.header.stateField] === true)
|
2449 | }
|
2450 |
|
2451 | load (_data) {
|
2452 | let fixedScroll = false
|
2453 | let data = _data
|
2454 |
|
2455 |
|
2456 | if (this.options.pagination && this.options.sidePagination === 'server') {
|
2457 | this.options.totalRows = data[this.options.totalField]
|
2458 | this.options.totalNotFiltered = data[this.options.totalNotFilteredField]
|
2459 | this.footerData = data[this.options.footerField] ? [data[this.options.footerField]] : undefined
|
2460 | }
|
2461 |
|
2462 | fixedScroll = data.fixedScroll
|
2463 | data = Array.isArray(data) ? data : data[this.options.dataField]
|
2464 |
|
2465 | this.initData(data)
|
2466 | this.initSearch()
|
2467 | this.initPagination()
|
2468 | this.initBody(fixedScroll)
|
2469 | }
|
2470 |
|
2471 | append (data) {
|
2472 | this.initData(data, 'append')
|
2473 | this.initSearch()
|
2474 | this.initPagination()
|
2475 | this.initSort()
|
2476 | this.initBody(true)
|
2477 | }
|
2478 |
|
2479 | prepend (data) {
|
2480 | this.initData(data, 'prepend')
|
2481 | this.initSearch()
|
2482 | this.initPagination()
|
2483 | this.initSort()
|
2484 | this.initBody(true)
|
2485 | }
|
2486 |
|
2487 | remove (params) {
|
2488 | let removed = 0
|
2489 |
|
2490 | for (let i = this.options.data.length - 1; i >= 0; i--) {
|
2491 |
|
2492 | const row = this.options.data[i]
|
2493 |
|
2494 | if (!row.hasOwnProperty(params.field) && params.field !== '$index') {
|
2495 | continue
|
2496 | }
|
2497 |
|
2498 | if (
|
2499 | !row.hasOwnProperty(params.field) &&
|
2500 | params.field === '$index' &&
|
2501 | params.values.includes(i) ||
|
2502 | params.values.includes(row[params.field])
|
2503 | ) {
|
2504 | removed++
|
2505 |
|
2506 | this.options.data.splice(i, 1)
|
2507 | }
|
2508 | }
|
2509 |
|
2510 | if (!removed) {
|
2511 | return
|
2512 | }
|
2513 |
|
2514 | if (this.options.sidePagination === 'server') {
|
2515 | this.options.totalRows -= removed
|
2516 | this.data = [...this.options.data]
|
2517 | }
|
2518 |
|
2519 | this.initSearch()
|
2520 | this.initPagination()
|
2521 | this.initSort()
|
2522 | this.initBody(true)
|
2523 | }
|
2524 |
|
2525 | removeAll () {
|
2526 | if (this.options.data.length > 0) {
|
2527 | this.options.data.splice(0, this.options.data.length)
|
2528 | this.initSearch()
|
2529 | this.initPagination()
|
2530 | this.initBody(true)
|
2531 | }
|
2532 | }
|
2533 |
|
2534 | insertRow (params) {
|
2535 | if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
|
2536 | return
|
2537 | }
|
2538 | this.options.data.splice(params.index, 0, params.row)
|
2539 | this.initSearch()
|
2540 | this.initPagination()
|
2541 | this.initSort()
|
2542 | this.initBody(true)
|
2543 | }
|
2544 |
|
2545 | updateRow (params) {
|
2546 | const allParams = Array.isArray(params) ? params : [params]
|
2547 |
|
2548 | for (const params of allParams) {
|
2549 | if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
|
2550 | continue
|
2551 | }
|
2552 |
|
2553 | if (params.hasOwnProperty('replace') && params.replace) {
|
2554 | this.options.data[params.index] = params.row
|
2555 | } else {
|
2556 | $.extend(this.options.data[params.index], params.row)
|
2557 | }
|
2558 | }
|
2559 |
|
2560 | this.initSearch()
|
2561 | this.initPagination()
|
2562 | this.initSort()
|
2563 | this.initBody(true)
|
2564 | }
|
2565 |
|
2566 | getRowByUniqueId (_id) {
|
2567 | const uniqueId = this.options.uniqueId
|
2568 | const len = this.options.data.length
|
2569 | let id = _id
|
2570 | let dataRow = null
|
2571 | let i
|
2572 | let row
|
2573 | let rowUniqueId
|
2574 |
|
2575 | for (i = len - 1; i >= 0; i--) {
|
2576 | row = this.options.data[i]
|
2577 |
|
2578 | if (row.hasOwnProperty(uniqueId)) {
|
2579 | rowUniqueId = row[uniqueId]
|
2580 | } else if (row._data && row._data.hasOwnProperty(uniqueId)) {
|
2581 | rowUniqueId = row._data[uniqueId]
|
2582 | } else {
|
2583 | continue
|
2584 | }
|
2585 |
|
2586 | if (typeof rowUniqueId === 'string') {
|
2587 | id = id.toString()
|
2588 | } else if (typeof rowUniqueId === 'number') {
|
2589 | if ((Number(rowUniqueId) === rowUniqueId) && (rowUniqueId % 1 === 0)) {
|
2590 | id = parseInt(id, 10)
|
2591 | } else if ((rowUniqueId === Number(rowUniqueId)) && (rowUniqueId !== 0)) {
|
2592 | id = parseFloat(id)
|
2593 | }
|
2594 | }
|
2595 |
|
2596 | if (rowUniqueId === id) {
|
2597 | dataRow = row
|
2598 | break
|
2599 | }
|
2600 | }
|
2601 |
|
2602 | return dataRow
|
2603 | }
|
2604 |
|
2605 | updateByUniqueId (params) {
|
2606 | const allParams = Array.isArray(params) ? params : [params]
|
2607 | let updatedUid = null
|
2608 |
|
2609 | for (const params of allParams) {
|
2610 | if (!params.hasOwnProperty('id') || !params.hasOwnProperty('row')) {
|
2611 | continue
|
2612 | }
|
2613 |
|
2614 | const rowId = this.options.data.indexOf(this.getRowByUniqueId(params.id))
|
2615 |
|
2616 | if (rowId === -1) {
|
2617 | continue
|
2618 | }
|
2619 |
|
2620 | if (params.hasOwnProperty('replace') && params.replace) {
|
2621 | this.options.data[rowId] = params.row
|
2622 | } else {
|
2623 | $.extend(this.options.data[rowId], params.row)
|
2624 | }
|
2625 | updatedUid = params.id
|
2626 | }
|
2627 |
|
2628 | this.initSearch()
|
2629 | this.initPagination()
|
2630 | this.initSort()
|
2631 | this.initBody(true, updatedUid)
|
2632 | }
|
2633 |
|
2634 | removeByUniqueId (id) {
|
2635 | const len = this.options.data.length
|
2636 | const row = this.getRowByUniqueId(id)
|
2637 |
|
2638 | if (row) {
|
2639 | this.options.data.splice(this.options.data.indexOf(row), 1)
|
2640 | }
|
2641 |
|
2642 | if (len === this.options.data.length) {
|
2643 | return
|
2644 | }
|
2645 |
|
2646 | if (this.options.sidePagination === 'server') {
|
2647 | this.options.totalRows -= 1
|
2648 | this.data = [...this.options.data]
|
2649 | }
|
2650 |
|
2651 | this.initSearch()
|
2652 | this.initPagination()
|
2653 | this.initBody(true)
|
2654 | }
|
2655 |
|
2656 | updateCell (params) {
|
2657 | if (!params.hasOwnProperty('index') ||
|
2658 | !params.hasOwnProperty('field') ||
|
2659 | !params.hasOwnProperty('value')) {
|
2660 | return
|
2661 | }
|
2662 | this.data[params.index][params.field] = params.value
|
2663 |
|
2664 | if (params.reinit === false) {
|
2665 | return
|
2666 | }
|
2667 | this.initSort()
|
2668 | this.initBody(true)
|
2669 | }
|
2670 |
|
2671 | updateCellByUniqueId (params) {
|
2672 | const allParams = Array.isArray(params) ? params : [params]
|
2673 |
|
2674 | allParams.forEach(({ id, field, value }) => {
|
2675 | const rowId = this.options.data.indexOf(this.getRowByUniqueId(id))
|
2676 |
|
2677 | if (rowId === -1) {
|
2678 | return
|
2679 | }
|
2680 | this.options.data[rowId][field] = value
|
2681 | })
|
2682 |
|
2683 | if (params.reinit === false) {
|
2684 | return
|
2685 | }
|
2686 | this.initSort()
|
2687 | this.initBody(true)
|
2688 | }
|
2689 |
|
2690 | showRow (params) {
|
2691 | this._toggleRow(params, true)
|
2692 | }
|
2693 |
|
2694 | hideRow (params) {
|
2695 | this._toggleRow(params, false)
|
2696 | }
|
2697 |
|
2698 | _toggleRow (params, visible) {
|
2699 | let row
|
2700 |
|
2701 | if (params.hasOwnProperty('index')) {
|
2702 | row = this.getData()[params.index]
|
2703 | } else if (params.hasOwnProperty('uniqueId')) {
|
2704 | row = this.getRowByUniqueId(params.uniqueId)
|
2705 | }
|
2706 |
|
2707 | if (!row) {
|
2708 | return
|
2709 | }
|
2710 |
|
2711 | const index = Utils.findIndex(this.hiddenRows, row)
|
2712 |
|
2713 | if (!visible && index === -1) {
|
2714 | this.hiddenRows.push(row)
|
2715 | } else if (visible && index > -1) {
|
2716 | this.hiddenRows.splice(index, 1)
|
2717 | }
|
2718 |
|
2719 | this.initBody(true)
|
2720 | this.initPagination()
|
2721 | }
|
2722 |
|
2723 | getHiddenRows (show) {
|
2724 | if (show) {
|
2725 | this.initHiddenRows()
|
2726 | this.initBody(true)
|
2727 | this.initPagination()
|
2728 | return
|
2729 | }
|
2730 | const data = this.getData()
|
2731 | const rows = []
|
2732 |
|
2733 | for (const row of data) {
|
2734 | if (this.hiddenRows.includes(row)) {
|
2735 | rows.push(row)
|
2736 | }
|
2737 | }
|
2738 | this.hiddenRows = rows
|
2739 | return rows
|
2740 | }
|
2741 |
|
2742 | showColumn (field) {
|
2743 | const fields = Array.isArray(field) ? field : [field]
|
2744 |
|
2745 | fields.forEach(field => {
|
2746 | this._toggleColumn(this.fieldsColumnsIndex[field], true, true)
|
2747 | })
|
2748 | }
|
2749 |
|
2750 | hideColumn (field) {
|
2751 | const fields = Array.isArray(field) ? field : [field]
|
2752 |
|
2753 | fields.forEach(field => {
|
2754 | this._toggleColumn(this.fieldsColumnsIndex[field], false, true)
|
2755 | })
|
2756 | }
|
2757 |
|
2758 | _toggleColumn (index, checked, needUpdate) {
|
2759 | if (index === -1 || this.columns[index].visible === checked) {
|
2760 | return
|
2761 | }
|
2762 | this.columns[index].visible = checked
|
2763 | this.initHeader()
|
2764 | this.initSearch()
|
2765 | this.initPagination()
|
2766 | this.initBody()
|
2767 |
|
2768 | if (this.options.showColumns) {
|
2769 | const $items = this.$toolbar.find('.keep-open input:not(".toggle-all")').prop('disabled', false)
|
2770 |
|
2771 | if (needUpdate) {
|
2772 | $items.filter(Utils.sprintf('[value="%s"]', index)).prop('checked', checked)
|
2773 | }
|
2774 |
|
2775 | if ($items.filter(':checked').length <= this.options.minimumCountColumns) {
|
2776 | $items.filter(':checked').prop('disabled', true)
|
2777 | }
|
2778 | }
|
2779 | }
|
2780 |
|
2781 | getVisibleColumns () {
|
2782 | return this.columns.filter(column => column.visible && !this.isSelectionColumn(column))
|
2783 | }
|
2784 |
|
2785 | getHiddenColumns () {
|
2786 | return this.columns.filter(({ visible }) => !visible)
|
2787 | }
|
2788 |
|
2789 | isSelectionColumn (column) {
|
2790 | return column.radio || column.checkbox
|
2791 | }
|
2792 |
|
2793 | showAllColumns () {
|
2794 | this._toggleAllColumns(true)
|
2795 | }
|
2796 |
|
2797 | hideAllColumns () {
|
2798 | this._toggleAllColumns(false)
|
2799 | }
|
2800 |
|
2801 | _toggleAllColumns (visible) {
|
2802 | for (const column of this.columns.slice().reverse()) {
|
2803 | if (column.switchable) {
|
2804 | if (
|
2805 | !visible &&
|
2806 | this.options.showColumns &&
|
2807 | this.getVisibleColumns().filter(it => it.switchable).length === this.options.minimumCountColumns
|
2808 | ) {
|
2809 | continue
|
2810 | }
|
2811 | column.visible = visible
|
2812 | }
|
2813 | }
|
2814 |
|
2815 | this.initHeader()
|
2816 | this.initSearch()
|
2817 | this.initPagination()
|
2818 | this.initBody()
|
2819 | if (this.options.showColumns) {
|
2820 | const $items = this.$toolbar.find('.keep-open input[type="checkbox"]:not(".toggle-all")').prop('disabled', false)
|
2821 |
|
2822 | if (visible) {
|
2823 | $items.prop('checked', visible)
|
2824 | } else {
|
2825 | $items.get().reverse().forEach(item => {
|
2826 | if ($items.filter(':checked').length > this.options.minimumCountColumns) {
|
2827 | $(item).prop('checked', visible)
|
2828 | }
|
2829 | })
|
2830 | }
|
2831 |
|
2832 | if ($items.filter(':checked').length <= this.options.minimumCountColumns) {
|
2833 | $items.filter(':checked').prop('disabled', true)
|
2834 | }
|
2835 | }
|
2836 | }
|
2837 |
|
2838 | mergeCells (options) {
|
2839 | const row = options.index
|
2840 | let col = this.getVisibleFields().indexOf(options.field)
|
2841 | const rowspan = options.rowspan || 1
|
2842 | const colspan = options.colspan || 1
|
2843 | let i
|
2844 | let j
|
2845 | const $tr = this.$body.find('>tr[data-index]')
|
2846 |
|
2847 | col += Utils.getDetailViewIndexOffset(this.options)
|
2848 |
|
2849 | const $td = $tr.eq(row).find('>td').eq(col)
|
2850 |
|
2851 | if (row < 0 || col < 0 || row >= this.data.length) {
|
2852 | return
|
2853 | }
|
2854 |
|
2855 | for (i = row; i < row + rowspan; i++) {
|
2856 | for (j = col; j < col + colspan; j++) {
|
2857 | $tr.eq(i).find('>td').eq(j).hide()
|
2858 | }
|
2859 | }
|
2860 |
|
2861 | $td.attr('rowspan', rowspan).attr('colspan', colspan).show()
|
2862 | }
|
2863 |
|
2864 | checkAll () {
|
2865 | this._toggleCheckAll(true)
|
2866 | }
|
2867 |
|
2868 | uncheckAll () {
|
2869 | this._toggleCheckAll(false)
|
2870 | }
|
2871 |
|
2872 | _toggleCheckAll (checked) {
|
2873 | const rowsBefore = this.getSelections()
|
2874 |
|
2875 | this.$selectAll.add(this.$selectAll_).prop('checked', checked)
|
2876 | this.$selectItem.filter(':enabled').prop('checked', checked)
|
2877 | this.updateRows()
|
2878 | this.updateSelected()
|
2879 |
|
2880 | const rowsAfter = this.getSelections()
|
2881 |
|
2882 | if (checked) {
|
2883 | this.trigger('check-all', rowsAfter, rowsBefore)
|
2884 | return
|
2885 | }
|
2886 |
|
2887 | this.trigger('uncheck-all', rowsAfter, rowsBefore)
|
2888 | }
|
2889 |
|
2890 | checkInvert () {
|
2891 | const $items = this.$selectItem.filter(':enabled')
|
2892 | let checked = $items.filter(':checked')
|
2893 |
|
2894 | $items.each((i, el) => {
|
2895 | $(el).prop('checked', !$(el).prop('checked'))
|
2896 | })
|
2897 | this.updateRows()
|
2898 | this.updateSelected()
|
2899 | this.trigger('uncheck-some', checked)
|
2900 | checked = this.getSelections()
|
2901 | this.trigger('check-some', checked)
|
2902 | }
|
2903 |
|
2904 | check (index) {
|
2905 | this._toggleCheck(true, index)
|
2906 | }
|
2907 |
|
2908 | uncheck (index) {
|
2909 | this._toggleCheck(false, index)
|
2910 | }
|
2911 |
|
2912 | _toggleCheck (checked, index) {
|
2913 | const $el = this.$selectItem.filter(`[data-index="${index}"]`)
|
2914 | const row = this.data[index]
|
2915 |
|
2916 | if (
|
2917 | $el.is(':radio') ||
|
2918 | this.options.singleSelect ||
|
2919 | this.options.multipleSelectRow &&
|
2920 | !this.multipleSelectRowCtrlKey &&
|
2921 | !this.multipleSelectRowShiftKey
|
2922 | ) {
|
2923 | for (const r of this.options.data) {
|
2924 | r[this.header.stateField] = false
|
2925 | }
|
2926 | this.$selectItem.filter(':checked').not($el).prop('checked', false)
|
2927 | }
|
2928 |
|
2929 | row[this.header.stateField] = checked
|
2930 |
|
2931 | if (this.options.multipleSelectRow) {
|
2932 | if (this.multipleSelectRowShiftKey && this.multipleSelectRowLastSelectedIndex >= 0) {
|
2933 | const [fromIndex, toIndex] = this.multipleSelectRowLastSelectedIndex < index ?
|
2934 | [this.multipleSelectRowLastSelectedIndex, index] : [index, this.multipleSelectRowLastSelectedIndex]
|
2935 |
|
2936 | for (let i = fromIndex + 1; i < toIndex; i++) {
|
2937 | this.data[i][this.header.stateField] = true
|
2938 | this.$selectItem.filter(`[data-index="${i}"]`).prop('checked', true)
|
2939 | }
|
2940 | }
|
2941 |
|
2942 | this.multipleSelectRowCtrlKey = false
|
2943 | this.multipleSelectRowShiftKey = false
|
2944 | this.multipleSelectRowLastSelectedIndex = checked ? index : -1
|
2945 | }
|
2946 |
|
2947 | $el.prop('checked', checked)
|
2948 | this.updateSelected()
|
2949 | this.trigger(checked ? 'check' : 'uncheck', this.data[index], $el)
|
2950 | }
|
2951 |
|
2952 | checkBy (obj) {
|
2953 | this._toggleCheckBy(true, obj)
|
2954 | }
|
2955 |
|
2956 | uncheckBy (obj) {
|
2957 | this._toggleCheckBy(false, obj)
|
2958 | }
|
2959 |
|
2960 | _toggleCheckBy (checked, obj) {
|
2961 | if (!obj.hasOwnProperty('field') || !obj.hasOwnProperty('values')) {
|
2962 | return
|
2963 | }
|
2964 |
|
2965 | const rows = []
|
2966 |
|
2967 | this.data.forEach((row, i) => {
|
2968 | if (!row.hasOwnProperty(obj.field)) {
|
2969 | return false
|
2970 | }
|
2971 | if (obj.values.includes(row[obj.field])) {
|
2972 | let $el = this.$selectItem.filter(':enabled')
|
2973 | .filter(Utils.sprintf('[data-index="%s"]', i))
|
2974 | const onlyCurrentPage = obj.hasOwnProperty('onlyCurrentPage') ? obj.onlyCurrentPage : false
|
2975 |
|
2976 | $el = checked ? $el.not(':checked') : $el.filter(':checked')
|
2977 |
|
2978 | if (!$el.length && onlyCurrentPage) {
|
2979 | return
|
2980 | }
|
2981 |
|
2982 | $el.prop('checked', checked)
|
2983 | row[this.header.stateField] = checked
|
2984 | rows.push(row)
|
2985 | this.trigger(checked ? 'check' : 'uncheck', row, $el)
|
2986 | }
|
2987 | })
|
2988 | this.updateSelected()
|
2989 | this.trigger(checked ? 'check-some' : 'uncheck-some', rows)
|
2990 | }
|
2991 |
|
2992 | refresh (params) {
|
2993 | if (params && params.url) {
|
2994 | this.options.url = params.url
|
2995 | }
|
2996 | if (params && params.pageNumber) {
|
2997 | this.options.pageNumber = params.pageNumber
|
2998 | }
|
2999 | if (params && params.pageSize) {
|
3000 | this.options.pageSize = params.pageSize
|
3001 | }
|
3002 | this.trigger('refresh', this.initServer(params && params.silent,
|
3003 | params && params.query, params && params.url))
|
3004 | }
|
3005 |
|
3006 | destroy () {
|
3007 | this.$el.insertBefore(this.$container)
|
3008 | $(this.options.toolbar).insertBefore(this.$el)
|
3009 | this.$container.next().remove()
|
3010 | this.$container.remove()
|
3011 | this.$el.html(this.$el_.html())
|
3012 | .css('margin-top', '0')
|
3013 | .attr('class', this.$el_.attr('class') || '')
|
3014 |
|
3015 | const resizeEvent = Utils.getEventName('resize.bootstrap-table', this.$el.attr('id'))
|
3016 |
|
3017 | $(window).off(resizeEvent)
|
3018 | }
|
3019 |
|
3020 | resetView (params) {
|
3021 | let padding = 0
|
3022 |
|
3023 | if (params && params.height) {
|
3024 | this.options.height = params.height
|
3025 | }
|
3026 |
|
3027 | this.$tableContainer.toggleClass('has-card-view', this.options.cardView)
|
3028 |
|
3029 | if (this.options.height) {
|
3030 | const fixedBody = this.$tableBody.get(0)
|
3031 |
|
3032 | this.hasScrollBar = fixedBody.scrollWidth > fixedBody.clientWidth
|
3033 | }
|
3034 |
|
3035 | if (!this.options.cardView && this.options.showHeader && this.options.height) {
|
3036 | this.$tableHeader.show()
|
3037 | this.resetHeader()
|
3038 | padding += this.$header.outerHeight(true) + 1
|
3039 | } else {
|
3040 | this.$tableHeader.hide()
|
3041 | this.trigger('post-header')
|
3042 | }
|
3043 |
|
3044 | if (!this.options.cardView && this.options.showFooter) {
|
3045 | this.$tableFooter.show()
|
3046 | this.fitFooter()
|
3047 | if (this.options.height) {
|
3048 | padding += this.$tableFooter.outerHeight(true)
|
3049 | }
|
3050 | }
|
3051 |
|
3052 | if (this.$container.hasClass('fullscreen')) {
|
3053 | this.$tableContainer.css('height', '')
|
3054 | this.$tableContainer.css('width', '')
|
3055 | } else if (this.options.height) {
|
3056 | if (this.$tableBorder) {
|
3057 | this.$tableBorder.css('width', '')
|
3058 | this.$tableBorder.css('height', '')
|
3059 | }
|
3060 |
|
3061 | const toolbarHeight = this.$toolbar.outerHeight(true)
|
3062 | const paginationHeight = this.$pagination.outerHeight(true)
|
3063 | const height = this.options.height - toolbarHeight - paginationHeight
|
3064 | const $bodyTable = this.$tableBody.find('>table')
|
3065 | const tableHeight = $bodyTable.outerHeight()
|
3066 |
|
3067 | this.$tableContainer.css('height', `${height}px`)
|
3068 |
|
3069 | if (this.$tableBorder && $bodyTable.is(':visible')) {
|
3070 | let tableBorderHeight = height - tableHeight - 2
|
3071 |
|
3072 | if (this.hasScrollBar) {
|
3073 | tableBorderHeight -= Utils.getScrollBarWidth()
|
3074 | }
|
3075 | this.$tableBorder.css('width', `${$bodyTable.outerWidth()}px`)
|
3076 | this.$tableBorder.css('height', `${tableBorderHeight}px`)
|
3077 | }
|
3078 | }
|
3079 |
|
3080 | if (this.options.cardView) {
|
3081 |
|
3082 | this.$el.css('margin-top', '0')
|
3083 | this.$tableContainer.css('padding-bottom', '0')
|
3084 | this.$tableFooter.hide()
|
3085 | } else {
|
3086 |
|
3087 | this.getCaret()
|
3088 | this.$tableContainer.css('padding-bottom', `${padding}px`)
|
3089 | }
|
3090 |
|
3091 | this.trigger('reset-view')
|
3092 | }
|
3093 |
|
3094 | showLoading () {
|
3095 | this.$tableLoading.toggleClass('open', true)
|
3096 |
|
3097 | let fontSize = this.options.loadingFontSize
|
3098 |
|
3099 | if (this.options.loadingFontSize === 'auto') {
|
3100 | fontSize = this.$tableLoading.width() * 0.04
|
3101 | fontSize = Math.max(12, fontSize)
|
3102 | fontSize = Math.min(32, fontSize)
|
3103 | fontSize = `${fontSize}px`
|
3104 | }
|
3105 |
|
3106 | this.$tableLoading.find('.loading-text').css('font-size', fontSize)
|
3107 | }
|
3108 |
|
3109 | hideLoading () {
|
3110 | this.$tableLoading.toggleClass('open', false)
|
3111 | }
|
3112 |
|
3113 | togglePagination () {
|
3114 | this.options.pagination = !this.options.pagination
|
3115 |
|
3116 | const icon = this.options.showButtonIcons ? this.options.pagination ? this.options.icons.paginationSwitchDown : this.options.icons.paginationSwitchUp : ''
|
3117 | const text = this.options.showButtonText ? this.options.pagination ? this.options.formatPaginationSwitchUp() : this.options.formatPaginationSwitchDown() : ''
|
3118 |
|
3119 | this.$toolbar.find('button[name="paginationSwitch"]')
|
3120 | .html(`${Utils.sprintf(this.constants.html.icon, this.options.iconsPrefix, icon) } ${ text}`)
|
3121 | this.updatePagination()
|
3122 | this.trigger('toggle-pagination', this.options.pagination)
|
3123 | }
|
3124 |
|
3125 | toggleFullscreen () {
|
3126 | this.$el.closest('.bootstrap-table').toggleClass('fullscreen')
|
3127 | this.resetView()
|
3128 | }
|
3129 |
|
3130 | toggleView () {
|
3131 | this.options.cardView = !this.options.cardView
|
3132 | this.initHeader()
|
3133 |
|
3134 | const icon = this.options.showButtonIcons ? this.options.cardView ? this.options.icons.toggleOn : this.options.icons.toggleOff : ''
|
3135 | const text = this.options.showButtonText ? this.options.cardView ? this.options.formatToggleOff() : this.options.formatToggleOn() : ''
|
3136 |
|
3137 | this.$toolbar.find('button[name="toggle"]')
|
3138 | .html(`${Utils.sprintf(this.constants.html.icon, this.options.iconsPrefix, icon) } ${ text}`)
|
3139 | this.initBody()
|
3140 | this.trigger('toggle', this.options.cardView)
|
3141 | }
|
3142 |
|
3143 | resetSearch (text) {
|
3144 | const $search = Utils.getSearchInput(this)
|
3145 |
|
3146 | const textToUse = text || ''
|
3147 |
|
3148 | $search.val(textToUse)
|
3149 | this.searchText = textToUse
|
3150 | this.onSearch({ currentTarget: $search }, false)
|
3151 | }
|
3152 |
|
3153 | filterBy (columns, options) {
|
3154 | this.filterOptions = Utils.isEmptyObject(options) ? this.options.filterOptions : $.extend(this.options.filterOptions, options)
|
3155 | this.filterColumns = Utils.isEmptyObject(columns) ? {} : columns
|
3156 | this.options.pageNumber = 1
|
3157 | this.initSearch()
|
3158 | this.updatePagination()
|
3159 | }
|
3160 |
|
3161 | scrollTo (params) {
|
3162 | let options = { unit: 'px', value: 0 }
|
3163 |
|
3164 | if (typeof params === 'object') {
|
3165 | options = Object.assign(options, params)
|
3166 | } else if (typeof params === 'string' && params === 'bottom') {
|
3167 | options.value = this.$tableBody[0].scrollHeight
|
3168 | } else if (typeof params === 'string' || typeof params === 'number') {
|
3169 | options.value = params
|
3170 | }
|
3171 |
|
3172 | let scrollTo = options.value
|
3173 |
|
3174 | if (options.unit === 'rows') {
|
3175 | scrollTo = 0
|
3176 | this.$body.find(`> tr:lt(${options.value})`).each((i, el) => {
|
3177 | scrollTo += $(el).outerHeight(true)
|
3178 | })
|
3179 | }
|
3180 |
|
3181 | this.$tableBody.scrollTop(scrollTo)
|
3182 | }
|
3183 |
|
3184 | getScrollPosition () {
|
3185 | return this.$tableBody.scrollTop()
|
3186 | }
|
3187 |
|
3188 | selectPage (page) {
|
3189 | if (page > 0 && page <= this.options.totalPages) {
|
3190 | this.options.pageNumber = page
|
3191 | this.updatePagination()
|
3192 | }
|
3193 | }
|
3194 |
|
3195 | prevPage () {
|
3196 | if (this.options.pageNumber > 1) {
|
3197 | this.options.pageNumber--
|
3198 | this.updatePagination()
|
3199 | }
|
3200 | }
|
3201 |
|
3202 | nextPage () {
|
3203 | if (this.options.pageNumber < this.options.totalPages) {
|
3204 | this.options.pageNumber++
|
3205 | this.updatePagination()
|
3206 | }
|
3207 | }
|
3208 |
|
3209 | toggleDetailView (index, _columnDetailFormatter) {
|
3210 | const $tr = this.$body.find(Utils.sprintf('> tr[data-index="%s"]', index))
|
3211 |
|
3212 | if ($tr.next().is('tr.detail-view')) {
|
3213 | this.collapseRow(index)
|
3214 | } else {
|
3215 | this.expandRow(index, _columnDetailFormatter)
|
3216 | }
|
3217 |
|
3218 | this.resetView()
|
3219 | }
|
3220 |
|
3221 | expandRow (index, _columnDetailFormatter) {
|
3222 | const row = this.data[index]
|
3223 | const $tr = this.$body.find(Utils.sprintf('> tr[data-index="%s"][data-has-detail-view]', index))
|
3224 |
|
3225 | if (this.options.detailViewIcon) {
|
3226 | $tr.find('a.detail-icon').html(Utils.sprintf(this.constants.html.icon, this.options.iconsPrefix, this.options.icons.detailClose))
|
3227 | }
|
3228 |
|
3229 | if ($tr.next().is('tr.detail-view')) {
|
3230 | return
|
3231 | }
|
3232 |
|
3233 | $tr.after(Utils.sprintf('<tr class="detail-view"><td colspan="%s"></td></tr>', $tr.children('td').length))
|
3234 |
|
3235 | const $element = $tr.next().find('td')
|
3236 |
|
3237 | const detailFormatter = _columnDetailFormatter || this.options.detailFormatter
|
3238 | const content = Utils.calculateObjectValue(this.options, detailFormatter, [index, row, $element], '')
|
3239 |
|
3240 | if ($element.length === 1) {
|
3241 | $element.append(content)
|
3242 | }
|
3243 |
|
3244 | this.trigger('expand-row', index, row, $element)
|
3245 | }
|
3246 |
|
3247 | expandRowByUniqueId (uniqueId) {
|
3248 | const row = this.getRowByUniqueId(uniqueId)
|
3249 |
|
3250 | if (!row) {
|
3251 | return
|
3252 | }
|
3253 |
|
3254 | this.expandRow(this.data.indexOf(row))
|
3255 | }
|
3256 |
|
3257 | collapseRow (index) {
|
3258 | const row = this.data[index]
|
3259 | const $tr = this.$body.find(Utils.sprintf('> tr[data-index="%s"][data-has-detail-view]', index))
|
3260 |
|
3261 | if (!$tr.next().is('tr.detail-view')) {
|
3262 | return
|
3263 | }
|
3264 |
|
3265 | if (this.options.detailViewIcon) {
|
3266 | $tr.find('a.detail-icon').html(Utils.sprintf(this.constants.html.icon, this.options.iconsPrefix, this.options.icons.detailOpen))
|
3267 | }
|
3268 |
|
3269 | this.trigger('collapse-row', index, row, $tr.next())
|
3270 | $tr.next().remove()
|
3271 | }
|
3272 |
|
3273 | collapseRowByUniqueId (uniqueId) {
|
3274 | const row = this.getRowByUniqueId(uniqueId)
|
3275 |
|
3276 | if (!row) {
|
3277 | return
|
3278 | }
|
3279 |
|
3280 | this.collapseRow(this.data.indexOf(row))
|
3281 | }
|
3282 |
|
3283 | expandAllRows () {
|
3284 | const trs = this.$body.find('> tr[data-index][data-has-detail-view]')
|
3285 |
|
3286 | for (let i = 0; i < trs.length; i++) {
|
3287 | this.expandRow($(trs[i]).data('index'))
|
3288 | }
|
3289 | }
|
3290 |
|
3291 | collapseAllRows () {
|
3292 | const trs = this.$body.find('> tr[data-index][data-has-detail-view]')
|
3293 |
|
3294 | for (let i = 0; i < trs.length; i++) {
|
3295 | this.collapseRow($(trs[i]).data('index'))
|
3296 | }
|
3297 | }
|
3298 |
|
3299 | updateColumnTitle (params) {
|
3300 | if (!params.hasOwnProperty('field') || !params.hasOwnProperty('title')) {
|
3301 | return
|
3302 | }
|
3303 |
|
3304 | this.columns[this.fieldsColumnsIndex[params.field]].title =
|
3305 | this.options.escape ? Utils.escapeHTML(params.title) : params.title
|
3306 |
|
3307 | if (this.columns[this.fieldsColumnsIndex[params.field]].visible) {
|
3308 | this.$header.find('th[data-field]').each((i, el) => {
|
3309 | if ($(el).data('field') === params.field) {
|
3310 | $($(el).find('.th-inner')[0]).text(params.title)
|
3311 | return false
|
3312 | }
|
3313 | })
|
3314 |
|
3315 | this.resetView()
|
3316 | }
|
3317 | }
|
3318 |
|
3319 | updateFormatText (formatName, text) {
|
3320 | if (!/^format/.test(formatName) || !this.options[formatName]) {
|
3321 | return
|
3322 | }
|
3323 | if (typeof text === 'string') {
|
3324 | this.options[formatName] = () => text
|
3325 | } else if (typeof text === 'function') {
|
3326 | this.options[formatName] = text
|
3327 | }
|
3328 | this.initToolbar()
|
3329 | this.initPagination()
|
3330 | this.initBody()
|
3331 | }
|
3332 | }
|
3333 |
|
3334 | BootstrapTable.VERSION = Constants.VERSION
|
3335 | BootstrapTable.DEFAULTS = Constants.DEFAULTS
|
3336 | BootstrapTable.LOCALES = Constants.LOCALES
|
3337 | BootstrapTable.COLUMN_DEFAULTS = Constants.COLUMN_DEFAULTS
|
3338 | BootstrapTable.METHODS = Constants.METHODS
|
3339 | BootstrapTable.EVENTS = Constants.EVENTS
|
3340 |
|
3341 |
|
3342 |
|
3343 |
|
3344 | $.BootstrapTable = BootstrapTable
|
3345 | $.fn.bootstrapTable = function (option, ...args) {
|
3346 | let value
|
3347 |
|
3348 | this.each((i, el) => {
|
3349 | let data = $(el).data('bootstrap.table')
|
3350 | const options = $.extend({}, BootstrapTable.DEFAULTS, $(el).data(),
|
3351 | typeof option === 'object' && option)
|
3352 |
|
3353 | if (typeof option === 'string') {
|
3354 | if (!Constants.METHODS.includes(option)) {
|
3355 | throw new Error(`Unknown method: ${option}`)
|
3356 | }
|
3357 |
|
3358 | if (!data) {
|
3359 | return
|
3360 | }
|
3361 |
|
3362 | value = data[option](...args)
|
3363 |
|
3364 | if (option === 'destroy') {
|
3365 | $(el).removeData('bootstrap.table')
|
3366 | }
|
3367 | }
|
3368 |
|
3369 | if (!data) {
|
3370 | data = new $.BootstrapTable(el, options)
|
3371 | $(el).data('bootstrap.table', data)
|
3372 | data.init()
|
3373 | }
|
3374 | })
|
3375 |
|
3376 | return typeof value === 'undefined' ? this : value
|
3377 | }
|
3378 |
|
3379 | $.fn.bootstrapTable.Constructor = BootstrapTable
|
3380 | $.fn.bootstrapTable.theme = Constants.THEME
|
3381 | $.fn.bootstrapTable.VERSION = Constants.VERSION
|
3382 | $.fn.bootstrapTable.defaults = BootstrapTable.DEFAULTS
|
3383 | $.fn.bootstrapTable.columnDefaults = BootstrapTable.COLUMN_DEFAULTS
|
3384 | $.fn.bootstrapTable.events = BootstrapTable.EVENTS
|
3385 | $.fn.bootstrapTable.locales = BootstrapTable.LOCALES
|
3386 | $.fn.bootstrapTable.methods = BootstrapTable.METHODS
|
3387 | $.fn.bootstrapTable.utils = Utils
|
3388 |
|
3389 |
|
3390 |
|
3391 |
|
3392 | $(() => {
|
3393 | $('[data-toggle="table"]').bootstrapTable()
|
3394 | })
|
3395 |
|
3396 | export default BootstrapTable
|