-
Notifications
You must be signed in to change notification settings - Fork 74
/
TableLite.vue
executable file
·1410 lines (1334 loc) · 40.3 KB
/
TableLite.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="vtl vtl-card">
<div class="vtl-card-title" v-if="title">{{ title }}</div>
<div class="vtl-card-body">
<div class="vtl-row">
<div
class="col-sm-12"
:class="{
'fixed-first-column': isFixedFirstColumn,
'fixed-first-second-column': isFixedFirstColumn && hasCheckbox,
}"
>
<div v-if="isLoading" class="vtl-loading-mask">
<div class="vtl-loading-content">
<span style="color: white">Loading...</span>
</div>
</div>
<table
class="vtl-table vtl-table-hover vtl-table-bordered vtl-table-responsive vtl-table-responsive-sm"
ref="localTable"
:style="'max-height: ' + maxHeight + 'px;'"
>
<thead class="vtl-thead">
<tr class="vtl-thead-tr">
<th v-if="hasCheckbox" class="vtl-thead-th vtl-checkbox-th">
<div>
<input
type="checkbox"
class="vtl-thead-checkbox"
:indeterminate="setting.isIndeterminate"
v-model="setting.isCheckAll"
/>
</div>
</th>
<th
v-for="(col, index) in columns"
class="vtl-thead-th"
:class="col.headerClasses"
:key="index"
:style="
Object.assign(
{
width: col.width ? col.width : 'auto',
},
col.headerStyles
)
"
>
<div
class="vtl-thead-column"
:class="{
'vtl-sortable': col.sortable,
'vtl-both': col.sortable,
'vtl-asc': setting.order === col.field && setting.sort === 'asc',
'vtl-desc': setting.order === col.field && setting.sort === 'desc',
}"
@click.prevent="col.sortable ? doSort(col.field) : false"
v-html="col.label"
>
</div>
</th>
</tr>
</thead>
<template v-if="rows.length > 0">
<tbody
v-if="isStaticMode"
class="vtl-tbody"
:set="(templateRows = groupingKey == '' ? [localRows] : localRows)"
>
<template
v-for="(rows, groupingIndex) in templateRows"
:key="groupingIndex"
>
<tr v-if="groupingKey != ''" class="vtl-tbody-tr vtl-group-tr">
<td
:colspan="hasCheckbox ? columns.length + 1 : columns.length"
class="vtl-tbody-td vtl-group-td"
>
<div class="flex">
<div v-if="hasGroupToggle" class="animation">
<a
:ref="(el) => (toggleButtonRefs[groupingIndex] = el)"
class="cursor-pointer"
@click.prevent="toggleGroup(groupingIndex)"
>▼</a
>
</div>
<div
class="ml-2"
v-html="
groupingDisplay
? groupingDisplay(groupingIndex)
: groupingIndex
"
></div>
</div>
</td>
</tr>
<tr
v-for="(row, i) in rows"
:key="row[setting.keyColumn] ? row[setting.keyColumn] : i"
:ref="
(el) => {
if (!groupingRowsRefs[groupingIndex]) {
groupingRowsRefs[groupingIndex] = [];
}
groupingRowsRefs[groupingIndex][i] = el;
}
"
:name="'vtl-group-' + groupingIndex"
class="vtl-tbody-tr"
:class="
typeof rowClasses === 'function' ? rowClasses(row) : rowClasses
"
@mouseenter="addHoverClassToTr"
@mouseleave="removeHoverClassFromTr"
@click="$emit('row-clicked', row)"
>
<td v-if="hasCheckbox" class="vtl-tbody-td vtl-checkbox-td">
<div>
<input
type="checkbox"
class="vtl-tbody-checkbox"
:ref="
(el) => {
rowCheckbox.push(el);
}
"
:value="row[setting.keyColumn]"
@click="checked(row, $event)"
/>
</div>
</td>
<td
v-for="(col, j) in columns"
:key="j"
class="vtl-tbody-td"
:class="['vtl-tbody-td' + j].concat(col.columnClasses)"
:style="col.columnStyles"
@mouseover="addVerticalHighlight(j)"
@mouseleave="removeVerticalHighlight(j)"
>
<div v-if="col.display" v-html="col.display(row)"></div>
<div v-else>
<div v-if="setting.isSlotMode && slots[col.field]">
<slot :name="col.field" :value="row"></slot>
</div>
<span v-else>{{ row[col.field] }}</span>
</div>
</td>
</tr>
</template>
</tbody>
<tbody
v-else
:set="(templateRows = groupingKey == '' ? [rows] : groupingRows)"
>
<template
v-for="(rows, groupingIndex) in templateRows"
:key="groupingIndex"
>
<tr v-if="groupingKey != ''" class="vtl-tbody-tr vtl-group-tr">
<td
:colspan="hasCheckbox ? columns.length + 1 : columns.length"
class="vtl-tbody-td vtl-group-td"
>
<div class="flex">
<div v-if="hasGroupToggle" class="animation">
<a
:ref="(el) => (toggleButtonRefs[groupingIndex] = el)"
class="cursor-pointer"
@click.prevent="toggleGroup(groupingIndex)"
>▼</a
>
</div>
<div
class="ml-2"
v-html="
groupingDisplay
? groupingDisplay(groupingIndex)
: groupingIndex
"
></div>
</div>
</td>
</tr>
<tr
v-for="(row, i) in rows"
:ref="
(el) => {
if (!groupingRowsRefs[groupingIndex]) {
groupingRowsRefs[groupingIndex] = [];
}
groupingRowsRefs[groupingIndex][i] = el;
}
"
:name="'vtl-group-' + groupingIndex"
:key="row[setting.keyColumn] ? row[setting.keyColumn] : i"
class="vtl-tbody-tr"
:class="
typeof rowClasses === 'function' ? rowClasses(row) : rowClasses
"
@mouseenter="addHoverClassToTr"
@mouseleave="removeHoverClassFromTr"
@click="$emit('row-clicked', row)"
>
<td v-if="hasCheckbox" class="vtl-tbody-td vtl-checkbox-td">
<div>
<input
type="checkbox"
class="vtl-tbody-checkbox"
:ref="
(el) => {
rowCheckbox.push(el);
}
"
:value="row[setting.keyColumn]"
@click="checked(row, $event)"
/>
</div>
</td>
<td
v-for="(col, j) in columns"
:key="j"
class="vtl-tbody-td"
:class="['vtl-tbody-td' + j].concat(col.columnClasses)"
:style="col.columnStyles"
@mouseover="addVerticalHighlight(j)"
@mouseleave="removeVerticalHighlight(j)"
>
<div v-if="col.display" v-html="col.display(row)"></div>
<div v-else>
<div v-if="setting.isSlotMode && slots[col.field]">
<slot :name="col.field" :value="row"></slot>
</div>
<span v-else>{{ row[col.field] }}</span>
</div>
</td>
</tr>
</template>
</tbody>
</template>
</table>
</div>
</div>
<div class="vtl-paging vtl-row" v-if="rows.length > 0">
<template v-if="!setting.isHidePaging">
<div class="vtl-paging-info col-sm-12 col-md-4">
<div role="status" aria-live="polite">
{{
stringFormat(messages.pagingInfo, setting.offset, setting.limit, total)
}}
</div>
</div>
<div class="vtl-paging-change-div col-sm-12 col-md-4">
<span class="vtl-paging-count-label">{{ messages.pageSizeChangeLabel }}</span>
<select class="vtl-paging-count-dropdown" v-model="setting.pageSize">
<option
v-for="pageOption in pageOptions"
:value="pageOption.value"
:key="pageOption.value"
>
{{ pageOption.text }}
</option>
</select>
<span class="vtl-paging-page-label">{{ messages.gotoPageLabel }}</span>
<select class="vtl-paging-page-dropdown" v-model="setting.page">
<option v-for="n in setting.maxPage" :key="n" :value="parseInt(n)">
{{ n }}
</option>
</select>
</div>
<div class="vtl-paging-pagination-div col-sm-12 col-md-4">
<div class="dataTables_paginate">
<ul class="vtl-paging-pagination-ul vtl-pagination">
<li
class="vtl-paging-pagination-page-li vtl-paging-pagination-page-li-first page-item"
:class="{ disabled: setting.page <= 1 }"
>
<a
class="vtl-paging-pagination-page-link vtl-paging-pagination-page-link-first page-link cursor-pointer"
aria-label="Previous"
@click.prevent="setting.page = 1"
>
<span aria-hidden="true">«</span>
<span class="sr-only">First</span>
</a>
</li>
<li
class="vtl-paging-pagination-page-li vtl-paging-pagination-page-li-prev page-item"
:class="{ disabled: setting.page <= 1 }"
>
<a
class="vtl-paging-pagination-page-link vtl-paging-pagination-page-link-prev page-link cursor-pointer"
aria-label="Previous"
@click.prevent="prevPage"
>
<span aria-hidden="true"><</span>
<span class="sr-only">Prev</span>
</a>
</li>
<li
class="vtl-paging-pagination-page-li vtl-paging-pagination-page-li-number page-item"
v-for="n in setting.paging"
:key="n"
:class="{ disabled: setting.page === n }"
>
<a
class="vtl-paging-pagination-page-link vtl-paging-pagination-page-link-number page-link cursor-pointer"
@click.prevent="movePage(n)"
>{{ n }}</a
>
</li>
<li
class="vtl-paging-pagination-page-li vtl-paging-pagination-page-li-next page-item"
:class="{ disabled: setting.page >= setting.maxPage }"
>
<a
class="vtl-paging-pagination-page-link vtl-paging-pagination-page-link-next page-link cursor-pointer"
aria-label="Next"
@click.prevent="nextPage"
>
<span aria-hidden="true">></span>
<span class="sr-only">Next</span>
</a>
</li>
<li
class="vtl-paging-pagination-page-li vtl-paging-pagination-page-li-last page-item"
:class="{ disabled: setting.page >= setting.maxPage }"
>
<a
class="vtl-paging-pagination-page-link vtl-paging-pagination-page-link-last page-link cursor-pointer"
aria-label="Next"
@click.prevent="setting.page = setting.maxPage"
>
<span aria-hidden="true">»</span>
<span class="sr-only">Last</span>
</a>
</li>
</ul>
</div>
</div>
</template>
</div>
<div class="vtl-row" v-else>
<div class="vtl-empty-msg col-sm-12 text-center">
{{ messages.noDataAvailable }}
</div>
</div>
</div>
</div>
</template>
<script>
import {
defineComponent,
ref,
reactive,
computed,
watch,
onBeforeUpdate,
nextTick,
onMounted,
} from "vue";
export default defineComponent({
name: "my-table",
emits: [
"return-checked-rows",
"do-search",
"is-finished",
"get-now-page",
"row-clicked",
"row-toggled",
],
props: {
// 是否讀取中 (is data loading)
isLoading: {
type: Boolean,
require: true,
},
// 是否執行了重新查詢 (Whether to perform a re-query)
isReSearch: {
type: Boolean,
require: true,
},
// 有無Checkbox (Presence of Checkbox)
hasCheckbox: {
type: Boolean,
default: false,
},
// Checkbox勾選後返回資料的型態 (Returns data type for checked of Checkbox)
checkedReturnType: {
type: String,
default: "key",
},
// 標題 (title)
title: {
type: String,
default: "",
},
// 是否鎖定第一欄位位置 (Fixed first column's position)
isFixedFirstColumn: {
type: Boolean,
default: false,
},
// 欄位 (Field)
columns: {
type: Array,
default: () => {
return [];
},
},
// 資料 (data)
rows: {
type: Array,
default: () => {
return [];
},
},
// 資料列類別 (data row classes)
rowClasses: {
type: [Array, Function],
default: () => {
return [];
},
},
// 一頁顯示筆數 (Display the number of items on one page)
pageSize: {
type: Number,
default: 10,
},
// 總筆數 (Total number of transactions)
total: {
type: Number,
default: 100,
},
// 現在頁數 (Current page number)
page: {
type: Number,
default: 1,
},
// 排序條件 (Sort condition)
sortable: {
type: Object,
default: () => {
return {
order: "id",
sort: "asc",
};
},
},
// 顯示文字 (Display text)
messages: {
type: Object,
default: () => {
return {
pagingInfo: "Showing {0}-{1} of {2}",
pageSizeChangeLabel: "Row count:",
gotoPageLabel: "Go to page:",
noDataAvailable: "No data",
};
},
},
// 靜態模式 (Static mode(no refresh server data))
isStaticMode: {
type: Boolean,
default: false,
},
// 插槽模式 (V-slot mode)
isSlotMode: {
type: Boolean,
default: false,
},
// 是否隱藏換頁資訊 (Hide paging)
isHidePaging: {
type: Boolean,
default: false,
},
// 一頁顯示筆數下拉選單 (Dropdown of Display the number of items on one page)
pageOptions: {
type: Array,
default: () => [
{
value: 10,
text: 10,
},
{
value: 25,
text: 25,
},
{
value: 50,
text: 50,
},
],
},
// 分類條件 (Key of grouping)
groupingKey: {
type: String,
default: "",
},
// 是否顯示隱藏分類開關 (Has hide group rows toggle)
hasGroupToggle: {
type: Boolean,
default: false,
},
// 客製化分類顯示 (Customize grouping title)
groupingDisplay: {
type: Function,
default: null,
},
// 設定表格高度 (Table's max height)
maxHeight: {
default: "auto",
},
// 預設群組顯示時為折疊 (Grouping collapsed on start)
startCollapsed: {
type: Boolean,
default: false,
},
// 保持刷新後折疊狀態 (Keep collapsed status after refresh)
isKeepCollapsed: {
type: Boolean,
default: false,
},
// 選擇行是否高亮(Vertical highlight switch)
isVerticalHighlight: {
type: Boolean,
default: false,
},
},
setup(props, { emit, slots }) {
let localTable = ref(null);
// 檢查下拉選單中是否包含預設一頁顯示筆數 (Validate dropdown's values have page-size value or not)
let defaultPageSize =
props.pageOptions.length > 0
? ref(props.pageOptions[0].value)
: ref(props.pageSize);
if (props.pageOptions.length > 0) {
props.pageOptions.forEach((v) => {
if (
Object.prototype.hasOwnProperty.call(v, "value") &&
Object.prototype.hasOwnProperty.call(v, "text") &&
props.pageSize == v.value
) {
defaultPageSize.value = v.value;
}
});
}
// 組件用內部設定值 (Internal set value for components)
const setting = reactive({
// 是否啟用Slot模式 (Enable slot mode)
isSlotMode: props.isSlotMode,
// 是否全選 (Whether to select all)
isCheckAll: false,
// 是否讓全選框呈現半選狀態 (checkbox indeterminate state)
isIndeterminate: false,
// 是否隱藏換頁資訊 (Hide paging)
isHidePaging: props.isHidePaging,
// KEY欄位名稱 (KEY field name)
keyColumn: computed(() => {
let key = "";
Object.assign(props.columns).forEach((col) => {
if (col.isKey) {
key = col.field;
}
});
return key;
}),
// 當前頁數 (current page number)
page: props.page,
// 每頁顯示筆數 (Display count per page)
pageSize: defaultPageSize.value,
// 最大頁數 (Maximum number of pages)
maxPage: computed(() => {
if (props.total <= 0) {
return 0;
}
let maxPage = Math.floor(props.total / setting.pageSize);
let mod = props.total % setting.pageSize;
if (mod > 0) {
maxPage++;
}
return maxPage;
}),
// 該頁數起始值 (The starting value of the page number)
offset: computed(() => {
return (setting.page - 1) * setting.pageSize + 1;
}),
// 該頁數最大值 (Maximum number of pages0
limit: computed(() => {
let limit = setting.page * setting.pageSize;
return props.total >= limit ? limit : props.total;
}),
// 換頁陣列 (Paging array)
paging: computed(() => {
let startPage = setting.page - 2 <= 0 ? 1 : setting.page - 2;
if (setting.maxPage - setting.page <= 2) {
startPage = setting.maxPage - 4;
}
startPage = startPage <= 0 ? 1 : startPage;
let pages = [];
for (let i = startPage; i <= setting.maxPage; i++) {
if (pages.length < 5) {
pages.push(i);
}
}
return pages;
}),
// 組件內用排序 (Sortable for local)
order: props.sortable.order,
sort: props.sortable.sort,
pageOptions: props.pageOptions,
isVerticalHighlight: props.isVerticalHighlight,
});
// 已選擇中的資料 (Checked rows)
const isChecked = ref([]);
// 組件內用資料 (Data rows for local)
const localRows = computed(() => {
let rows = props.rows;
// refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/compare
var collator = new Intl.Collator(undefined, {
numeric: true,
sensitivity: "base",
});
let sortOrder = setting.sort === "desc" ? -1 : 1;
rows.sort(function (a, b) {
return collator.compare(a[setting.order], b[setting.order]) * sortOrder;
});
let result = null;
if (props.groupingKey) {
// If have set grouping-key create group temp data
let tmp = {};
rows.forEach((v) => {
if (!tmp[v[props.groupingKey]]) {
tmp[v[props.groupingKey]] = [];
}
tmp[v[props.groupingKey]].push(v);
});
result = {};
for (let index = setting.offset - 1; index < setting.limit; index++) {
result[rows[index][props.groupingKey]] = tmp[rows[index][props.groupingKey]];
}
} else {
result = [];
for (let index = setting.offset - 1; index < setting.limit; index++) {
result.push(rows[index]);
}
}
nextTick(function () {
// 資料完成渲染後回傳私有元件
callIsFinished();
});
return result;
});
////////////////////////////
//
// Checkbox 相關操作
// (Checkbox related operations)
//
// 定義Checkbox參照 (Define Checkbox reference)
const rowCheckbox = ref([]);
/**
* 重新渲染前執行 (Execute before re-rendering)
*/
onBeforeUpdate(() => {
// 每次更新前都把值全部清空 (Clear all values before each update)
rowCheckbox.value = [];
});
/**
* 監聽全勾選Checkbox (Check all checkboxes for monitoring)
*/
watch(
() => setting.isCheckAll,
(state) => {
if (props.hasCheckbox) {
setting.isIndeterminate = false;
isChecked.value = [];
if (state) {
let tmpRows = (props.isStaticMode) ? props.rows.slice((setting.offset - 1), setting.limit) : props.rows;
if (props.checkedReturnType == "row") {
isChecked.value = tmpRows;
} else {
tmpRows.forEach((val) => {
isChecked.value.push(val[setting.keyColumn]);
});
}
}
rowCheckbox.value.forEach((val) => {
if (val) {
val.checked = state;
}
});
// 回傳畫面上選上的資料 (Return the selected data on the screen)
emit("return-checked-rows", isChecked.value);
}
}
);
/**
* 監控有無顯示Checkbox變化 (hasCeckbox props for monitoring)
*/
watch(
() => props.hasCheckbox,
(v) => {
if (!v) {
setting.isCheckAll = false;
}
}
);
/**
* Checkbox點擊事件 (Checkbox click event)
*/
const checked = (row, event) => {
event.stopPropagation();
setting.isIndeterminate = false;
let checkboxValue = row[setting.keyColumn];
if (props.checkedReturnType == "row") {
checkboxValue = row;
}
if (event.target.checked) {
isChecked.value.push(checkboxValue);
} else {
const index = isChecked.value.indexOf(checkboxValue);
if (index >= 0) {
isChecked.value.splice(index, 1);
}
}
if (isChecked.value.length == props.rows.length) {
if (setting.isCheckAll) {
emit("return-checked-rows", isChecked.value);
}
setting.isCheckAll = true;
} else {
if (isChecked.value.length > 0) {
setting.isIndeterminate = true;
}
// 回傳畫面上選上的資料 (Return the selected data on the screen)
emit("return-checked-rows", isChecked.value);
}
};
/**
* 清空畫面上所有選擇資料 (Clear all selected data on the screen)
*/
const clearChecked = () => {
isChecked.value = [];
rowCheckbox.value.forEach((val) => {
if (val && val.checked) {
val.checked = false;
}
});
// 回傳畫面上選上的資料 (Return the selected data on the screen)
emit("return-checked-rows", isChecked.value);
};
////////////////////////////
//
// 排序·換頁等 相關操作
// (Sorting, page change, etc. related operations)
//
/**
* 呼叫執行排序 (Call execution sequencing)
*/
const doSort = (order) => {
let sort = "asc";
if (order == setting.order) {
// 排序中的項目時 (When sorting items)
if (setting.sort == "asc") {
sort = "desc";
}
}
let offset = (setting.page - 1) * setting.pageSize;
let limit = setting.pageSize;
setting.order = order;
setting.sort = sort;
emit("do-search", offset, limit, order, sort);
// 清空畫面上選擇的資料 (Clear the selected data on the screen)
if (setting.isCheckAll) {
// 取消全選時自然會清空 (It will be cleared when you cancel all selections)
setting.isCheckAll = false;
} else {
if (props.hasCheckbox) {
clearChecked();
}
}
};
/**
* 切換頁碼 (Switch page number)
*
* @param page number 新頁碼 (New page number)
* @param prevPage number 現在頁碼 (Current page number)
*/
const changePage = (page, prevPage) => {
setting.isCheckAll = false;
setting.isIndeterminate = false;
if (props.hasCheckbox) {
isChecked.value = [];
}
let order = setting.order;
let sort = setting.sort;
let offset = (page - 1) * setting.pageSize;
let limit = setting.pageSize;
if (!props.isReSearch || page > 1 || page == prevPage) {
// 非重新查詢發生的頁碼變動才執行呼叫查詢 (Call query will only be executed if the page number is changed without re-query)
emit("do-search", offset, limit, order, sort);
}
};
// 監聽頁碼切換 (Monitor page switching)
watch(() => setting.page, changePage);
// 監聽手動頁碼切換 (Monitor manual page switching)
watch(
() => props.page,
(val) => {
if (val <= 1) {
setting.page = 1;
emit("get-now-page", setting.page);
} else if (val >= setting.maxPage) {
setting.page = setting.maxPage;
emit("get-now-page", setting.page);
} else {
setting.page = val;
}
}
);
/**
* 切換顯示筆數 (Switch display number)
*/
const changePageSize = () => {
if (setting.page === 1) {
// 沒自動觸發 changePage(),所以手動觸發
changePage(setting.page, setting.page);
} else {
// 強制返回第一頁,並自動觸發 changePage()
setting.page = 1;
setting.isCheckAll = false;
setting.isIndeterminate = false;
}
};
// 監聽組件內顯示筆數切換 (Monitor display number switch from component)
watch(() => setting.pageSize, changePageSize);
// 監聽來自Prop的顯示筆數切換 (Monitor display number switch from prop)
watch(
() => props.pageSize,
(newPageSize) => {
setting.pageSize = newPageSize;
}
);
/**
* 上一頁 (Previous page)
*/
const prevPage = () => {
if (setting.page == 1) {
// 如果是第一頁,不予執行 (If it is the first page, it will not be executed)
return false;
}
setting.page--;
};
/**
* 移動至指定頁數 (Move to the specified number of pages)
*/
const movePage = (page) => {
setting.page = page;
};
/**
* 下一頁 (Next page)
*/
const nextPage = () => {
if (setting.page >= setting.maxPage) {
// 如果等於大於最大頁數,不與執行 (If it is equal to or greater than the maximum number of pages, no execution)
return false;
}
setting.page++;
};
// 監聽資料變更 (Monitoring data changes)
watch(
() => props.rows,
() => {
if (props.isReSearch || props.isStaticMode) {
setting.page = 1;
}
nextTick(function () {
// 資料完成渲染後回傳私有元件 (Return the private components after the data is rendered)
if (!props.isStaticMode) {
callIsFinished();
}
});
},
{ deep: true }
);
const stringFormat = (template, ...args) => {
return template.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != "undefined" ? args[number] : match;
});
};
// Call 「is-finished」 Method
const callIsFinished = () => {
if (localTable.value) {
let localElement = localTable.value.getElementsByClassName("is-rows-el");
emit("is-finished", localElement);
}
emit("get-now-page", setting.page);
};
// Toggle button elements
const toggleButtonRefs = ref({});
// Grouping rows
const groupingRowsRefs = ref({});
// Saved toggle status
const groupingToggleStatus = ref({});
// Data rows for grouping (Default-mode only)
const groupingRows = computed(() => {
let result = {};
props.rows.forEach((v) => {
if (!result[v[props.groupingKey]]) {
result[v[props.groupingKey]] = [];
}
result[v[props.groupingKey]].push(v);
});
nextTick(function () {
if (props.startCollapsed || props.isKeepCollapsed) {
for (const [groupIndex, el] of Object.entries(toggleButtonRefs.value)) {
if (el) {
let isOpen = !props.startCollapsed;
if (
props.isKeepCollapsed &&
groupingToggleStatus.value[groupIndex] !== undefined
) {
isOpen = !groupingToggleStatus.value[groupIndex];
}
if (
(isOpen && el.parentElement.classList.contains("rotated-90")) ||
(!isOpen && !el.parentElement.classList.contains("rotated-90"))
) {
el.parentElement.classList.toggle("rotated-90");
}
if (!isOpen) {
groupingRowsRefs.value[groupIndex].forEach((element) => {
if (element) {
element.classList.add("hidden");
}
});
}
}
}
}
callIsFinished();
});
return result;
});
/**
* Toggle Group rows
*
* @param {String} groupIndex
*/
const toggleGroup = (groupIndex) => {
let targetEl = toggleButtonRefs.value[groupIndex];
if (targetEl) {
targetEl.parentElement.classList.toggle("rotated-90");
const isClose = targetEl.parentElement.classList.contains("rotated-90");
groupingRowsRefs.value[groupIndex].forEach((element) => {
if (element) {
if (isClose) {
element.classList.add("hidden");
} else {
element.classList.remove("hidden");
}
}
});
groupingToggleStatus.value[groupIndex] = isClose;
emit("row-toggled", groupingRows.value[groupIndex], isClose);
}