/
datatable.js
5469 lines (4750 loc) · 206 KB
/
datatable.js
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
/**
* __PrimeFaces DataTable Widget__
*
* DataTable displays data in tabular format.
*
* @typedef {number | JQuery} PrimeFaces.widget.DataTable.RowSpecifier Either the 0-based index of a row, or the row
* element (`TR`) itself.
*
* @typedef {"ASCENDING" | "DESCENDING" | "UNSORTED"} PrimeFaces.widget.DataTable.SortOrder The available sort order
* types for the DataTable.
*
* @typedef {"single" | "multiple"} PrimeFaces.widget.DataTable.CmSelectionMode Indicates whether multiple rows or only
* a single row of a DataTable can be selected.
*
* @typedef {"radio" | "checkbox"} PrimeFaces.widget.DataTable.SelectionMode Indicates whether rows are selected via
* radio buttons or via checkboxes.
*
* @typedef {"single" | "multiple"} PrimeFaces.widget.DataTable.SortMode Indicates whether a DataTable can be sorted
* by multiple columns or only by a single column.
*
* @typedef {"single" | "multiple"} PrimeFaces.widget.DataTable.RowExpandMode Indicates whether multiple columns of a
* DataTable can be expanded at the same time, or whether other expanded rows should be collapsed when a new row is
* expanded.
*
* @typedef {"eager" | "lazy"} PrimeFaces.widget.DataTable.RowEditMode Indicates whether row editors are loaded eagerly
* or on-demand.
*
* @typedef {"eager" | "lazy"} PrimeFaces.widget.DataTable.CellEditMode Indicates whether cell editors are loaded
* eagerly or on-demand.
*
* @typedef {"expand" | "fit"} PrimeFaces.widget.DataTable.ResizeMode Indicates the resize behavior of columns.
*
* @typedef {"new" | "add" | "none"} PrimeFaces.widget.DataTable.SelectionRowMode Indicates how rows of a DataTable
* may be selected, when clicking on the row itself (not the checkbox / radiobutton from p:column).
* `new` always unselects other rows, `add` preserves the currently selected rows, and `none` disables row selection.
*
* @typedef {"cancel" | "save"} PrimeFaces.widget.DataTable.RowEditAction When a row is editable: whether to `save` the
* current contents of the row or `cancel` the row edit and discard all changes.
*
* @typedef PrimeFaces.widget.DataTable.OnRowClickCallback Callback that is invoked when the user clicks on a row of the
* DataTable.
* @param {JQuery.TriggeredEvent} PrimeFaces.widget.DataTable.OnRowClickCallback.event The click event that occurred.
* @param {JQuery} PrimeFaces.widget.DataTable.OnRowClickCallback.row The TR row that was clicked.
*
* @interface {PrimeFaces.widget.DataTable.RowMeta} RowMeta Describes the meta information of row, such as its index and
* its row key.
* @prop {string | undefined} RowMeta.key The unique key of the row. `undefined` when no key was defined for the rows.
* @prop {number} RowMeta.index The 0-based index of the row in the DataTable.
*
*
* @interface {PrimeFaces.widget.DataTable.SortMeta} SortMeta Describes a sorting operation of the DataTable. The
* items of the DataTable may be sorted by multiple column, in which case the sorting operation is describes by a list
* of these objects.
* @prop {string} SortMeta.col ID of the column to sort by.
* @prop {-1 | 1} SortMeta.order Whether to sort the items by the column value in an ascending or descending order.
*
* @implements {PrimeFaces.widget.ContextMenu.ContextMenuProvider<PrimeFaces.widget.DataTable>}
*
* @prop {boolean} allLoadedLiveScroll Whether all available items were already loaded.
* @prop {string} [ascMessage] Localized message for sorting a column in ascending order.
* @prop {JQuery} bodyTable The DOM element for the body part of the table.
* @prop {Record<number, string>} cacheMap Cache for the contents of a row. Key is the row index, value the HTML content
* of the row.
* @prop {number} cacheRows Number of rows to cache.
* @prop {JQuery} checkAllToggler DOM element of the container with the `check all` checkbox in the header.
* @prop {JQuery} clone Clone of the table header.
* @prop {boolean} columnWidthsFixed Whether column widths are fixed or may be resized.
* @prop {JQuery} [contextMenuCell] DOM element of the table cell for which the context menu was opened.
* @prop {PrimeFaces.widget.ContextMenu} contextMenuWidget Widget with the context menu for the DataTable.
* @prop {JQuery} currentCell Current cell to be edited.
* @prop {PrimeFaces.widget.DataTable.RowMeta | null} cursorRowMeta 0-based index of row where the the cursor is located.
* @prop {string} [descMessage] Localized message for sorting a column in descending order.
* @prop {JQuery} dragIndicatorBottom DOM element of the icon that indicates a column is draggable.
* @prop {JQuery} dragIndicatorTop DOM element of the icon that indicates a column is draggable.
* @prop {JQuery} [expansionHolder] DOM element of the hidden input that holds the row keys of the rows that
* are expanded. Used to preserve the expansion state during AJAX updates.
* @prop {number[]} expansionProcess List of row indices to expand.
* @prop {number} filterTimeout ID as returned by `setTimeout` used during filtering.
* @prop {JQuery | null} focusedRow DOM element of the currently focused row.
* @prop {boolean} focusedRowWithCheckbox Whether the focused row includes the checkbox for selecting the row.
* @prop {JQuery} footerCols The DOM elements for the footer columns.
* @prop {JQuery} footerTable The DOM elements for the footer table.
* @prop {JQuery} frozenThead The DOM element for the header THEAD.
* @prop {JQuery} groupResizers The DOM elements for the resizer button of each group.
* @prop {boolean} hasColumnGroup Whether the table has any column groups.
* @prop {JQuery} headerTable The DOM elements for the header table.
* @prop {JQuery} headers DOM elements for the `TH` headers of this DataTable.
* @prop {boolean} ignoreRowHoverEvent Whether to ignore row hover event.
* @prop {boolean} isRTL Whether the writing direction is set to right-to-left.
* @prop {boolean} isRowTogglerClicked Whether a row toggler was clicked.
* @prop {boolean} liveScrollActive Whether live scrolling is currently active.
* @prop {string[]} [loadedExpansionRows] List of row keys of the expansion rows that had their content
* already loaded via AJAX.
* @prop {boolean} loadingLiveScroll Whether data is currently being loaded due to the live scrolling feature.
* @prop {boolean} mousedownOnRow Whether a mousedown event occurred on a row.
* @prop {JQuery} orderStateHolder INPUT element storing the current column / row order.
* @prop {PrimeFaces.widget.DataTable.RowMeta | null} originRowMeta The original row index of the row that was clicked.
* @prop {string} [otherMessage] Localized message for removing the sort order and showing rows in their
* original order.
* @prop {PrimeFaces.widget.Paginator} paginator When pagination is enabled: The paginator widget instance used for
* paging.
* @prop {boolean} percentageScrollHeight The current relative vertical scroll position.
* @prop {boolean} percentageScrollWidth The current relative horizontal scroll position.
* @prop {boolean} reflowDD `true` if reflow is enabled, `false` otherwise.
* @prop {number} relativeHeight The height of the table viewport, relative to the total height, used for scrolling.
* @prop {string[]} resizableState A list with the current widths for each resizable column.
* @prop {JQuery} resizableStateHolder INPUT element storing the current widths for each resizable column.
* @prop {number} resizeTimeout The set-timeout timer ID of the timer used for resizing.
* @prop {JQuery} resizerHelper The DOM element for the resize helper.
* @prop {number} [rowHeight] Constant height in pixels for each row, when virtual scrolling is enabled.
* @prop {string} rowSelector The CSS selector for the table rows.
* @prop {string} rowSelectorForRowClick The CSS selector for the table rows that can be clicked.
* @prop {JQuery} scrollBody The DOM element for the scrollable body of the table.
* @prop {JQuery} scrollFooter The DOM element for the scrollable body of the table.
* @prop {JQuery} scrollFooterBox The DOM element for the scrollable footer box of the table.
* @prop {JQuery} scrollHeader The DOM element for the scrollable header of the table.
* @prop {JQuery} scrollHeaderBox The DOM element for the scrollable header box of the table.
* @prop {number} scrollOffset The current scroll position.
* @prop {JQuery} scrollStateHolder INPUT element storing the current scroll position.
* @prop {JQuery} scrollTbody The DOM element for the scrollable TBODY.
* @prop {number} scrollTimeout The set-timeout timer ID of the timer used for scrolling.
* @prop {string} scrollbarWidth CSS attribute for the scrollbar width, eg. `20px`.
* @prop {string[]} selection List of row keys for the currently selected rows.
* @prop {string} selectionHolder ID of the INPUT element storing the currently selected rows.
* @prop {boolean} shouldLiveScroll Whether live scrolling is currently enabled.
* @prop {Record<string, PrimeFaces.widget.DataTable.SortMeta>} sortMeta Information about how each column is sorted.
* Key is the column key.
* @prop {JQuery} sortableColumns DOM elements for the columns that are sortable.
* @prop {JQuery} stickyContainer The DOM element for the sticky container of the table.
* @prop {JQuery} tbody DOM element of the `TBODY` element of this DataTable, if it exists.
* @prop {JQuery} tfoot DOM element of the `TFOOT` element of this DataTable, if it exists.
* @prop {JQuery} thead DOM element of the `THEAD` element of this DataTable, if it exists.
* @prop {JQuery} theadClone The DOM element for the cloned table head.
* @prop {boolean} virtualScrollActive Whether virtual scrolling is currently active.
*
*
* @interface {PrimeFaces.widget.DataTableCfg} cfg The configuration for the {@link DataTable| DataTable widget}.
* You can access this configuration via {@link PrimeFaces.widget.BaseWidget.cfg|BaseWidget.cfg}. Please note that this
* configuration is usually meant to be read-only and should not be modified.
* @extends {PrimeFaces.widget.DeferredWidgetCfg} cfg
*
* @prop {boolean} cfg.allowUnsorting When true columns can be unsorted upon clicking sort.
* @prop {string} cfg.cellEditMode Defines the cell edit behavior.
* @prop {string} cfg.cellSeparator Separator text to use in output mode of editable cells with multiple components.
* @prop {boolean} cfg.clientCache Caches the next page asynchronously.
* @prop {boolean} cfg.disableContextMenuIfEmpty `true` to disable the context menu when the DataTable has got on
* data row, or `false` otherwise.
* @prop {boolean} cfg.disabledTextSelection Disables text selection on row click.
* @prop {boolean} cfg.draggableColumns Columns can be reordered with drag & drop when enabled.
* @prop {boolean} cfg.draggableRows When enabled, rows can be reordered using drag & drop.
* @prop {string} cfg.editInitEvent Event that triggers row/cell editing.
* @prop {PrimeFaces.widget.DataTable.CellEditMode} cfg.editMode Whether rows may be edited as a whole or whether each
* cell can be edited individually.
* @prop {boolean} cfg.editable Controls in-cell editing.
* @prop {boolean} cfg.expansion `true` if rows are expandable, or `false` otherwise.
* @prop {boolean} cfg.filter `true` if filtering is enabled, or `false` otherwise.
* @prop {number} cfg.filterDelay Delay for filtering in milliseconds.
* @prop {string} cfg.filterEvent Event to invoke filtering for input filters.
* @prop {number} cfg.frozenColumns The number of frozen columns.
* @prop {boolean} cfg.liveResize Columns are resized live in this mode without using a resize helper.
* @prop {boolean} cfg.liveScroll Enables live scrolling.
* @prop {number} cfg.liveScrollBuffer Percentage of the height of the buffer between the bottom of the page and the
* scroll position to initiate the load for the new chunk. This value is in the range `0...100`.
* @prop {boolean} cfg.multiSort `true` if sorting by multiple columns is enabled, or `false` otherwise.
* @prop {boolean} cfg.multiViewState Whether multiple resize mode is enabled.
* @prop {boolean} cfg.nativeElements `true` to use native radio button and checkbox elements, or `false` otherwise.
* @prop {PrimeFaces.widget.DataTable.OnRowClickCallback} cfg.onRowClick Callback that is invoked when the user clicked on
* a row of the DataTable.
* @prop {boolean} cfg.reflow Reflow mode is a responsive mode to display columns as stacked depending on screen size.
* @prop {boolean} cfg.resizableColumns Enables column resizing.
* @prop {PrimeFaces.widget.DataTable.ResizeMode} cfg.resizeMode Defines the resize behavior.
* @prop {string} cfg.rowDragSelector CSS selector for the draggable handle.
* @prop {PrimeFaces.widget.DataTable.RowEditMode} cfg.rowEditMode Defines the row edit.
* @prop {PrimeFaces.widget.DataTable.RowExpandMode} cfg.rowExpandMode Defines row expand mode.
* @prop {boolean} cfg.rowHover Adds hover effect to rows. Hover is always on when selection is enabled.
* @prop {PrimeFaces.widget.DataTable.SelectionRowMode} cfg.selectionRowMode Defines row selection mode when clicking on the row itself.
* @prop {string} cfg.rowSelector CSS selector find finding the rows of this DataTable.
* @prop {boolean} cfg.saveOnCellBlur Saves the changes in cell editing on blur, when set to false changes are
* discarded.
* @prop {string} cfg.scrollHeight Scroll viewport height.
* @prop {number} cfg.scrollLimit Maximum number of rows that may be loaded via live scrolling.
* @prop {number} cfg.scrollStep Number of additional rows to load in each live scroll.
* @prop {string} cfg.scrollWidth Scroll viewport width.
* @prop {boolean} cfg.scrollable Makes data scrollable with fixed header.
* @prop {PrimeFaces.widget.DataTable.SelectionMode} cfg.selectionMode Enables row selection.
* @prop {boolean} cfg.selectionPageOnly When using a paginator and selection mode is `checkbox`, the select all
* checkbox in the header will select all rows on the current page if `true`, or all rows on all pages if `false`.
* Default is `true`.
* @prop {boolean} cfg.sorting `true` if sorting is enabled on the DataTable, `false` otherwise.
* @prop {string[]} cfg.sortMetaOrder IDs of the columns by which to order. Order by the first column, then by the
* second, etc.
* @prop {boolean} cfg.stickyHeader Sticky header stays in window viewport during scrolling.
* @prop {string} cfg.stickyTopAt Selector to position on the page according to other fixing elements on the top of the
* table.
* @prop {string} cfg.tabindex The value of the `tabindex` attribute for this DataTable.
* @prop {boolean} cfg.virtualScroll Loads data on demand as the scrollbar gets close to the bottom.
*
* @interface {PrimeFaces.widget.DataTable.WidthInfo} WidthInfo Describes the width information of a DOM element.
* @prop {number | string} WidthInfo.width The width of the element. It's either a unit-less numeric pixel value or a
* string containing the width including an unit.
* @prop {boolean} WidthInfo.isOuterWidth Tells whether the width includes the border-box or not.
*/
PrimeFaces.widget.DataTable = PrimeFaces.widget.DeferredWidget.extend({
/**
* Map between the sort order names and the multiplier for the comparator.
* @protected
* @type {Record<PrimeFaces.widget.DataTable.SortOrder, -1 | 0 | 1>}
*/
SORT_ORDER: {
ASCENDING: 1,
DESCENDING: -1,
UNSORTED: 0
},
/**
* @override
* @inheritdoc
* @param {PrimeFaces.PartialWidgetCfg<TCfg>} cfg
*/
init: function(cfg) {
this._super(cfg);
this.thead = this.getThead();
this.tbody = this.getTbody();
this.tfoot = this.getTfoot();
if(this.cfg.paginator) {
this.bindPaginator();
}
if(this.cfg.sorting) {
this.bindSortEvents();
}
if(this.cfg.rowHover) {
this.setupRowHover();
}
if(this.cfg.selectionMode) {
this.setupSelection();
}
if(this.cfg.filter) {
this.setupFiltering();
}
if(this.cfg.expansion) {
this.expansionProcess = [];
this.bindExpansionEvents();
}
if(this.cfg.editable) {
this.bindEditEvents();
}
if(this.cfg.draggableRows) {
this.makeRowsDraggable();
}
if(this.cfg.reflow) {
this.initReflow();
}
if(this.cfg.resizableColumns) {
this.resizableStateHolder = $(this.jqId + '_resizableColumnState');
this.resizableState = [];
if(this.resizableStateHolder.attr('value')) {
this.resizableState = this.resizableStateHolder.val().split(',');
}
}
this.updateEmptyColspan();
this.renderDeferred();
},
/**
* @override
* @protected
* @inheritdoc
*/
_render: function() {
var $this = this;
this.isRTL = this.jq.hasClass('ui-datatable-rtl');
this.cfg.partialUpdate = (this.cfg.partialUpdate === false) ? false : true;
if(this.cfg.scrollable) {
this.setupScrolling();
}
if(this.cfg.groupColumnIndexes) {
this.groupRows();
this.bindToggleRowGroupEvents();
}
if(this.cfg.resizableColumns) {
this.setupResizableColumns();
}
if(this.cfg.draggableColumns) {
this.setupDraggableColumns();
}
if(this.cfg.stickyHeader) {
PrimeFaces.queueTask(function () {$this.setupStickyHeader();}, 1);
}
if(this.cfg.onRowClick) {
this.bindRowClick();
}
if(this.cfg.expansion) {
this.initRowExpansion();
this.updateExpandedRowsColspan();
}
if(this.cfg.reflow) {
this.jq.css('visibility', 'visible');
}
},
/**
* Retrieves the table header of this DataTable.
* @return {JQuery} DOM element of the table header.
*/
getThead: function() {
return $(this.jqId + '_head');
},
/**
* Retrieves the table body of this DataTable.
* @return {JQuery} DOM element of the table body.
*/
getTbody: function() {
return $(this.jqId + '_data');
},
/**
* Retrieves the table footer of this DataTable.
* @return {JQuery} DOM element of the table footer.
*/
getTfoot: function() {
return $(this.jqId + '_foot');
},
/**
* Sets the given HTML string as the content of the body of this DataTable. Afterwards, sets up all required event
* listeners etc.
* @protected
* @param {string} data HTML string to set on the body.
* @param {boolean} [clear] Whether the contents of the table body should be removed beforehand.
*/
updateData: function(data, clear) {
var empty = (clear === undefined) ? true: clear;
if(empty)
this.tbody.html(data);
else
this.tbody.append(data);
this.postUpdateData();
},
/**
* Called after an AJAX update. Binds the appropriate event listeners again.
* @private
*/
postUpdateData: function() {
if(this.cfg.draggableRows) {
this.makeRowsDraggable();
}
if(this.cfg.reflow) {
this.initReflow();
}
if(this.cfg.groupColumnIndexes) {
this.groupRows();
this.bindToggleRowGroupEvents();
}
if(this.cfg.expansion) {
this.initRowExpansion();
}
},
/**
* @override
* @inheritdoc
* @param {PrimeFaces.PartialWidgetCfg<TCfg>} cfg
*/
refresh: function(cfg) {
this.columnWidthsFixed = false;
this.ignoreRowHoverEvent = false;
this.unbindEvents();
this._super(cfg);
},
/**
* Removes event listeners needed if refreshing to prevent multiple sort and pagination events.
*
* Cancels all current drag and drop events.
* @private
*/
unbindEvents: function() {
if (this.sortableColumns) {
this.sortableColumns.off();
}
if (this.paginator) {
this.paginator.unbindEvents();
}
// #5582: destroy any current draggable items
if (this.cfg.draggableColumns || this.cfg.draggableRows) {
var dragdrop = $.ui.ddmanager.current;
if (dragdrop && dragdrop.helper) {
var item = dragdrop.currentItem || dragdrop.element;
if(item.closest('.ui-datatable')[0] === this.jq[0]) {
document.body.style.cursor = 'default';
dragdrop.cancel();
}
}
}
},
/**
* Binds the change event listener and renders the paginator
* @private
*/
bindPaginator: function() {
var _self = this;
this.cfg.paginator.paginate = function(newState) {
if(_self.cfg.clientCache) {
_self.loadDataWithCache(newState);
}
else {
_self.paginate(newState);
}
};
this.paginator = new PrimeFaces.widget.Paginator(this.cfg.paginator);
this.paginator.bindSwipeEvents(this.jq, this.cfg);
if(this.cfg.clientCache) {
this.cacheRows = this.paginator.getRows();
var newState = {
first: this.paginator.getFirst(),
rows: this.paginator.getRows(),
page: this.paginator.getCurrentPage()
};
this.clearCacheMap();
this.fetchNextPage(newState);
}
},
/**
* Applies events related to sorting in a non-obtrusive way
* @private
*/
bindSortEvents: function() {
var $this = this,
hasAriaSort = false;
this.cfg.tabindex = this.cfg.tabindex||'0';
this.cfg.multiSort = this.cfg.multiSort||false;
this.cfg.allowUnsorting = this.cfg.allowUnsorting||false;
this.headers = this.thead.find('> tr > th');
this.sortableColumns = this.headers.filter('.ui-sortable-column');
this.sortableColumns.attr('tabindex', this.cfg.tabindex);
//aria messages
this.ascMessage = PrimeFaces.getAriaLabel('datatable.sort.ASC');
this.descMessage = PrimeFaces.getAriaLabel('datatable.sort.DESC');
if (this.cfg.allowUnsorting) {
this.otherMessage = PrimeFaces.getAriaLabel('datatable.sort.NONE');
}
else {
this.otherMessage = PrimeFaces.getAriaLabel('datatable.sort.ASC');
}
//reflow dropdown
this.reflowDD = $(this.jqId + '_reflowDD');
this.sortMeta = [];
for(var i = 0; i < this.sortableColumns.length; i++) {
var columnHeader = this.sortableColumns.eq(i),
columnHeaderId = columnHeader.attr('id'),
sortIcon = columnHeader.children('span.ui-sortable-column-icon'),
sortOrder = null,
resolvedSortMetaIndex = null,
ariaLabel = columnHeader.attr('aria-label');
if (columnHeader.hasClass('ui-state-active')) {
if (sortIcon.hasClass('ui-icon-triangle-1-n')) {
sortOrder = this.SORT_ORDER.ASCENDING;
columnHeader.attr('aria-label', this.getSortMessage(ariaLabel, this.descMessage));
if (!hasAriaSort) {
columnHeader.attr('aria-sort', 'ascending');
hasAriaSort = true;
}
}
else if (sortIcon.hasClass('ui-icon-triangle-1-s')) {
sortOrder = this.SORT_ORDER.DESCENDING;
columnHeader.attr('aria-label', this.getSortMessage(ariaLabel, this.otherMessage));
if (!hasAriaSort) {
columnHeader.attr('aria-sort', 'descending');
hasAriaSort = true;
}
} else {
sortOrder = this.SORT_ORDER.UNSORTED;
columnHeader.attr('aria-label', this.getSortMessage(ariaLabel, this.ascMessage));
if (!hasAriaSort) {
columnHeader.attr('aria-sort', 'other');
hasAriaSort = true;
}
}
if (this.cfg.multiSort && this.cfg.sortMetaOrder) {
resolvedSortMetaIndex = $.inArray(columnHeaderId, this.cfg.sortMetaOrder);
this.sortMeta[resolvedSortMetaIndex] = {
col: columnHeaderId,
order: sortOrder
};
}
$this.updateReflowDD(columnHeader, sortOrder);
}
else {
sortOrder = this.SORT_ORDER.UNSORTED;
columnHeader.attr('aria-label', this.getSortMessage(ariaLabel, this.ascMessage));
if(!hasAriaSort && i == (this.sortableColumns.length - 1)) {
this.sortableColumns.eq(0).attr('aria-sort', 'other');
hasAriaSort = true;
}
}
columnHeader.data('sortorder', sortOrder);
}
this.sortableColumns.on('mouseenter.dataTable', function() {
var column = $(this);
column.addClass('ui-state-hover');
})
.on('mouseleave.dataTable', function() {
var column = $(this);
column.removeClass('ui-state-hover');
})
.on('blur.dataTable', function() {
$(this).removeClass('ui-state-focus');
})
.on('focus.dataTable', function() {
$(this).addClass('ui-state-focus');
})
.on('keydown.dataTable', function(e) {
if((e.key === 'Enter') && $(e.target).is(':not(:input)')) {
$(this).trigger('click.dataTable', (e.metaKey||e.ctrlKey));
e.preventDefault();
}
})
.on('click.dataTable', function(e, metaKeyOn) {
if(!$this.shouldSort(e, this)) {
return;
}
PrimeFaces.clearSelection();
var columnHeader = $(this),
sortOrderData = columnHeader.data('sortorder'),
sortOrder = (sortOrderData === $this.SORT_ORDER.UNSORTED) ? $this.SORT_ORDER.ASCENDING :
(sortOrderData === $this.SORT_ORDER.ASCENDING) ? $this.SORT_ORDER.DESCENDING :
$this.cfg.allowUnsorting ? $this.SORT_ORDER.UNSORTED : $this.SORT_ORDER.ASCENDING,
metaKey = e.metaKey || e.ctrlKey || metaKeyOn;
if(!$this.cfg.multiSort || !metaKey) {
$this.sortMeta = [];
}
$this.addSortMeta({
col: columnHeader.attr('id'),
order: sortOrder
});
$this.sort(columnHeader, sortOrder, $this.cfg.multiSort && metaKey);
if($this.cfg.scrollable) {
$(PrimeFaces.escapeClientId(columnHeader.attr('id') + '_clone')).trigger('focus');
}
$this.updateReflowDD(columnHeader, sortOrder);
});
$this.updateSortPriorityIndicators();
if(this.reflowDD && this.cfg.reflow) {
PrimeFaces.skinSelect(this.reflowDD);
this.reflowDD.on('change', function(e) {
var selectedOption = $(this).find(":selected");
var columnKey = selectedOption.data('columnkey');
var sortOrder = selectedOption.data('sortorder');
var columnHeader = $this.jq.find(PrimeFaces.escapeClientId(columnKey));
columnHeader.data('sortorder', sortOrder);
columnHeader.trigger('click.dataTable');
});
}
},
/**
* Creates the sort order message shown to indicate what the current sort order is.
* @private
* @param {string | undefined} ariaLabel Optional label text from an aria attribute.
* @param {string} sortOrderMessage Sort order message.
* @return {string} The sort order message to use.
*/
getSortMessage: function(ariaLabel, sortOrderMessage) {
var headerName = ariaLabel ? ariaLabel.split(':')[0] : '';
return headerName + ': ' + sortOrderMessage;
},
/**
* Called in response to a click. Checks whether this DataTable should now be sorted. Returns `false` when there
* are no items to be sorted, or when no sorting button was clicked.
* @private
* @param {JQuery.TriggeredEvent} event (Click) event that occurred.
* @param {JQuery} column Column Column of this DataTable on which the event occurred.
* @return {boolean} `true` to perform a sorting operation, `false` otherwise.
*/
shouldSort: function(event, column) {
if(this.isEmpty()) {
return false;
}
var target = $(event.target);
if(target.closest('.ui-column-customfilter', column).length) {
return false;
}
return target.is('th,span');
},
/**
* Adds the given sorting to the list of sort rows. Each sorting describes a column by which to sort. This data
* table may be sorted by multiple columns.
* @private
* @param {PrimeFaces.widget.DataTable.SortMeta} meta Sorting to add.
*/
addSortMeta: function(meta) {
this.sortMeta = $.grep(this.sortMeta, function(value) {
return value.col !== meta.col;
});
this.sortMeta.push(meta);
},
/**
* Binds filter events to standard filters
* @private
*/
setupFiltering: function() {
var $this = this,
filterColumns = this.thead.find('> tr > th.ui-filter-column');
this.cfg.filterEvent = this.cfg.filterEvent||'keyup';
this.cfg.filterDelay = this.cfg.filterDelay||300;
filterColumns.children('.ui-column-filter').each(function() {
var filter = $(this);
if(filter.is("input[type='search']")) {
PrimeFaces.skinInput(filter);
$this.bindTextFilter(filter);
}
else {
PrimeFaces.skinSelect(filter);
$this.bindChangeFilter(filter);
}
});
// ARIA labels for filters
filterColumns.each(function() {
var filterColumn = $(this);
var filter = filterColumn.find(':input');
var title = filterColumn.find('.ui-column-title')
if (filter && title) {
filter.attr('aria-label', PrimeFaces.getLocaleLabel('filter') + " " + title.text());
}
});
},
/**
* Sets up the event listeners for the text filters on a column.
* @private
* @param {JQuery} filter INPUT element of the text filter.
*/
bindTextFilter: function(filter) {
if(this.cfg.filterEvent === 'enter')
this.bindEnterKeyFilter(filter);
else
this.bindFilterEvent(filter);
// #8113 clear 'x' event handler
this.bindClearFilterEvent(filter);
// #7562 draggable columns cannot be filtered with touch
if (PrimeFaces.env.isTouchable(this.cfg)) {
filter.on('touchstart', function(e) {
e.stopPropagation();
});
}
},
/**
* Sets up the change event listeners on the column filter elements.
* @private
* @param {JQuery} filter DOM element of a column filter
*/
bindChangeFilter: function(filter) {
var $this = this;
filter.off('change')
.on('change', function() {
$this.filter();
});
},
/**
* Sets up the enter key event listeners for the text filters on a column.
* @private
* @param {JQuery} filter INPUT element of the text filter.
*/
bindEnterKeyFilter: function(filter) {
var $this = this;
filter.off('keydown').on('keydown', function(e) {
if(PrimeFaces.utils.blockEnterKey(e)) {
$this.filter();
}
});
},
/**
* Sets up the 'search' event which for HTML5 text search fields handles the clear 'x' button.
* @private
* @param {JQuery} filter INPUT element of the text filter.
*/
bindClearFilterEvent: function(filter) {
var $this = this;
filter.off('search').on('search', function(e) {
// only care when 'X'' is clicked
if ($(this).val() == "") {
$this.filter();
}
});
},
/**
* Sets up all event listeners for the given filter element of a column filter.
* @private
* @param {JQuery} filter DOM element of a column filter.
*/
bindFilterEvent: function(filter) {
var $this = this;
var filterEventName = this.cfg.filterEvent + '.dataTable';
filter.on(filterEventName, function(e) {
//prevent form submit on enter key
if (e.key && (PrimeFaces.utils.ignoreFilterKey(e) || PrimeFaces.utils.blockEnterKey(e))) {
return;
}
if($this.filterTimeout) {
clearTimeout($this.filterTimeout);
}
$this.filterTimeout = PrimeFaces.queueTask(function() {
$this.filter();
$this.filterTimeout = null;
}, $this.cfg.filterDelay);
});
},
/**
* Sets up the DataTable and adds all event listeners required for hovering over rows.
* @private
*/
setupRowHover: function() {
var selector = '> tr.ui-widget-content';
if(!this.cfg.selectionMode || this.cfg.selectionMode === 'checkbox') {
this.bindRowHover(selector);
}
},
/**
* Sets up the DataTable and adds all event listener required for selecting rows.
* @private
*/
setupSelection: function() {
var $this = this;
this.selectionHolder = this.jqId + '_selection';
this.cfg.selectionRowMode = this.cfg.selectionRowMode||'new';
this.rowSelector = '> tr.ui-widget-content.ui-datatable-selectable';
this.cfg.disabledTextSelection = this.cfg.disabledTextSelection === false ? false : true;
this.cfg.selectionPageOnly = this.cfg.selectionPageOnly !== false;
this.rowSelectorForRowClick = this.cfg.rowSelector||'td:not(.ui-column-unselectable):not(.ui-grouped-column),span:not(.ui-c)';
var preselection = $(this.selectionHolder).val();
this.selection = !preselection ? [] : preselection.split(',');
// set aria labels
this.tbody.find(this.rowSelector).each(function() {
$this.updateSelectionAria($(this))
});
this.bindSelectionEvents();
//shift key based range selection
if (this.isCheckboxSelectionEnabled()) {
this.originRowMeta = null;
this.cursorRowMeta = null;
}
this.originRowMeta = this.originRowMeta ? this.getRowMeta(this.tbody.find("[data-rk='" + this.originRowMeta.key + "']")) : null;
if (this.cursorRowMeta) {
// #3551 lookup row after update by row key
var cursorRow = this.tbody.find("[data-rk='" + this.cursorRowMeta.key + "']");
this.cursorRowMeta = this.getRowMeta(cursorRow);
if (this.isMultipleSelection() && this.originRowMeta !== null) {
this.selectRowsInRange(cursorRow, true);
}
else {
this.originRowMeta = this.getRowMeta(cursorRow);
this.cursorRowMeta = null;
this.selectRow(cursorRow, true);
}
}
else {
this.cursorRowMeta = null;
}
},
/**
* Applies events related to selection in a non-obtrusive way
* @private
*/
bindSelectionEvents: function() {
if(this.cfg.selectionMode === 'radio') {
this.bindRadioEvents();
if(this.cfg.selectionRowMode !== 'none') {
this.bindRowEvents();
}
else {
this.jq.find('tr.ui-datatable-selectable').css('cursor', 'default');
}
}
else if(this.cfg.selectionMode === 'checkbox') {
this.bindCheckboxEvents();
this.updateHeaderCheckbox();
if(this.cfg.selectionRowMode !== 'none') {
this.bindRowEvents();
}
else {
this.jq.find('tr.ui-datatable-selectable').css('cursor', 'default');
}
}
else {
this.bindRowEvents();
}
},
/**
* Sets up all event listeners for event triggered on a row of this DataTable.
* @private
*/
bindRowEvents: function() {
var $this = this;
this.bindRowHover(this.rowSelector);
this.tbody.off('click.dataTable mousedown.dataTable', this.rowSelector).on('mousedown.dataTable', this.rowSelector, null, function(e) {
$this.mousedownOnRow = true;
})
.on('click.dataTable', this.rowSelector, null, function(e) {
$this.onRowClick(e, this);
$this.mousedownOnRow = false;
});
//double click
if (this.hasBehavior('rowDblselect')) {
this.tbody.off('dblclick.dataTable', this.rowSelector).on('dblclick.dataTable', this.rowSelector, null, function(e) {
$this.onRowDblclick(e, $(this));
});
};
this.bindSelectionKeyEvents();
},
/**
* Sets up all delegated event listeners on the table body.
* @private
*/
bindSelectionKeyEvents: function() {
var $this = this;
this.getFocusableTbody().on('focus', function(e) {
//ignore mouse click on row
if(!$this.mousedownOnRow) {
$this.focusedRow = $this.tbody.children('tr.ui-widget-content.ui-datatable-selectable.ui-state-highlight').eq(0);
if ($this.focusedRow.length == 0) {
$this.focusedRow = $this.tbody.children('tr.ui-widget-content.ui-datatable-selectable').eq(0);
}
$this.highlightFocusedRow();
if($this.cfg.scrollable) {
PrimeFaces.scrollInView($this.scrollBody, $this.focusedRow);
}
}
})
.on('blur', function() {
if($this.focusedRow) {
$this.unhighlightFocusedRow();
$this.focusedRow = null;
}
})
.on('keydown', function(e) {
if($(e.target).is(':input')) {
return;
}
if($this.focusedRow) {
switch(e.code) {
case 'ArrowUp':
case 'ArrowDown':
var rowSelector = 'tr.ui-widget-content.ui-datatable-selectable',
row = e.key === 'ArrowUp' ? $this.focusedRow.prevAll(rowSelector).eq(0) : $this.focusedRow.nextAll(rowSelector).eq(0);
if(row.length) {
$this.unhighlightFocusedRow();
if($this.isCheckboxSelectionEnabled()) {
row.find('> td.ui-selection-column .ui-chkbox input').trigger('focus');
}
else {
$this.focusedRow = row;
}
$this.highlightFocusedRow();
if($this.cfg.scrollable) {
PrimeFaces.scrollInView($this.scrollBody, $this.focusedRow);
}
}
e.preventDefault();
break;
case 'Enter':
case 'NumpadEnter':
case 'Space':
if($this.focusedRowWithCheckbox) {
$this.focusedRow.find('> td.ui-selection-column > div.ui-chkbox > div.ui-chkbox-box').trigger('click.dataTable');
}
else {
e.target = $this.focusedRow.children().eq(0).get(0);
$this.onRowClick(e,$this.focusedRow.get(0));
}
e.preventDefault();
break;
default:
break;
};
}
});
},
/**
* Highlights the currently focused row (if any) by adding the appropriate CSS class.
* @protected
*/
highlightFocusedRow: function() {
this.focusedRow.addClass('ui-state-hover');
},
/**
* Unhighlights the currently focused row (if any) by adding the appropriate CSS class.
* @protected
*/
unhighlightFocusedRow: function() {
this.focusedRow.removeClass('ui-state-hover');
},