-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorTable.ts
More file actions
2177 lines (1856 loc) · 63.6 KB
/
ColorTable.ts
File metadata and controls
2177 lines (1856 loc) · 63.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Project: ColorTable
* File: DataTable
* Created by Pennycodes on 6/1/2022.
* Copyright ColorTable
*/
interface AjaxOptions {
url: string | null
serverPaging?: boolean
refresh?: number
timeout?: number
type?: 'post' | 'get' | 'POST' | 'GET'
filterInput?: 'client' | 'server'
filterSelect?: 'client' | 'server'
}
interface SyncData {
data: Array<any>,
toAdd: Array<any>,
toUpdate: Object,
toDelete: Array<any>
}
interface Options {
pageSize: number
filterText: string
filterInputClass: string
counterDivSelector: string
loadingDivSelector: string
pagingDivSelector: string
data?: AjaxOptions | any
nbColumns: number
forceStrings: boolean
tableClass: string
empty: string
loadingActiveClass: string
pagingListClass: string
pagingDivClass: string
pagingItemClass: string
pagingLinkClass: string
pagingLinkHref: string
pagingLinkDisabledTabIndex: boolean
sort: Array<any> | Function | "*" | boolean | Object
sortKey: boolean | number |string
sortDir: 'asc' | 'desc'
pagingNumberOfPages: number
identify: boolean | Function | string | number
onChange: Function
counterText: Function
firstPage: string
prevPage: string
pagingPages: Function | boolean
nextPage: string
lastPage: string
dataTypes: boolean | string[] | boolean[]
filters: Array<any> | "*"
filterEmptySelect: string
filterSelectOptions: boolean
filterSelectClass: string
beforeRefresh: Function
afterRefresh: Function
lineFormat: (id: string, data: Array<any>) => HTMLElement
}
class ColorTable {
options: Options
table: HTMLTableElement
totalPage: number
serverPaging: boolean
currentStart: number
filterIndex: []
pagingDivs: NodeListOf<HTMLElement>
pagingLists: NodeListOf<HTMLElement>
counterDivs: NodeListOf<HTMLElement>
loadingDiv: HTMLElement
ths: HTMLCollectionOf<HTMLTableCellElement>
data: Array<any>
refreshTimeOut: number
syncData?: SyncData
filters?: Array<any>
filterTags?: Array<any>
filterValues?: Array<any>
sortListener?: EventListenerOrEventListenerObject
constructor(table: HTMLTableElement, options: Options) {
this.table = table
this.options = ColorTable.DefaultOptions()
for (const k in ColorTable.DefaultOptions()) {
if (ColorTable.DefaultOptions().hasOwnProperty(k)) {
if (options.hasOwnProperty(k)) {
this.options[k] = options[k]
}
else {
this.options[k] = ColorTable.DefaultOptions()[k]
}
}
}
if (options.hasOwnProperty('data')) {
this.options.data = options.data
}
/* If nb columns not specified, count the number of column from thead. */
if (this.options.nbColumns < 0) {
this.options.nbColumns = this.table.tHead.rows[0].cells.length
}
/* Create the base for pagination. */
this.pagingDivs = document.querySelectorAll(this.options.pagingDivSelector)
for (let i = 0; i < this.pagingDivs.length; ++i) {
const div = this.pagingDivs[i]
this.addClass(div, 'pagination-datatables')
this.addClass(div, this.options.pagingDivClass)
const ul = document.createElement('ul')
this.addClass(ul, this.options.pagingListClass)
div.appendChild(ul)
}
this.pagingLists = document.querySelectorAll(this.options.pagingDivSelector + ' ul')
this.counterDivs = document.querySelectorAll(this.options.counterDivSelector)
this.loadingDiv = document.querySelector(this.options.loadingDivSelector)
if (!this.table.tHead) {
this.table.tHead = document.createElement('thead')
this.table.appendChild(this.table.rows[0])
}
/* Compatibility issue (forceStrings => No defined data types). */
if (this.options.forceStrings) {
this.options.dataTypes = false
}
this.ths = this.table.tHead.rows[0].cells
if (!this.table.tBodies[0]) {
this.table.tBodies[0] = document.createElement('tbody')
}
if (this.options.data instanceof Array) {
this.data = this.options.data
}
else if (this.options.data instanceof Object) {
const ajaxOptions = ColorTable.DefaultAjaxOptions()
for (const k in this.options.data) {
ajaxOptions[k] = this.options.data[k]
}
this.options.data = ajaxOptions
const fetchData = true
if (fetchData) {
this.data = []
if (this.options.data.serverPaging) {
this.serverPaging = true
}
this.loadingDiv.classList.remove(this.options.loadingActiveClass)
this.getAjaxDataAsync(0)
}
}
else if (this.table.tBodies[0].rows.length > 0) {
let i
this.data = []
const rows = this.table.tBodies[0].rows
const nCols = rows[0].cells.length
for (i = 0; i < rows.length; ++i) {
this.data.push ([])
}
for (let j = 0 ; j < nCols ; ++j) {
let dt: any = function (x) {
return x
}
if (this.options.dataTypes instanceof Array) {
switch (this.options.dataTypes[j]) {
case 'int':
dt = parseInt
break
case 'float':
case 'double':
dt = parseFloat
break
case 'date':
case 'datetime':
dt = function (x) { return new Date(x) }
break
case false:
case true:
case 'string':
case 'str':
dt = function (x) { return x }
break
default:
dt = this.options.dataTypes[j]
}
}
for (i = 0; i < rows.length; ++i) {
this.data[i].push(dt(rows[i].cells[j].innerHTML.trim()))
}
}
if (this.options.dataTypes === true) {
for (let c = 0; c < this.data[0].length; ++c) {
let isNumeric = true
for (i = 0; i < this.data.length; ++i) {
if (this.data[i][c] !== ""
&& !((this.data[i][c] - parseFloat(this.data[i][c]) + 1) >= 0)) {
isNumeric = false
}
}
if (isNumeric) {
for (i = 0; i < this.data.length; ++i) {
if (this.data[i][c] !== "") {
this.data[i][c] = parseFloat(this.data[i][c])
}
}
}
}
}
}
/* Add sorting class to all th and add callback. */
this.createSort()
/* Add filter where it's needed. */
this.createFilter()
this.triggerSort()
this.filter()
}
/**
*
* Add the specified class(es) to the specified DOM Element.
*
* @param node The DOM Element
* @param classes (Array or String)
*
**/
private addClass(node: HTMLElement, classes: string | string[]) {
if (typeof classes === "string") {
classes = classes.split(' ')
}
classes.forEach(function (c) {
if (!c) return
node.classList.add(c)
})
}
/**
*
* Remove the specified node from the DOM.
*
* @param node The node to removes
*
**/
private static removeNode(node: HTMLElement) {
if (node) node.parentNode.removeChild(node)
}
/**
*
* Set timeout (if specified) for refresh.
*
* Note: This function should be call when the ajax loading is finished.
*
* @update refreshTimeOut The new timeout
*
**/
private setRefreshTimeout() {
if (this.options.data.refresh) {
clearTimeout(this.refreshTimeOut)
this.refreshTimeOut = setTimeout((function (datatable) {
return function () { datatable.getAjaxDataAsync(0) }
})(this), this.options.data.refresh)
}
}
/**
*
* Hide the loading div.
* Note: Call setRefreshTimeout & hideLoadingDiv if all the data have been loaded.
*
**/
private hideLoadingDiv() {
this.setRefreshTimeout()
this.loadingDiv.classList.remove(this.options.loadingActiveClass)
}
/**
*
* Show the loading div for ajax requests
**/
private showLoadingDiv() {
this.loadingDiv.classList.add(this.options.loadingActiveClass)
}
/**
*
* Get data according to this.options.data, asynchronously.
*
* @param start The first offset to send to the server
*
* @update data Concat/set data received from server to old data
*
* Note: Each call increment start by pageSize.
*
**/
private getAjaxDataAsync(start: number) {
this.showLoadingDiv()
const xhr = new XMLHttpRequest()
xhr.timeout = this.options.data.timeout
xhr.onreadystatechange = function (datatable) {
return function () {
if (this.readyState === 4) {
switch (this.status) {
case 200:
case 201:
if(datatable.serverPaging) {
datatable.totalPage = this.response.total
datatable.data = this.response.data
}
else {
datatable.data = datatable.data.concat(this.response)
}
datatable.hideLoadingDiv()
datatable.sort(true)
datatable.updateFilter()
break
default:
datatable.hideLoadingDiv()
console.error("ERROR: " + this.status + " - " + this.statusText)
console.log(xhr)
alert('An error occurred. Check console for details')
break
}
}
}
} (this)
let url = this.options.data.url
const limit = this.options.pageSize
const formData = new FormData()
if (start > -1 && this.serverPaging) {
if (this.options.data.type.toUpperCase() == 'GET') {
url += '?start=' + start + '&limit=' + limit
}
else {
formData.append('start', start.toString())
formData.append('limit', limit.toString())
}
}
xhr.open(this.options.data.type, url, true)
xhr.responseType = 'json'
xhr.send(formData)
}
/**
*
* @return The last page number according to options.pageSize and
* current number of filtered elements.
*
**/
private getLastPageNumber() {
let lastPage = this.serverPaging ? this.totalPage : this.filterIndex.length
return parseInt(String(Math.ceil(lastPage / this.options.pageSize)), 10)
}
/**
* Creating Pagination Links
* @param content The link content, this is the page numbers
* @param page the indexing page
* @param disabled disable page link
* @private
*/
private createPagingLink (content: string, page: string, disabled: boolean) {
const link = document.createElement('a')
// Check options...
if (this.options.pagingLinkClass) {
link.classList.add(this.options.pagingLinkClass)
}
if (this.options.pagingLinkHref) {
link.href = this.options.pagingLinkHref
}
if (this.options.pagingLinkDisabledTabIndex !== false && disabled) {
link.tabIndex = Number(this.options.pagingLinkDisabledTabIndex)
}
link.dataset.page = page
link.innerHTML = content
return link
}
/**
*
* Update the paging divs.
*
**/
private updatePaging() {
/* Be careful if you change something here, all this part calculate the first
and last page to display */
const totalPage = this.serverPaging ? this.totalPage : this.filterIndex.length
const nbPages = this.options.pagingNumberOfPages
const dataTable = this
const cp = parseInt(String(this.currentStart / this.options.pageSize), 10) + 1
const lp = this.getLastPageNumber()
let start
let end
const first = totalPage ? this.currentStart + 1 : 0
const last = (this.currentStart + this.options.pageSize) > totalPage ?
totalPage : this.currentStart + this.options.pageSize
if (cp < nbPages / 2) {
start = 1
}
else if (cp >= lp - nbPages / 2) {
start = lp - nbPages + 1
if (start < 1) {
start = 1
}
}
else {
start = parseInt(String(cp - nbPages / 2 + 1), 10)
}
if (start + nbPages < lp + 1) {
end = start + nbPages - 1
}
else {
end = lp
}
/* Just iterate over each paging list and append li to ul. */
let li
for (let i = 0; i < this.pagingLists.length; ++i) {
let children = []
if (dataTable.options.firstPage) {
li = document.createElement('li')
li.appendChild(dataTable.createPagingLink(
dataTable.options.firstPage, "first", cp === 1
))
if (cp === 1) li.classList.add('active')
children.push(li)
}
if (dataTable.options.prevPage) {
li = document.createElement('li')
li.appendChild(dataTable.createPagingLink(
dataTable.options.prevPage, "prev", cp === 1
))
if (cp === 1) li.classList.add('active')
children.push(li)
}
if (dataTable.options.pagingPages) {
if (typeof this.options.pagingPages !== "boolean") {
const _children = this.options.pagingPages.call(this.table, start,
end, cp, first, last)
if (_children instanceof Array) {
children = children.concat(_children)
}
else {
children.push(_children)
}
}
}
else {
for (let k = start; k <= end; k++) {
li = document.createElement('li')
li.appendChild(dataTable.createPagingLink(
k, k, cp === k
))
if (k === cp) li.classList.add('active')
children.push(li)
}
}
if (dataTable.options.nextPage) {
li = document.createElement('li')
li.appendChild(dataTable.createPagingLink(
dataTable.options.nextPage, "next", cp === lp || lp === 0
))
if (cp === lp || lp === 0) li.classList.add('active')
children.push(li)
}
if (dataTable.options.lastPage) {
li = document.createElement('li')
li.appendChild(dataTable.createPagingLink(
dataTable.options.lastPage, "last", cp === lp || lp === 0
))
if (cp === lp || lp === 0) li.classList.add('active')
children.push(li)
}
this.pagingLists[i].innerHTML = ''
children.forEach(function (e) {
if (dataTable.options.pagingItemClass) {
e.classList.add(dataTable.options.pagingItemClass)
}
if (e.childNodes.length > 0) {
e.childNodes[0].addEventListener('click', function (event) {
event.preventDefault()
if (this.parentNode.classList.contains('active') ||
typeof this.dataset.page === 'undefined') {
return
}
switch (this.dataset.page) {
case 'first':
dataTable.loadPage(1)
break
case 'prev':
dataTable.loadPage(cp - 1)
break
case 'next':
dataTable.loadPage(cp + 1)
break
case 'last':
dataTable.loadPage(lp)
break
default:
dataTable.loadPage(parseInt(this.dataset.page, 10))
}
}, false)
}
this.pagingLists[i].appendChild(e)
}, this)
}
}
/**
*
* Update the counter divs.
*
**/
private updateCounter() {
const totalPage = this.serverPaging ? this.totalPage : this.filterIndex.length
const cp = totalPage ?
parseInt(String(this.currentStart / this.options.pageSize), 10) + 1 : 0
const lp = parseInt(String(Math.ceil(totalPage / this.options.pageSize)), 10)
const first = totalPage ? this.currentStart + 1 : 0
const last = (this.currentStart + this.options.pageSize) > totalPage ?
totalPage : this.currentStart + this.options.pageSize
for (let i = 0; i < this.counterDivs.length; ++i) {
this.counterDivs[i].innerHTML = this.options.counterText.call(this.table, cp, lp,
first, last,
totalPage,
this.serverPaging ? this.totalPage : this.data.length )
}
}
/**
*
* @return The sort function according to options. Sort, options.sortKey & options.sortDir.
*
* Note: This function could return false if no sort function can be generated.
*
**/
private getSortFunction() {
if (!this.options.sort) return false
if (this.options.sort instanceof Function) return this.options.sort
if (this.data.length === 0 || !(typeof this.options.sortKey !== "boolean" && this.options.sortKey in this.data[0])) return false
const key = this.options.sortKey
const asc = this.options.sortDir === 'asc'
if (this.options.sort[key] instanceof Function) {
return function (s) {
return function (a, b) {
const valA = a[key], valB = b[key];
return asc ? s(valA, valB) : -s(valA, valB)
};
} (this.options.sort[key])
}
return function (a, b) {
const ValA = a[key], ValB = b[key];
if (ValA > ValB) return asc ? 1 : -1
if (ValA < ValB) return asc ? -1 : 1
return 0
}
}
/**
*
* Destroy the filters (remove the filter line).
*
**/
private destroyFilter() {
ColorTable.removeNode(this.table.querySelector('.datatable-filter-line'))
}
/**
*
* Change the text input filter placeholder according to this.options.filterText.
*
**/
private changePlaceHolder() {
const placeholder = this.options.filterText ? this.options.filterText : ''
const inputTexts = this.table.querySelectorAll<HTMLInputElement>('.datatable-filter-line input[type="text"]')
for (let i = 0; i < inputTexts.length; ++i) {
inputTexts[i].placeholder = placeholder
}
}
/**
*
* Create a text filter for the specified field.
*
* @param field The field corresponding to the filter
*
* @update filters Add the new filter to the list of filter (calling addFilter)
*
* @return The input filter
*
**/
private createTextFilter (field) {
const opt = this.options.filters[field]
const input = opt instanceof HTMLInputElement ? opt : document.createElement('input')
input.type = 'text'
if (this.options.filterText) {
input.placeholder = this.options.filterText
}
this.addClass(input, 'datatable-filter datatable-input-text')
input.dataset.filter = field
this.filterValues[field] = ''
const typeWatch = (function () {
let timer = 0
return function (callback, ms) {
clearTimeout(timer)
timer = setTimeout(callback, ms)
}
})()
input.onkeyup = function (datatable) {
return function () {
// @ts-ignore
const val = this.value.toUpperCase()
// @ts-ignore
const field = this.dataset.filter
typeWatch(function () {
datatable.filterValues[field] = val
if (datatable.serverPaging && datatable.options.data.filterInput === 'server') {
datatable.search()
}
else {
datatable.filter()
}
}, 300)
}
} (this)
input.onkeydown = input.onkeyup
const regexp = opt === 'regexp' || input.dataset.regexp
if (opt instanceof Function) {
this.addFilter(field, opt)
}
else if (regexp) {
this.addFilter(field, function (data, val) {
return new RegExp(val).test(String(data))
})
}
else {
this.addFilter(field, function (data, val) {
return String(data).toUpperCase().indexOf(val) !== -1
})
}
this.addClass(input, this.options.filterInputClass)
return input
}
/**
* Check if the specified value is in the specified array, without strict type checking.
*
* @param value The val to search
* @param array The array
*
* @return true if the value was found in the array
**/
private static _isIn (value, array) {
let found = false
for (let i = 0; i < array.length && !found; ++i) {
found = array[i] == value
}
return found
}
/**
* Return the index of the specified element in the object.
*
* @param v
* @param a
*
* @return The index, or -1
**/
private static _index (v, a) {
if (a === undefined || a === null) {
return -1
}
let index = -1
for (let i = 0; i < a.length && index == -1; ++i) {
if (a[i] === v) index = i
}
return index
}
/**
* Return the keys of the specified object.
*
* @param obj
*
* @return The keys of the specified object.
**/
private static _keys (obj) {
if (obj === undefined || obj === null) {
return undefined
}
const keys = []
for (const k in obj) {
if (obj.hasOwnProperty(k)) keys.push(k)
}
return keys
}
/**
*
* Create a select filter for the specified field.
*
* @param field The field corresponding to the filter
*
* @update filters Add the new filter to the list of filter (calling addFilter)
*
* @return The select filter.
*
**/
private createSelectFilter(field) {
let option
const opt = this.options.filters[field]
let values: {}, selected = [], multiple = false,
empty = true, emptyValue = this.options.filterEmptySelect
let tag
if (opt instanceof HTMLSelectElement) {
tag = opt
}
else if (opt instanceof Object && 'element' in opt && opt.element) {
tag = opt.element
}
if (opt instanceof HTMLSelectElement || opt === 'select') {
values = this.getFilterOptions(field)
}
else {
multiple = ('multiple' in opt) && (opt.multiple === true)
empty = ('empty' in opt) && opt.empty
emptyValue = (('empty' in opt) && (typeof opt.empty === 'string')) ?
opt.empty : this.options.filterEmptySelect
if ('values' in opt) {
if (opt.values === 'auto') {
values = this.getFilterOptions(field)
}
else {
values = opt.values
}
if ('default' in opt) {
selected = opt['default']
}
else if (multiple) {
selected = []
for (const k in values) {
if (values[k] instanceof Object) {
selected = selected.concat(ColorTable._keys(values[k]))
}
else {
selected.push(k)
}
}
}
else {
selected = []
}
if (!(selected instanceof Array)) {
selected = [selected]
}
}
else {
values = opt
selected = multiple ? ColorTable._keys(values) : []
}
}
const select = tag ? tag : document.createElement('select')
if (multiple) {
select.multiple = true
}
if (opt['default']) {
select.dataset['default'] = opt['default']
}
this.addClass(select, 'datatable-filter datatable-select')
select.dataset.filter = field
if (empty) {
const option = document.createElement('option')
option.dataset.empty = "true"
option.value = ""
option.innerHTML = emptyValue
select.appendChild(option)
}
const allKeys = []
for (const key in values) {
if (values[key] instanceof Object) {
const optgroup = document.createElement('optgroup')
optgroup.label = key
for (const sKey in values[key]) {
if (values[key].hasOwnProperty(sKey)) {
allKeys.push(sKey)
option = document.createElement('option')
option.value = sKey
option.selected = ColorTable._isIn(sKey, selected)
option.innerHTML = values[key][sKey]
optgroup.appendChild(option)
}
}
select.appendChild(optgroup)
}
else {
allKeys.push(key)
option = document.createElement('option')
option.value = key
option.selected = ColorTable._isIn(key, selected)
option.innerHTML = values[key]
select.appendChild(option)
}
}
let val = select.value
if (multiple) {
val = []
for (let i = 0; i < select.options.length; ++i) {
if (select.options[i].selected) val.push(select.options[i].value)
}
}
this.filterValues[field] = multiple ? val : ((empty && !val) ? allKeys : [val])
select.onchange = function (allKeys, multiple, empty, datatable) {
return function () {
let val = this.value
if (multiple) {
val = []
for (let i = 0; i < this.options.length; ++i) {
if (this.options[i].selected) val.push(this.options[i].value)
}
}
const field = this.dataset.filter
datatable.filterValues[field] = multiple ? val : ((empty && !val) ? allKeys : [val])
if (datatable.serverPaging && datatable.options.data.filterSelect === 'server') {
datatable.search()
}
else {
datatable.filter()
}
}
} (allKeys, multiple, empty, this)
if (opt instanceof Object && opt.fn instanceof Function) {
this.addFilter(field, opt.fn)
select.dataset.filterType = 'function'
}
else {
this.addFilter(field, function (aKeys) {
return function (data, val) {
if (!val) return false
if (val == aKeys && !data) return true
return ColorTable._isIn(data, val)
}
} (allKeys))
select.dataset.filterType = 'default'
}
this.addClass(select, this.options.filterSelectClass)
return select
}
/**
*
* Create the filter line according to options.filters.
*
**/
private createFilter() {
this.filters = []
this.filterTags = []
this.filterValues = []
// Special options if '*'
if (this.options.filters === '*') {
let nThs = this.table.tHead.rows[0].cells.length
this.options.filters = []
while (nThs--) {
this.options.filters.push(true)
}
}
if (this.options.filters) {
const tr = document.createElement('tr')
tr.classList.add('datatable-filter-line')
for (const field in this.options.filters) {
if (this.options.filters.hasOwnProperty(field)) {