-
Notifications
You must be signed in to change notification settings - Fork 1k
/
DataTableDoc.vue
executable file
·3095 lines (2905 loc) · 140 KB
/
DataTableDoc.vue
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
<template>
<div class="content-section documentation">
<AppDoc name="DataTableDemo" :sources="sources" :service="['CustomerService']" :data="['customers-large']" github="datatable/DataTableDemo.vue">
<h5>Import</h5>
<pre v-code.script><code>
import DataTable from 'primevue/datatable';
import Column from 'primevue/column';
import ColumnGroup from 'primevue/columngroup'; //optional for column grouping
</code></pre>
<h5>Getting Started</h5>
<p>DataTable requires a value as an array of objects and columns defined with Column component. Throughout the samples, a car interface having vin, brand, year and color properties is used to define an object to be displayed by the datatable.
Cars are loaded by a CarService that connects to a server to fetch the cars with a axios. Note that this is only for demo purposes, DataTable does not have any restrictions on how the data is provided.</p>
<pre v-code.script><code>
import axios from 'axios'
export default class CarService {
getCarsSmall() {
return axios.get('demo/data/cars-small.json').then(res => res.data.data);
}
getCarsMedium() {
return axios.get('demo/data/cars-medium.json').then(res => res.data.data);
}
getCarsLarge() {
return axios.get('demo/data/cars-large.json').then(res => res.data.data);
}
}
</code></pre>
<p>Example response;</p>
<pre v-code.script><code>
{
"data": [
{"brand": "Volkswagen", "year": 2012, "color": "Orange", "vin": "dsad231ff"},
{"brand": "Audi", "year": 2011, "color": "Black", "vin": "gwregre345"},
{"brand": "Renault", "year": 2005, "color": "Gray", "vin": "h354htr"},
{"brand": "BMW", "year": 2003, "color": "Blue", "vin": "j6w54qgh"},
{"brand": "Mercedes", "year": 1995, "color": "Orange", "vin": "hrtwy34"},
{"brand": "Volvo", "year": 2005, "color": "Black", "vin": "jejtyj"},
{"brand": "Honda", "year": 2012, "color": "Yellow", "vin": "g43gr"},
{"brand": "Jaguar", "year": 2013, "color": "Orange", "vin": "greg34"},
{"brand": "Ford", "year": 2000, "color": "Black", "vin": "h54hw5"},
{"brand": "Fiat", "year": 2013, "color": "Red", "vin": "245t2s"}
]
}
</code></pre>
<p>Following sample datatable has 4 columns and retrieves the data from a service on mount.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars">
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
</DataTable>
</template>
</code></pre>
<pre v-code.script><code>
import CarService from '../../service/CarService';
export default {
data() {
return {
cars: null
}
},
carService: null,
created() {
this.carService = new CarService();
},
mounted() {
this.carService.getCarsSmall().then(data => this.cars = data);
}
}
</code></pre>
<h5>Dynamic Columns</h5>
<p>Column components can be dynamically generated using a v-for as well.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars">
<Column v-for="col of columns" :field="col.field" :header="col.header" :key="col.field"></Column>
</DataTable>
</template>
</code></pre>
<pre v-code.script><code>
import CarService from '../../service/CarService';
export default {
data() {
return {
columns: null,
cars: null
}
},
carService: null,
created() {
this.carService = new CarService();
this.columns = [
{field: 'vin', header: 'Vin'},
{field: 'year', header: 'Year'},
{field: 'brand', header: 'Brand'},
{field: 'color', header: 'Color'}
];
},
mounted() {
this.carService.getCarsSmall().then(data => this.cars = data);
}
}
</code></pre>
<h5>Column Component Properties utilized by the DataTable</h5>
<div class="doc-tablewrapper">
<table class="doc-table">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>columnKey</td>
<td>any</td>
<td>null</td>
<td>Identifier of a column if field property is not defined.</td>
</tr>
<tr>
<td>field</td>
<td>string</td>
<td>null</td>
<td>Property represented by the column.</td>
</tr>
<tr>
<td>sortField</td>
<td>string</td>
<td>null</td>
<td>Property name to use in sorting, defaults to field.</td>
</tr>
<tr>
<td>filterField</td>
<td>string</td>
<td>null</td>
<td>Property name to use in filtering, defaults to field.</td>
</tr>
<tr>
<td>sortable</td>
<td>any</td>
<td>false</td>
<td>Defines if a column is sortable.</td>
</tr>
<tr>
<td>header</td>
<td>any</td>
<td>null</td>
<td>Header content of the column.</td>
</tr>
<tr>
<td>footer</td>
<td>any</td>
<td>null</td>
<td>Footer content of the column.</td>
</tr>
<tr>
<td>style</td>
<td>object</td>
<td>null</td>
<td>Inline style of header, body and footer cells.</td>
</tr>
<tr>
<td>class</td>
<td>string</td>
<td>null</td>
<td>Style class of header, body and footer cells.</td>
</tr>
<tr>
<td>headerStyle</td>
<td>object</td>
<td>null</td>
<td>Inline style of the column header.</td>
</tr>
<tr>
<td>headerClass</td>
<td>string</td>
<td>null</td>
<td>Style class of the column header.</td>
</tr>
<tr>
<td>bodyStyle</td>
<td>object</td>
<td>null</td>
<td>Inline style of the column body.</td>
</tr>
<tr>
<td>bodyClass</td>
<td>string</td>
<td>null</td>
<td>Style class of the column body.</td>
</tr>
<tr>
<td>footerStyle</td>
<td>object</td>
<td>null</td>
<td>Inline style of the column footer.</td>
</tr>
<tr>
<td>footerClass</td>
<td>string</td>
<td>null</td>
<td>Style class of the column footer.</td>
</tr>
<tr>
<td>showFilterMenu</td>
<td>boolean</td>
<td>true</td>
<td>Whether to display the filter overlay.</td>
</tr>
<tr>
<td>showFilterOperator</td>
<td>boolean</td>
<td>true</td>
<td>When enabled, match all and match any operator selector is displayed.</td>
</tr>
<tr>
<td>showClearButton</td>
<td>boolean</td>
<td>true</td>
<td>Displays a button to clear the column filtering.</td>
</tr>
<tr>
<td>showApplyButton</td>
<td>boolean</td>
<td>true</td>
<td>Displays a button to apply the column filtering.</td>
</tr>
<tr>
<td>showFilterMatchModes</td>
<td>boolean</td>
<td>true</td>
<td>Whether to show the match modes selector.</td>
</tr>
<tr>
<td>showAddButton</td>
<td>boolean</td>
<td>true</td>
<td>When enabled, a button is displayed to add more rules.</td>
</tr>
<tr>
<td>filterMatchModeOptions</td>
<td>array</td>
<td>null</td>
<td>An array of label-value pairs to override the global match mode options.</td>
</tr>
<tr>
<td>maxConstraints</td>
<td>number</td>
<td>2</td>
<td>Maximum number of constraints for a column filter.</td>
</tr>
<tr>
<td>excludeGlobalFilter</td>
<td>boolean</td>
<td>false</td>
<td>Whether to exclude from global filtering or not.</td>
</tr>
<tr>
<td>filterHeaderStyle</td>
<td>object</td>
<td>null</td>
<td>Inline style of the column filter header in row filter display.</td>
</tr>
<tr>
<td>filterHeaderClass</td>
<td>string</td>
<td>null</td>
<td>Style class of the column filter header in row filter display.</td>
</tr>
<tr>
<td>filterMenuStyle</td>
<td>object</td>
<td>null</td>
<td>Inline style of the column filter overlay.</td>
</tr>
<tr>
<td>filterMenuClass</td>
<td>string</td>
<td>null</td>
<td>Style class of the column filter overlay.</td>
</tr>
<tr>
<td>selectionMode</td>
<td>string</td>
<td>null</td>
<td>Defines column based selection mode, options are "single" and "multiple".</td>
</tr>
<tr>
<td>expander</td>
<td>boolean</td>
<td>false</td>
<td>Displays an icon to toggle row expansion.</td>
</tr>
<tr>
<td>colspan</td>
<td>number</td>
<td>null</td>
<td>Number of columns to span for grouping.</td>
</tr>
<tr>
<td>rowspan</td>
<td>number</td>
<td>null</td>
<td>Number of rows to span for grouping.</td>
</tr>
<tr>
<td>rowReorder</td>
<td>boolean</td>
<td>false</td>
<td>Whether this column displays an icon to reorder the rows.</td>
</tr>
<tr>
<td>rowReorderIcon</td>
<td>string</td>
<td>pi pi-bars</td>
<td>Icon of the drag handle to reorder rows.</td>
</tr>
<tr>
<td>reorderableColumn</td>
<td>boolean</td>
<td>true</td>
<td>Defines if the column itself can be reordered with dragging.</td>
</tr>
<tr>
<td>rowEditor</td>
<td>boolean</td>
<td>false</td>
<td>When enabled, column displays row editor controls.</td>
</tr>
<tr>
<td>frozen</td>
<td>boolean</td>
<td>false</td>
<td>Whether the column is fixed in horizontal scrolling.</td>
</tr>
<tr>
<td>alignFrozen</td>
<td>string</td>
<td>left</td>
<td>Position of a frozen column, valid values are left and right.</td>
</tr>
<tr>
<td>exportable</td>
<td>boolean</td>
<td>true</td>
<td>Whether the column is included in data export.</td>
</tr>
<tr>
<td>filterMatchMode</td>
<td>string</td>
<td>null</td>
<td>Defines the filtering algorithm to use when searching the options.</td>
</tr>
<tr>
<td>hidden</td>
<td>boolean</td>
<td>false</td>
<td>Whether the column is rendered.</td>
</tr>
</tbody>
</table>
</div>
<h5>Column Slots</h5>
<div class="doc-tablewrapper">
<table class="doc-table">
<thead>
<tr>
<th>Name</th>
<th>Parameters</th>
</tr>
</thead>
<tbody>
<tr>
<td>header</td>
<td>column: Column node</td>
</tr>
<tr>
<td>body</td>
<td>data: Row data <br />
column: Column node <br />
field: Column field <br />
index: Row index <br />
frozenRow: Is row frozen</td>
</tr>
<tr>
<td>footer</td>
<td>column: Column node</td>
</tr>
<tr>
<td>editor</td>
<td>data: Row data <br />
column: Column node <br />
field: Column field <br />
index: Row index <br />
frozenRow: Is row frozen</td>
</tr>
<tr>
<td>filter</td>
<td>field: Column field <br />
filterModel: {value,matchMode} Filter metadata <br />
filterCallback: Callback function</td>
</tr>
<tr>
<td>filterheader</td>
<td>field: Column field <br />
filterModel: {value,matchMode} Filter metadata <br />
filterCallback: Callback function</td>
</tr>
<tr>
<td>filterfooter</td>
<td>field: Column field <br />
filterModel: {value,matchMode} Filter metadata <br />
filterCallback: Callback function</td>
</tr>
<tr>
<td>filterclear</td>
<td>field: Column field <br />
filterModel: {value,matchMode} Filter metadata <br />
filterCallback: Callback function</td>
</tr>
<tr>
<td>filterapply</td>
<td>field: Column field <br />
filterModel: {value,matchMode} Filter metadata <br />
filterCallback: Callback function</td>
</tr>
</tbody>
</table>
</div>
<h5>Auto Layout</h5>
<p>Default table-layout is fixed meaning the cell widths do not depend on their content. If you require cells to scale based on their contents set <i>autoLayout</i> property to true. Note that Scrollable and/or Resizable tables do not support auto layout due to technical limitations.</p>
<h5>Templating</h5>
<p>Field data of a corresponding row is displayed as the cell content by default, this can be customized using a <i>body</i> template where current row data and column properties are passed via the slot props.
On the other hand, <i>header</i> and <i>footer</i> sections of a column can either be defined with the properties or the templates. Similarly DataTable itself also provides header and footer properties along with the templates for the main header and footer of the table.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars">
<template #header>
<div>
<Button icon="pi pi-refresh" style="float: left"/>
List of Cars
</div>
</template>
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand">
<template #body="slotProps">
<img :src="'demo/images/car/' + slotProps.data.brand + '.png'" :alt="slotProps.data.brand" width="48px"/>
</template>
</Column>
<Column field="color" header="Color"></Column>
<Column headerStyle="width: 8em" bodyStyle="text-align: center">
<template #header>
<Button type="button" icon="pi pi-cog"></Button>
</template>
<template #body="slotProps">
<Button type="button" icon="pi pi-search" class="p-button-success" style="margin-right: .5em"></Button>
<Button type="button" icon="pi pi-pencil" class="p-button-warning"></Button>
</template>
</Column>
<template #footer>
In total there are {{cars ? cars.length : 0 }} cars.
</template>
</DataTable>
</template>
</code></pre>
<h5>Sizes</h5>
<p>In addition to the regular table, a smal and a large version are available with different paddings. For a table
with smaller paddings use <i>p-datatable-sm</i> class and for a larger one use <i>p-datatable-lg</i>.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars" class="p-datatable-sm">
<template #header>
Small Table
</template>
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
</DataTable>
<DataTable :value="cars">
<template #header>
Normal Table
</template>
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
</DataTable>
<DataTable :value="cars" class="p-datatable-lg">
<template #header>
Large Table
</template>
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
</DataTable>
</template>
</code></pre>
<h5>Column Grouping</h5>
<p>Columns can be grouped at header and footer sections by defining a ColumnGroup with nested rows and columns.</p>
<pre v-code><code><template v-pre>
<DataTable :value="sales">
<ColumnGroup type="header">
<Row>
<Column header="Brand" :rowspan="3" />
<Column header="Sale Rate" :colspan="4" />
</Row>
<Row>
<Column header="Sales" :colspan="2" />
<Column header="Profits" :colspan="2" />
</Row>
<Row>
<Column header="Last Year" />
<Column header="This Year" />
<Column header="Last Year" />
<Column header="This Year" />
</Row>
</ColumnGroup>
<Column field="brand" />
<Column field="lastYearSale" />
<Column field="thisYearSale" />
<Column field="lastYearProfit" />
<Column field="thisYearProfit" />
<ColumnGroup type="footer">
<Row>
<Column footer="Totals:" :colspan="3" />
<Column footer="$506,202" />
<Column footer="$531,020" />
</Row>
</ColumnGroup>
</DataTable>
</template>
</code></pre>
<h5>Pagination</h5>
<p>Pagination is enabled by setting <i>paginator</i> property to true and defining the <i>rows</i> property defines the number of rows per page.
See the <router-link to="/paginator">Paginator</router-link> for the available customization options such as paginator templates, page links,
rows per page options and more which can be passed through the DataTable.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars" :paginator="true" :rows="10">
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
</DataTable>
</template>
</code></pre>
<p>paginatorLeft and paginatorLeft templates are available to specify custom content at the left and right side.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars" :paginator="true" :rows="10">
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
<template #paginatorLeft>
<Button type="button" icon="pi pi-refresh" />
</template>
<template #paginatorRight>
<Button type="button" icon="pi pi-cloud" />
</template>
</DataTable>
</template>
</code></pre>
<p>Paginator can also be programmed programmatically using a binding to the <i>first</i> property that defines the index of the
first element to display. For example setting first to zero will reset the paginator to the very first page. This property
also supports v-model in case you'd like your binding to be updated whenever the user changes the page.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars" :paginator="true" :rows="10" :first="firstRecordIndex">
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
</DataTable>
</template>
</code></pre>
<h5>Sorting</h5>
<p>Enabling <i>sortable</i> property at column component would be enough to make a column sortable.
The property to use when sorting is the <i>field</i> by default and can be customized using the <i>sortField</i>.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars">
<Column field="vin" header="Vin" :sortable="true"></Column>
<Column field="year" header="Year" :sortable="true"></Column>
<Column field="brand" header="Brand" :sortable="true"></Column>
<Column field="color" header="Color" :sortable="true"></Column>
</DataTable>
</template>
</code></pre>
<p>By default sorting is executed on the clicked column only. To enable multiple field sorting, set <i>sortMode</i> property to "multiple" and use metakey when clicking on another column.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars" sortMode="multiple">
<Column field="vin" header="Vin" :sortable="true"></Column>
<Column field="year" header="Year" :sortable="true"></Column>
<Column field="brand" header="Brand" :sortable="true"></Column>
<Column field="color" header="Color" :sortable="true"></Column>
</DataTable>
</template>
</code></pre>
<p>In case you'd like to display the table as sorted per a single column by default on mount or programmatically apply sort, use <i>sortField</i> and <i>sortOrder</i> properties. These
two properties also support the v-model directive to get updated when the user applies sort a column.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars" sortField="year" :sortOrder="1">
<Column field="vin" header="Vin" :sortable="true"></Column>
<Column field="year" header="Year" :sortable="true"></Column>
<Column field="brand" header="Brand" :sortable="true"></Column>
<Column field="color" header="Color" :sortable="true"></Column>
</DataTable>
<DataTable :value="cars" sortField="dynamicSortField" :sortOrder="dynamicSortOrder">
<Column field="vin" header="Vin" :sortable="true"></Column>
<Column field="year" header="Year" :sortable="true"></Column>
<Column field="brand" header="Brand" :sortable="true"></Column>
<Column field="color" header="Color" :sortable="true"></Column>
</DataTable>
</template>
</code></pre>
<p>In multiple mode, use the <i>multiSortMeta</i> property and bind an array of SortMeta objects instead.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars" sortMode="multiple" :multiSortMeta="multiSortMeta">
<Column field="vin" header="Vin" :sortable="true"></Column>
<Column field="year" header="Year" :sortable="true"></Column>
<Column field="brand" header="Brand" :sortable="true"></Column>
<Column field="color" header="Color" :sortable="true"></Column>
</DataTable>
</template>
</code></pre>
<pre v-code.script><code>
data() {
return {
multiSortMeta: [
{field: 'year', order: 1},
{field: 'brand', order: -1}
]
}
}
</code></pre>
<h5>Filtering</h5>
<p>DataTable has advanced filtering capabilities that does the heavy lifting while providing flexible customization. Filtering has two layout alternatives defined with the <i>filterDisplay</i>.
In <b>row</b> setting, filter elements are displayed in a separate row at the header section whereas
in <i>menu</i> mode filter elements are displayed inside an overlay. Filter metadata is specified using the <i>filters</i> as a v-model and UI elements for the filtering
are placed inside the filter template. The template filter gets a <i>filterModel</i> and <i>filterCallback</i>,
use filterModel.value to populate the filter with your own form components and call the filterCallback with the event of your choice like @input, @change, @click.</p>
<pre v-code.script><code>
import CustomerService from '../../service/CustomerService';
import {FilterMatchMode} from 'primevue/api';
export default {
data() {
return {
customers: null,
filters: {
'name': {value: null, matchMode: FilterMatchMode.STARTS_WITH}
}
}
},
created() {
this.customerService = new CustomerService();
},
mounted() {
this.customerService.getCustomersLarge().then(data => this.customers = data);
}
}
</code></pre>
<h6>Filter Row</h6>
<p>Input field is displayed in a separate header row.</p>
<pre v-code><code><template v-pre>
<DataTable :value="customers1"
dataKey="id" v-model:filters="filters" filterDisplay="row" :loading="loading">
<Column field="name" header="Name">
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" :placeholder="`Search by name - ${filterModel.matchMode}`"/>
</template>
</Column>
<DataTable>
</template>
</code></pre>
<h6>Filter Row</h6>
<p>Input field is displayed in an overlay.</p>
<pre v-code><code><template v-pre>
<DataTable :value="customers1"
dataKey="id" v-model:filters="filters" filterDisplay="menu" :loading="loading">
<Column field="name" header="Name">
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" :placeholder="`Search by name - ${filterModel.matchMode}`"/>
</template>
</Column>
<DataTable>
</template>
</code></pre>
<h6>Multiple Constraints</h6>
<p>In "menu" display, it is possible to add more constraints to a same filter. In this case, metadata could be an array of constraints. The operator
defines whether all or any of the constraints should match.</p>
<pre v-code.script><code>
data() {
return {
customers: null,
filters: {
'name': {operator: FilterOperator.AND, constraints: [{value: null, matchMode: FilterMatchMode.STARTS_WITH}]},
}
}
}
</code></pre>
<h6>Populate Filters</h6>
<p>Providing a filters with predefined values would be enough to display the table as filtered by default. This approach can also be used to clear filters progammatically.</p>
<pre v-code.script><code>
data() {
return {
customers: null,
filters: {
'name': {operator: FilterOperator.AND, constraints: [
{value: 'Prime', matchMode: FilterMatchMode.STARTS_WITH},
{value: 'Vue', matchMode: FilterMatchMode.CONTAINS}
]}
}
}
}
</code></pre>
<h6>Match Modes</h6>
<p>Depending on the <i>dataType</i> of the column, suitable match modes are displayed. Default configuration is available at <i>PrimeVue.filterMatchModeOptions</i> which can be used to customize the modes globally for all tables.</p>
<pre v-code.script><code>
import {createApp} from 'vue';
import PrimeVue from 'primevue/config';
import FilterMatchMode from 'primevue/api',
const app = createApp(App);
app.use(PrimeVue, {
filterMatchModeOptions: {
text: [
FilterMatchMode.STARTS_WITH,
FilterMatchMode.CONTAINS,
FilterMatchMode.NOT_CONTAINS,
FilterMatchMode.ENDS_WITH,
FilterMatchMode.EQUALS,
FilterMatchMode.NOT_EQUALS
],
numeric: [
FilterMatchMode.EQUALS,
FilterMatchMode.NOT_EQUALS,
FilterMatchMode.LESS_THAN,
FilterMatchMode.LESS_THAN_OR_EQUAL_TO,
FilterMatchMode.GREATER_THAN,
FilterMatchMode.GREATER_THAN_OR_EQUAL_TO
],
date: [
FilterMatchMode.DATE_IS,
FilterMatchMode.DATE_IS_NOT,
FilterMatchMode.DATE_BEFORE,
FilterMatchMode.DATE_AFTER
]
}
});
</code></pre>
<p>If you need to override the match modes for a particular column use the <i>filterMatchModeOptions</i> property and provide an array with label-value pairs.</p>
<pre v-code><code><template v-pre>
<Column field="name" header="Name" :filterMatchModeOptions="matchModes">
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" :placeholder="`Search by name - ${filterModel.matchMode}`"/>
</template>
</Column>
</template>
</code></pre>
<pre v-code.script><code>
matchModes: [
{label: 'Starts With', FilterMatchMode.STARTS_WITH},
{label: 'Contains', FilterMatchMode.CONTAINS},
]
</code></pre>
<h6>Custom Filter</h6>
<p>Custom filtering is implemented using the <i>FilterService</i>, first register your filter and add it to your <i>filterMatchModeOptions</i>.</p>
<pre v-code.script><code>
import {FilterService} from 'primevue/api';
FilterService.register('myfilter', (a,b) => a === b);
</code></pre>
<pre v-code.script><code>
matchModes: [
{label: 'My Filter', "myfilter"},
{label: 'Starts With', FilterMatchMode.STARTS_WITH},
{label: 'Contains', FilterMatchMode.CONTAINS},
]
</code></pre>
<h6>Filter Slots</h6>
<p>Filter menu overlay can be customized even further with various templates including <i>filterheader</i>, <i>filterfooter</i>, <i>filterclear</i>, <i>filterapply</i>. Example here changes the buttons and adds a footer.</p>
<pre v-code><code><template v-pre>
<Column header="Country" filterField="country.name">
<template #filter="{filterModel}">
<InputText type="text" v-model="filterModel.value" class="p-column-filter" placeholder="Search by country"/>
</template>
<template #filterclear="{filterCallback}">
<Button type="button" icon="pi pi-times" @click="filterCallback()" class="p-button-secondary"></Button>
</template>
<template #filterapply="{filterCallback}">
<Button type="button" icon="pi pi-check" @click="filterCallback()" class="p-button-success"></Button>
</template>
<template #filterfooter>
<div class="p-px-3 p-pt-0 p-pb-3 p-text-center p-text-bold">Customized Buttons</div>
</template>
</Column>
</template>
</code></pre>
<h5>Selection</h5>
<p>DataTable provides single and multiple selection modes on click of a row. Selected rows are bound to the <i>selection</i> property and updated using the v-model directive.
Alternatively column based selection can be done using radio buttons or checkboxes using <i>selectionMode</i> of a particular column. In addition row-select and row-unselect
events are provided as optional callbacks.</p>
<p>The <i>dataKey</i> property identifies a unique value of a row in the dataset, it is not mandatory however being able to define it increases the performance of the table signifantly.</p>
<p>In single mode, selection binding is an object reference.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars" v-model:selection="selectedCar" selectionMode="single" dataKey="vin">
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
</DataTable>
</template>
</code></pre>
<p>In multiple mode, selection binding should be an array and multiple items can either be selected using metaKey or toggled individually depending on the value of <i>metaKeySelection</i> property value which is true by default. On touch enabled devices metaKeySelection is turned off automatically. Additionally ShiftKey is supported for range selection.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars" v-model:selection="selectedCars" selectionMode="multiple" dataKey="vin">
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
</DataTable>
</template>
</code></pre>
<p>If you prefer a radioButton or a checkbox instead of a row click, use the <i>selectionMode</i> of a column instead. Following datatable displays a checkbox at the first column of each row and automatically adds a header checkbox to toggle selection of all rows.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars" v-model:selection="selectedCars" selectionMode="multiple" dataKey="vin">
<Column selectionMode="multiple"></Column>
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
</DataTable>
</template>
</code></pre>
<h5>Scrolling</h5>
<p>DataTable supports both horizontal and vertical scrolling as well as frozen columns and rows. Scrollable DataTable is enabled using <i>scrollable</i> property and <i>scrollHeight</i> to define the viewport height.</p>
<pre v-code><code><template v-pre>
<DataTable :value="cars" :scrollable="true" scrollHeight="400px">
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
</DataTable>
</template>
</code></pre>
<h5>Column Widths of a Scrollable Table</h5>
<p>Scrollable table uses flex layout so there are a couple of rules to consider when adjusting the widths of columns.</p>
<ul>
<li>Use <i>min-width</i> in vertical scrolling only so that when there is enough space columns may grow and for smaller screens a horizontal scrollbar is displayed to provide responsive design.</li>
<li>When horizontal scrolling is enabled, prefer <i>width</i> instead of <i>min-width</i>.</li>
<li>In vertical scrolling only, use <i>flex</i> to disable grow and shrink while defining a initial width. When horizontal scrolling is enabled, this is not required as columns do not grow or shrink in horizontal scrolling.</li>
</ul>
<pre v-code><code><template v-pre>
<Column field="vin" header="Vin" style="flex: 0 0 4rem"></Column>
</template>
</code></pre>
<h6>Flex Scroll</h6>
<p>In cases where viewport should adjust itself according to the table parent's height instead of a fixed viewport height, set scrollHeight option as flex. In example below, table is inside a Dialog where viewport size dynamically responds to the dialog size changes such as maximizing.</p>
<pre v-code><code><template v-pre>
<Button label="Show" icon="pi pi-external-link" @click="openDialog" />
<Dialog header="Flex Scroll" v-model:visible="dialogVisible" :style="{width: '50vw'}" :maximizable="true" :modal="true" :contentStyle="{height: '300px'}">
<DataTable :value="cars" :scrollable="true" scrollHeight="flex">
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
</DataTable>
<template #footer>
<Button label="Yes" icon="pi pi-check" @click="closeDialog" />
<Button label="No" icon="pi pi-times" @click="closeDialog" class="p-button-secondary"/>
</template>
</Dialog>
</template>
</code></pre>
<h6>Full Page Scroll</h6>
<p>FlexScroll can also be used for cases where scrollable viewport should be responsive with respect to the window size. See the <router-link to="/datatable/flexscroll">full page</router-link> demo for an example.</p>
<pre v-code><code><template v-pre>
<div style="height: calc(100vh - 143px)">
<DataTable :value="cars" :scrollable="true" scrollHeight="flex">
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year"></Column>
<Column field="brand" header="Brand"></Column>
<Column field="color" header="Color"></Column>
</DataTable>
</div>
</template>
</code></pre>
<h6>Horizontal Scrolling</h6>
<p>For horizontal scrolling, it is required to set <i>scrollDirection</i> to "horizontal" and give fixed widths to columns.</p>
<pre v-code><code><template v-pre>
<DataTable :value="customers" :scrollable="true" scrollDirection="horizontal">
<Column field="id" header="Id" footer="Id" :style="{width:'200px'}"></Column>
<Column field="name" header="Name" footer="Name" :style="{width:'200px'}"></Column>
<Column field="country.name" header="Country" footer="Country" :style="{width:'200px'}"></Column>
<Column field="date" header="Date" footer="Date" :style="{width:'200px'}"></Column>
<Column field="balance" header="Balance" footer="Balance" :style="{width:'200px'}"></Column>
<Column field="company" header="Company" footer="Company" :style="{width:'200px'}"></Column>
<Column field="status" header="Status" footer="Status" :style="{width:'200px'}"></Column>
<Column field="activity" header="Activity" footer="Activity" :style="{width:'200px'}"></Column>
<Column field="representative.name" header="Representative" footer="Representative" :style="{width:'200px'}"></Column>
</DataTable>
</template>
</code></pre>
<h6>Horizontal and Vertical Scrolling</h6>
<p>Set <i>scrollDirection</i> to "both" and give fixed widths to columns to scroll both ways.</p>
<pre v-code><code><template v-pre>
<DataTable :value="customers" :scrollable="true" scrollHeight="400px" scrollDirection="both">
<Column field="id" header="Id" footer="Id" :style="{width:'200px'}"></Column>
<Column field="name" header="Name" footer="Name" :style="{width:'200px'}"></Column>
<Column field="country.name" header="Country" footer="Country" :style="{width:'200px'}"></Column>
<Column field="date" header="Date" footer="Date" :style="{width:'200px'}"></Column>
<Column field="balance" header="Balance" footer="Balance" :style="{width:'200px'}"></Column>
<Column field="company" header="Company" footer="Company" :style="{width:'200px'}"></Column>
<Column field="status" header="Status" footer="Status" :style="{width:'200px'}"></Column>
<Column field="activity" header="Activity" footer="Activity" :style="{width:'200px'}"></Column>
<Column field="representative.name" header="Representative" footer="Representative" :style="{width:'200px'}"></Column>
</DataTable>
</template>
</code></pre>
<h6>Frozen Rows</h6>
<p>Frozen rows are used to fix certain rows while scrolling, this data is defined with the <i>frozenValue</i> property.</p>
<pre v-code><code><template v-pre>