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