vuetable-next 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/dist/vuetable-next.d.ts +17 -5
  2. package/dist/vuetable-next.js +1500 -4236
  3. package/dist/vuetable-next.js.map +1 -1
  4. package/dist/vuetable-next.umd.cjs +7 -32
  5. package/dist/vuetable-next.umd.cjs.map +1 -1
  6. package/package.json +8 -10
  7. package/.eslintignore +0 -4
  8. package/.idea/easycode.ignore +0 -13
  9. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  10. package/.idea/jsLinters/eslint.xml +0 -6
  11. package/.idea/material_theme_project_new.xml +0 -13
  12. package/.idea/modules.xml +0 -8
  13. package/.idea/vcs.xml +0 -6
  14. package/.idea/vuetable-next.iml +0 -12
  15. package/.vscode/extensions.json +0 -3
  16. package/auto-imports.d.ts +0 -73
  17. package/demo/vue2/index.ts +0 -6
  18. package/demo/vue2/index.vue +0 -17
  19. package/demo/vue3/index.ts +0 -4
  20. package/demo/vue3/index.vue +0 -12
  21. package/eslint.config.js +0 -122
  22. package/index.html +0 -86
  23. package/public/vite.svg +0 -1
  24. package/src/assets/vue.svg +0 -1
  25. package/src/components/Vuetable.vue +0 -1207
  26. package/src/components/VuetableColGroup.vue +0 -66
  27. package/src/components/VuetableColGutter.vue +0 -25
  28. package/src/components/VuetableCssSemanticUI.ts +0 -36
  29. package/src/components/VuetableFieldCheckbox.vue +0 -87
  30. package/src/components/VuetableFieldCheckboxMixin.vue +0 -46
  31. package/src/components/VuetableFieldHandle.vue +0 -41
  32. package/src/components/VuetableFieldMixin.vue +0 -30
  33. package/src/components/VuetableFieldSequence.vue +0 -43
  34. package/src/components/VuetablePagination.vue +0 -119
  35. package/src/components/VuetablePaginationDropdown.vue +0 -88
  36. package/src/components/VuetablePaginationInfo.vue +0 -51
  37. package/src/components/VuetablePaginationInfoMixin.vue +0 -64
  38. package/src/components/VuetablePaginationMixin.vue +0 -107
  39. package/src/components/VuetableRowHeader.vue +0 -196
  40. package/src/components/useVuetablePagination.ts +0 -56
  41. package/src/components/useVuetablePaginationInfo.ts +0 -40
  42. package/src/data/fields.ts +0 -102
  43. package/src/data/lang.ts +0 -4
  44. package/src/index.ts +0 -57
  45. package/src/main.ts +0 -398
  46. package/src/style.css +0 -79
  47. package/src/vite-env.d.ts +0 -1
  48. package/test/Vuetable.AJAX.test.ts +0 -291
  49. package/test/Vuetable.Components.test.ts +0 -27
  50. package/test/Vuetable.Composable.test.ts +0 -151
  51. package/test/Vuetable.Computed.test.ts +0 -211
  52. package/test/Vuetable.Features.test.ts +0 -145
  53. package/test/Vuetable.FieldsDefinition.test.ts +0 -236
  54. package/test/Vuetable.HTML.test.ts +0 -16
  55. package/test/Vuetable.Methods.test.ts +0 -373
  56. package/test/__snapshots__/Vuetable.Components.test.ts.snap +0 -11
  57. package/test/__snapshots__/Vuetable.HTML.test.ts.snap +0 -85
  58. package/tsconfig.app.json +0 -32
  59. package/tsconfig.json +0 -22
  60. package/tsconfig.node.json +0 -13
  61. package/vite.config.ts +0 -52
  62. package/vitest.config.ts +0 -17
@@ -1,1207 +0,0 @@
1
- <template>
2
- <div :class="$_css.tableWrapper">
3
- <div
4
- v-if="isFixedHeader"
5
- class="vuetable-head-wrapper">
6
- <table :class="['vuetable', $_css.tableClass, $_css.tableHeaderClass]">
7
- <vuetable-col-group is-header>
8
- </vuetable-col-group>
9
- <thead>
10
- <slot
11
- :fields="tableFields"
12
- name="tableHeader">
13
- <template
14
- :key="headerIndex"
15
- v-for="(header, headerIndex) in headerRows">
16
- <component
17
- :is="header"
18
- :index="headerIndex"
19
- @vuetable:header-event="onHeaderEvent">
20
- </component>
21
- </template>
22
- </slot>
23
- </thead>
24
- </table>
25
- </div>
26
-
27
- <div
28
- :class="{'fixed-header' : isFixedHeader}"
29
- :style="{ height: tableHeight! }"
30
- class="vuetable-body-wrapper">
31
- <table :class="['vuetable', isFixedHeader ? 'fixed-header' : '', $_css.tableClass, $_css.tableBodyClass]">
32
- <vuetable-col-group>
33
- </vuetable-col-group>
34
- <thead v-if="!isFixedHeader">
35
- <slot
36
- :fields="tableFields"
37
- name="tableHeader">
38
- <template :key="headerIndex" v-for="(header, headerIndex) in headerRows">
39
- <component
40
- :is="header"
41
- :index="headerIndex"
42
- @vuetable:header-event="onHeaderEvent">
43
- </component>
44
- </template>
45
- </slot>
46
- </thead>
47
- <tfoot>
48
- <slot
49
- :fields="tableFields"
50
- name="tableFooter">
51
- </slot>
52
- </tfoot>
53
- <tbody
54
- v-cloak
55
- class="vuetable-body">
56
- <template
57
- :key="itemIndex"
58
- v-for="(item, itemIndex) in tableData">
59
- <tr
60
- :item-index="itemIndex"
61
- :class="onRowClass(item, itemIndex)"
62
- @click="onRowClicked(item, itemIndex, $event)"
63
- @dblclick="onRowDoubleClicked(item, itemIndex, $event)"
64
- @mouseover="onMouseOver(item, itemIndex, $event)">
65
- <template v-for="(field, fieldIndex) in tableFields">
66
- <template v-if="field.visible">
67
- <template v-if="isFieldComponent(field.name)">
68
- <component
69
- :is="field.name"
70
- :key="fieldIndex"
71
- :row-data="item"
72
- :row-index="itemIndex"
73
- :row-field="field"
74
- :class="bodyClass('vuetable-component', field)"
75
- :style="{ width: field.width }"
76
- @vuetable:field-event="onFieldEvent">
77
- </component>
78
- </template>
79
- <template v-else-if="isFieldSlot(field.name)">
80
- <td
81
- :key="fieldIndex"
82
- :class="bodyClass('vuetable-slot', field)"
83
- :style="{ width: field.width! }">
84
- <slot
85
- :name="field.name as string"
86
- :row-data="item"
87
- :row-index="itemIndex"
88
- :row-field="field">
89
- </slot>
90
- </td>
91
- </template>
92
- <template v-else>
93
- <td
94
- :key="fieldIndex"
95
- :class="bodyClass('vuetable-td-'+field.name, field)"
96
- :style="{ width: field.width! }"
97
- v-html="renderNormalField(field, item)"
98
- @click="onCellClicked(item, itemIndex, field, $event)"
99
- @dblclick="onCellDoubleClicked(item, itemIndex, field, $event)"
100
- @contextmenu="onCellRightClicked(item, itemIndex, field, $event)">
101
- </td>
102
- </template>
103
- </template>
104
- </template>
105
- </tr>
106
- <template v-if="useDetailRow">
107
- <transition
108
- :key="itemIndex"
109
- :name="detailRowTransition">
110
- <tr
111
- v-if="isVisibleDetailRow(item[trackBy])"
112
- :class="onDetailRowClass(item, itemIndex)"
113
- @click="onDetailRowClick(item, itemIndex, $event)">
114
- <td :colspan="countVisibleFields">
115
- <component
116
- :is="detailRowComponent"
117
- :row-data="item"
118
- :row-index="itemIndex"
119
- :options="detailRowOptions">
120
- </component>
121
- </td>
122
- </tr>
123
- </transition>
124
- </template>
125
- </template>
126
- <template v-if="displayEmptyDataRow">
127
- <tr>
128
- <td
129
- :colspan="countVisibleFields"
130
- class="vuetable-empty-result"
131
- v-html="noDataTemplate">
132
- </td>
133
- </tr>
134
- </template>
135
- <template v-if="lessThanMinRows">
136
- <tr
137
- :key="i"
138
- v-for="i in blankRows"
139
- class="blank-row">
140
- <template v-for="(field, fieldIndex) in tableFields">
141
- <td v-if="field.visible" :key="fieldIndex">
142
- &nbsp;
143
- </td>
144
- </template>
145
- </tr>
146
- </template>
147
- </tbody>
148
- </table>
149
- </div>
150
- </div>
151
- </template>
152
-
153
- <script setup lang="ts">
154
- import type { AxiosPromise, AxiosResponse } from "axios";
155
- import axios from "axios";
156
- import VuetableRowHeader from "./VuetableRowHeader.vue";
157
- import VuetableColGroup from "./VuetableColGroup.vue";
158
- import CssSemanticUI from "./VuetableCssSemanticUI";
159
- import {
160
- defineProps,
161
- defineSlots,
162
- defineEmits,
163
- defineExpose,
164
- defineOptions,
165
- ref,
166
- reactive,
167
- computed,
168
- getCurrentInstance,
169
- nextTick,
170
- onMounted,
171
- onUnmounted,
172
- watch,
173
- provide, toRefs
174
- } from "vue-demi";
175
- import type { ComponentPublicInstance, DefineComponent } from "vue-demi";
176
- import { unref } from "vue";
177
-
178
- type SortOrder = { field: string; sortField: string; direction: "asc" | "desc"; };
179
-
180
- interface Props {
181
- fields: any[]
182
- loadOnStart?: boolean
183
- apiUrl?: string
184
- httpMethod?: "get" | "post"
185
- reactiveApiUrl?: boolean
186
- apiMode?: boolean
187
- data?: any | null
188
- dataManager?: ((sortOrder: SortOrder[], pagination: { [key: string]: any }) => any[]) | null
189
- dataPath?: string
190
- paginationPath?: string
191
- queryParams?: { [key: string]: string | number } | ((sortOrder: SortOrder[] | string, currentPage: number, perPage: number) => { [key: string]: string | number } | string | number)
192
- appendParams?: { [key: string]: string | number }
193
- httpOptions?: Record<string, unknown>
194
- httpFetch?: ((apiUrl: string, httpOptions: Record<string, unknown>) => AxiosResponse) | null
195
- perPage?: number
196
- initialPage?: number /** Page that should be displayed when the table is first displayed */
197
- firstPage?: number /** First page number. Set this prop to 0 for zero based pagination */
198
- sortOrder?: SortOrder[]
199
- multiSort?: boolean
200
- tableHeight?: string | null
201
- /*
202
- * physical key that will trigger multi-sort option
203
- * possible values: 'alt', 'ctrl', 'meta', 'shift'
204
- * 'ctrl' might not work as expected on Mac
205
- */
206
- multiSortKey?: "alt" | "ctrl" | "meta"| "shift"
207
- rowClass?: string | ((...args: any[]) => string | string[] | { [key: string]: boolean })
208
- detailRowComponent?: string | { [key: string]: any }
209
- detailRowTransition?: string
210
- detailRowClass?: string | ((...args: any[]) => any)
211
- detailRowOptions?: { [key: string]: any }
212
- trackBy?: string
213
- css?: { [key: string]: any }
214
- minRows?: number
215
- silent?: boolean
216
- noDataTemplate?: string
217
- showSortIcons?: boolean
218
- headerRows?: string[]
219
- transform?: ((response: AxiosResponse) => any) | null
220
- sortParams?: ((sortOrder: SortOrder[]) => string) | null
221
- fieldPrefix?: string
222
- }
223
-
224
- interface Field {
225
- name: string | DefineComponent
226
- sortField: string | null
227
- title: string | ((str?: any) => string)
228
- titleClass: string
229
- dataClass: string
230
- formatter: (<T>(...args: any[]) => T) | null
231
- visible: boolean
232
- width: string | null
233
- $_index: number
234
- [key: string]: unknown
235
- }
236
-
237
- interface Pagination {
238
- total: number
239
- per_page: number
240
- from: number
241
- to: number
242
- last_page: number
243
- current_page: number
244
- first_page: number
245
- }
246
-
247
- const {
248
- fields,
249
- loadOnStart = true,
250
- apiUrl = "",
251
- httpMethod = "get",
252
- reactiveApiUrl = true,
253
- apiMode = true,
254
- data = null,
255
- dataManager = null,
256
- dataPath = "data",
257
- paginationPath = "links.pagination",
258
- queryParams = {
259
- sort: "sort",
260
- page: "page",
261
- perPage: "per_page"
262
- },
263
- appendParams = {},
264
- httpOptions = {},
265
- httpFetch = null,
266
- perPage = 10,
267
- initialPage = 1,
268
- firstPage = 1,
269
- sortOrder = [],
270
- multiSort = false,
271
- tableHeight = null,
272
- multiSortKey = "alt",
273
- rowClass = "",
274
- detailRowComponent = "",
275
- detailRowTransition = "",
276
- detailRowClass = "vuetable-detail-row",
277
- detailRowOptions = {},
278
- trackBy = "id",
279
- css = {},
280
- minRows = 0,
281
- silent = false,
282
- noDataTemplate = "No Data Available",
283
- headerRows = ["VuetableRowHeader"],
284
- transform = null,
285
- sortParams = null,
286
- fieldPrefix = "vuetable-field-",
287
- showSortIcons = true
288
- } = defineProps<Props>();
289
-
290
- const emit = defineEmits<{
291
- "vuetable:load-error": [response?: AxiosResponse],
292
- "vuetable:load-success": [response?: AxiosResponse],
293
- "vuetable:loaded": [],
294
- "vuetable:loading": [],
295
- "vuetable:scrollbar-visible": [visible: boolean],
296
- "vuetable:pagination-data": [tablePagination: Pagination | null],
297
- "vuetable:row-clicked": [args: { data: any; index: number; event: Event }],
298
- "vuetable:row-dblclicked": [args: { data: any; index: number; event: Event }],
299
- "vuetable:detail-row-clicked": [args: { data: any; index: number; event: Event }],
300
- "vuetable:cell-clicked": [args: { data: any; index: number; field: Field; event: Event }],
301
- "vuetable:cell-dblclicked": [args: { data: any; index: number; field: Field; event: Event }],
302
- "vuetable:cell-rightclicked": [args: { data: any; index: number; field: Field; event: Event }],
303
- "vuetable:row-mouseover": [args: { data: any; index: number; event: Event }],
304
- "vuetable:field-event": [type: string, payload: any, app?: ComponentPublicInstance | null],
305
- "vuetable:header-event": [type: string, payload: any, app?: ComponentPublicInstance | null],
306
- "vuetable:checkbox-toggled": [isChecked: boolean, fieldName: string],
307
- "vuetable:checkbox-toggled-all": [isChecked: boolean],
308
- "vuetable:data-reset": [],
309
- "vuetable:initialized": [tableFields: Field[]]
310
- }>();
311
-
312
- const slots = defineSlots();
313
-
314
- defineOptions({
315
- name: "Vuetable",
316
- components: {
317
- VuetableRowHeader,
318
- VuetableColGroup,
319
- }
320
- });
321
-
322
- const app = getCurrentInstance();
323
-
324
- const tableFields = ref<Field[]>([]);
325
- const tableData = ref<any[] | null>(null);
326
- const tablePagination = ref<Pagination | null>(null);
327
- const currentPage = ref(initialPage);
328
- const selectedTo = ref<string[]>([]);
329
- const visibleDetailRows = ref<any[]>([]);
330
- const lastScrollPosition = ref(0);
331
- const scrollBarWidth = ref("17px");
332
- const scrollVisible = ref(false);
333
- const order = ref<SortOrder[]>(sortOrder as SortOrder[]);
334
- const optionsHttp = ref<Record<string, unknown>>(httpOptions);
335
- const instanceFetch = ref<Promise<AxiosResponse>>();
336
-
337
- const $_css = reactive({ ...CssSemanticUI.table, ...css });
338
-
339
- const dataIsAvailable = computed(() => !tableData.value?.length ? false : tableData.value?.length > 0);
340
- const useDetailRow = computed(() => dataIsAvailable.value && detailRowComponent !== "");
341
- const hasRowIdentifier = computed(() => tableData.value && typeof tableData.value[0][trackBy] !== "undefined");
342
- const countVisibleFields = computed(() => tableFields.value.filter(field => field.visible).length);
343
- const countTableData = computed(() => tableData.value?.length || 0);
344
- const displayEmptyDataRow = computed(() => countTableData.value === 0 && noDataTemplate.length > 0);
345
-
346
- const emptyDataTable = computed(() => tableData.value === null || tableData.value?.length === 0);
347
- const lessThanMinRows = computed(() => emptyDataTable.value ? true : tableData.value!.length < minRows);
348
- const blankRows = computed(() => emptyDataTable.value ? minRows : tableData.value!.length >= minRows ? 0 : minRows - tableData.value!.length);
349
-
350
- const isApiMode = computed(() => apiMode);
351
- const isDataMode = computed(() => !apiMode);
352
- const isFixedHeader = computed(() => tableHeight != null);
353
- const vuetable = computed(() => app?.proxy);
354
-
355
- normalizeFields();
356
- normalizeSortOrder();
357
-
358
- nextTick( () => {
359
- emit("vuetable:initialized", tableFields.value);
360
- });
361
-
362
- onMounted(() => {
363
- if (loadOnStart) {
364
- loadData();
365
- }
366
-
367
- if (isFixedHeader) {
368
- scrollBarWidth.value = getScrollBarWidth() + "px";
369
-
370
- let elem = app?.proxy?.$el.getElementsByClassName("vuetable-body-wrapper")[0];
371
- if (elem != null) {
372
- elem.addEventListener("scroll", handleScroll);
373
- }
374
- }
375
- });
376
-
377
- onUnmounted(() => {
378
- let elem = app?.proxy?.$el.getElementsByClassName("vuetable-body-wrapper")[0];
379
- if (elem != null) {
380
- elem.removeEventListener("scroll", handleScroll);
381
- }
382
- });
383
-
384
- defineExpose({
385
- apiUrl,
386
- isApiMode,
387
- tableFields,
388
- tableData,
389
- tablePagination,
390
- fieldPrefix,
391
- countVisibleFields,
392
- dataIsAvailable,
393
- countTableData,
394
- displayEmptyDataRow,
395
- useDetailRow,
396
- hasRowIdentifier,
397
- lessThanMinRows,
398
- blankRows,
399
- scrollVisible,
400
- scrollBarWidth,
401
- trackBy,
402
- showSortIcons,
403
- instanceFetch,
404
- isFieldSlot,
405
- onHeaderEvent,
406
- setData,
407
- getFieldTitle,
408
- orderBy,
409
- camelCase,
410
- clearSortOrder,
411
- toggleDetailRow,
412
- clearSelectedValues,
413
- showField,
414
- hideField,
415
- toggleField,
416
- onCheckboxToggled,
417
- onCheckboxToggledAll,
418
- checkIfRowIdentifierExists,
419
- changePage,
420
- resetData,
421
- refresh,
422
- isFieldComponent,
423
- makeTitle,
424
- getAllQueryParams,
425
- isSortable,
426
- getDefaultSortParam,
427
- getObjectValue,
428
- isSelectedRow,
429
- loadData
430
- });
431
-
432
- provide("vuetable", {
433
- isApiMode,
434
- fieldPrefix,
435
- tableFields: unref(tableFields),
436
- tableData: unref(tableData),
437
- tablePagination: unref(tablePagination),
438
- countVisibleFields: unref(countVisibleFields),
439
- dataIsAvailable: unref(dataIsAvailable),
440
- countTableData: unref(countTableData),
441
- displayEmptyDataRow: unref(displayEmptyDataRow),
442
- useDetailRow: unref(useDetailRow),
443
- hasRowIdentifier: unref(hasRowIdentifier),
444
- lessThanMinRows: unref(lessThanMinRows),
445
- blankRows: unref(blankRows),
446
- scrollVisible: unref(scrollVisible),
447
- scrollBarWidth: unref(scrollBarWidth),
448
- sortOrder,
449
- trackBy,
450
- perPage,
451
- showSortIcons,
452
- css: toRefs($_css),
453
- isFieldSlot,
454
- onHeaderEvent,
455
- setData,
456
- getFieldTitle,
457
- orderBy,
458
- camelCase,
459
- clearSortOrder,
460
- toggleDetailRow,
461
- clearSelectedValues,
462
- showField,
463
- hideField,
464
- toggleField,
465
- onCheckboxToggled,
466
- onCheckboxToggledAll,
467
- checkIfRowIdentifierExists,
468
- changePage,
469
- resetData,
470
- refresh,
471
- isFieldComponent,
472
- makeTitle,
473
- getAllQueryParams,
474
- isSortable,
475
- getDefaultSortParam,
476
- getObjectValue,
477
- isSelectedRow
478
- });
479
-
480
- function getScrollBarWidth () {
481
- const outer = document.createElement("div");
482
- const inner = document.createElement("div");
483
-
484
- outer.style.visibility = "hidden";
485
- outer.style.width = "100px";
486
-
487
- inner.style.width = "100%";
488
-
489
- outer.appendChild(inner);
490
- document.body.appendChild(outer);
491
-
492
- const widthWithoutScrollbar = outer.offsetWidth;
493
- outer.style.overflow = "scroll";
494
- const widthWithScrollbar = inner.offsetWidth;
495
- document.body.removeChild(outer);
496
-
497
- return (widthWithoutScrollbar - widthWithScrollbar);
498
- }
499
-
500
- //make sure that the header and the body are aligned when scrolling horizontally on a table that is wider than the viewport
501
- function handleScroll (e: { currentTarget: HTMLElement }) {
502
- const horizontal = e.currentTarget?.scrollLeft;
503
-
504
- //don't modify header scroll if we are scrolling vertically
505
- if (horizontal != lastScrollPosition.value) {
506
- let header = vuetable.value?.$el.getElementsByClassName("vuetable-head-wrapper")[0];
507
- if (header != null) {
508
- header.scrollLeft = horizontal;
509
- }
510
- lastScrollPosition.value = horizontal;
511
- }
512
- }
513
-
514
- function warn (msg: string) {
515
- if (!silent) {
516
- console.warn(msg);
517
- }
518
- }
519
-
520
- function bodyClass (base: string, field: Field): string[] {
521
- return [ base, field.dataClass ];
522
- }
523
-
524
- function normalizeFields () {
525
- if (typeof fields === "undefined") {
526
- warn("You need to provide \"fields\" prop.");
527
- return;
528
- }
529
-
530
- tableFields.value = [];
531
-
532
- fields.forEach( (field, i) => {
533
- tableFields.value.push(newField(field, i));
534
- });
535
- }
536
-
537
- function newField (field: Field | string, index: number): Field {
538
- const defaultField = {
539
- name: "",
540
- // title:
541
- // this allow the code to detect undefined title
542
- // and replace it with capitalized name instead
543
- titleClass: "",
544
- dataClass: "",
545
- sortField: null,
546
- formatter: null,
547
- visible: true,
548
- width: null,
549
- $_index: index,
550
- };
551
-
552
- if (typeof field === "string") {
553
- return {
554
- ...defaultField,
555
- name: normalizeFieldName(field),
556
- title: makeTitle(field)
557
- };
558
- }
559
-
560
- const obj = Object.assign({}, defaultField, field) as Field;
561
- obj.name = normalizeFieldName(obj.name);
562
- if (obj.title === undefined) {
563
- obj.title = makeTitle(obj.name as string);
564
- }
565
- if (obj.formatter !== null && typeof obj.formatter !== "function") {
566
- console.error(obj.name + " field formatter must be a function");
567
- obj.formatter = null;
568
- }
569
- return obj;
570
- }
571
-
572
- function normalizeFieldName (fieldName: string | DefineComponent) {
573
- if (fieldName instanceof Object) {
574
- return fieldName;
575
- }
576
-
577
- return fieldName.replace("__", fieldPrefix);
578
- }
579
-
580
- function setData (data: any | null) {
581
- if (data === null || typeof(data) === "undefined") return;
582
-
583
- emit("vuetable:loading");
584
-
585
- if (Array.isArray(data)) {
586
- tableData.value = data;
587
- emit("vuetable:loaded");
588
- return;
589
- }
590
-
591
- tableData.value = getObjectValue(data, dataPath, null);
592
- tablePagination.value = getObjectValue(data, paginationPath, null);
593
-
594
- nextTick( () => {
595
- checkIfRowIdentifierExists();
596
- updateHeader();
597
- emit("vuetable:pagination-data", tablePagination.value);
598
- emit("vuetable:loaded");
599
- });
600
- }
601
-
602
- function checkIfRowIdentifierExists () {
603
- if (!dataIsAvailable.value) {
604
- return;
605
- }
606
-
607
- if (!hasRowIdentifier.value) {
608
- warn("Invalid your data! Use \"track-by\" prop to specify.");
609
- return false;
610
- }
611
-
612
- return true;
613
- }
614
-
615
- function makeTitle (str: string | DefineComponent) {
616
- return isFieldComponent(str) ? "" : titleCase(str.replace(".", " "));
617
- }
618
-
619
- function getFieldTitle (field: Field): string {
620
- if (typeof field.title === "function") {
621
- return field.title();
622
- }
623
-
624
- return field.title;
625
- }
626
-
627
- function renderNormalField (field: Field, item: any) {
628
- return hasFormatter(field) ? callFormatter(field, item) : getObjectValue(item, field.name, "");
629
- }
630
-
631
- function isFieldComponent (fieldName: any) {
632
- if (fieldName instanceof Object) {
633
- // let's assume it is a Vue component
634
- return true;
635
- }
636
-
637
- return fieldName.slice(0, fieldPrefix.length) === fieldPrefix || fieldName.slice(0, 2) === "__";
638
- }
639
-
640
- function isFieldSlot (fieldName: string | DefineComponent) {
641
- return typeof slots[fieldName as string] !== "undefined";
642
- }
643
-
644
- function titleCase (str: string) {
645
- return str.replace(/\w+/g, txt => txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase());
646
- }
647
-
648
- function camelCase (str: string, delimiter = "_") {
649
- return str.split(delimiter).map(item => titleCase(item)).join("");
650
- }
651
-
652
- function loadData (success = loadSuccess, failed = loadFailed) {
653
- if (isDataMode.value) {
654
- handleDataMode();
655
- return;
656
- }
657
-
658
- emit("vuetable:loading");
659
-
660
- optionsHttp.value["params"] = getAppendParams(getAllQueryParams());
661
-
662
- instanceFetch.value = fetch(apiUrl, optionsHttp.value);
663
-
664
- return instanceFetch.value.then(
665
- success,
666
- failed
667
- ).catch(() => failed());
668
- }
669
-
670
- async function fetch (apiUrl: string, httpOptions: Record<string, unknown>): Promise<AxiosResponse> {
671
- if (httpFetch) {
672
- return httpFetch(apiUrl, httpOptions);
673
- }
674
- if (httpMethod === "get") {
675
- return axios.get(apiUrl, httpOptions);
676
- }
677
- else { // Is a POST request
678
- const params = await optionsHttp.value.params;
679
- delete httpOptions.params;
680
- delete optionsHttp.value.params;
681
- return axios.post(apiUrl, params, optionsHttp.value);
682
- }
683
- }
684
-
685
- function loadSuccess (response: AxiosResponse) {
686
- emit("vuetable:load-success", response);
687
-
688
- const body = transform ? transform(response!.data) : response!.data;
689
-
690
- tableData.value = getObjectValue(body, dataPath, null);
691
- tablePagination.value = getObjectValue(body, paginationPath, null);
692
-
693
- if (tablePagination.value === null) {
694
- warn(`vuetable: pagination-path ${ paginationPath } not found.
695
- It looks like the data returned from the server does not have pagination information
696
- or you may have set it incorrectly.\n
697
- You can explicitly suppress this warning by setting pagination-path="".`);
698
- }
699
-
700
- nextTick( () => {
701
- checkIfRowIdentifierExists();
702
- updateHeader();
703
- emit("vuetable:pagination-data", tablePagination.value);
704
- emit("vuetable:loaded");
705
- });
706
- }
707
-
708
- function updateHeader () {
709
- // nextTick doesn't seem to work in all cases. This might be because
710
- // nextTick is finished before the transition element (just my guess)
711
- //
712
- // the scrollHeight value does not yet changed, causing scrollVisible
713
- // to remain "true", therefore, the header gutter never gets updated
714
- // to reflect the display of scrollbar in the table body.
715
- // setTimeout 80ms seems to work in this case.
716
- setTimeout(checkScrollbarVisibility, 80);
717
- }
718
-
719
- function checkScrollbarVisibility () {
720
- nextTick( () => {
721
- let elem = vuetable.value?.$el.getElementsByClassName("vuetable-body-wrapper")[0];
722
- if (elem != null) {
723
- scrollVisible.value = (elem.scrollHeight > elem.clientHeight);
724
- emit("vuetable:scrollbar-visible", scrollVisible.value);
725
- }
726
- });
727
- }
728
-
729
- function loadFailed (response?: AxiosResponse) {
730
- console.error("load-error", response);
731
- emit("vuetable:load-error", response);
732
- emit("vuetable:loaded");
733
- }
734
-
735
- function getAllQueryParams () {
736
- let params: Record<string, string | number> | string | number = {};
737
-
738
- if (typeof queryParams === "function") {
739
- params = queryParams(order.value, currentPage.value, perPage);
740
- return typeof params === "object" ? params : {};
741
- }
742
-
743
- params[queryParams.sort] = getSortParam();
744
- params[queryParams.page] = currentPage.value;
745
- params[queryParams.perPage] = perPage;
746
-
747
- return params;
748
- }
749
-
750
- function getSortParam () {
751
- if (!order.value || order.value[0]?.field == "") {
752
- return "";
753
- }
754
-
755
- if (typeof sortParams === "function") {
756
- return sortParams(order.value);
757
- }
758
-
759
- return getDefaultSortParam();
760
- }
761
-
762
- function getDefaultSortParam () {
763
- return order.value.map(item => `${item.sortField}|${item.direction}`).join(",");
764
- }
765
-
766
- function getAppendParams (params: Record<string, string | number>) {
767
- for (let x in appendParams) {
768
- params[x] = appendParams[x];
769
- }
770
-
771
- return params;
772
- }
773
-
774
- function isSortable (field: Field) {
775
- return field.sortField !== null;
776
- }
777
-
778
- function currentSortOrderPosition (field: Field) {
779
- if (!isSortable(field)) {
780
- return false;
781
- }
782
-
783
- for (let i = 0; i < order.value.length; i++) {
784
- if (fieldIsInSortOrderPosition(field, i)) {
785
- return i;
786
- }
787
- }
788
-
789
- return false;
790
- }
791
-
792
- function fieldIsInSortOrderPosition (field: Field, i: number) {
793
- return order.value[i].field === field.name && order.value[i].sortField === field.sortField;
794
- }
795
-
796
- function orderBy (field: Field, event: Event & { [key: string]: string }) {
797
- if (!isSortable(field) ) return;
798
-
799
- const key = multiSortKey.toLowerCase() + "Key";
800
-
801
- if (multiSort && event[key]) { //adding column to multisort
802
- multiColumnSort(field);
803
- } else {
804
- //no multisort, or resetting sort
805
- singleColumnSort(field);
806
- }
807
-
808
- currentPage.value = firstPage; // reset page index
809
- if (isApiMode.value || dataManager) {
810
- loadData();
811
- }
812
- }
813
-
814
- function addSortColumn (field: Field, direction?: "asc" | "desc") {
815
- order.value.push({
816
- field: field.name as string,
817
- sortField: field.sortField!,
818
- direction: direction || "asc"
819
- });
820
- }
821
-
822
- function removeSortColumn (index: number) {
823
- order.value.splice(index, 1);
824
- }
825
-
826
- function setSortColumnDirection (index: number, direction: "asc" | "desc") {
827
- order.value[index].direction = direction;
828
- }
829
-
830
- function multiColumnSort (field: Field) {
831
- let i = currentSortOrderPosition(field);
832
-
833
- if (i === false) { //this field is not in the sort array yet
834
- addSortColumn(field, "asc");
835
- } else { //this field is in the sort array, now we change its state
836
- if (order.value[i].direction === "asc") {
837
- // switch direction
838
- setSortColumnDirection(i, "desc");
839
- } else {
840
- removeSortColumn(i);
841
- }
842
- }
843
- }
844
-
845
- function singleColumnSort (field: Field) {
846
- if (sortOrder.length === 0) {
847
- // this.clearSortOrder()
848
- addSortColumn(field, "asc");
849
- return;
850
- }
851
-
852
- order.value.splice(1); //removes additional columns
853
-
854
- if (fieldIsInSortOrderPosition(field, 0)) {
855
- // change sort direction
856
- order.value[0].direction = sortOrder[0].direction === "asc" ? "desc" : "asc";
857
- } else {
858
- // reset sort direction
859
- order.value[0].direction = "asc";
860
- }
861
- order.value[0].field = field.name as string;
862
- order.value[0].sortField = field.sortField!;
863
- }
864
-
865
- function clearSortOrder () {
866
- order.value = [];
867
- }
868
-
869
- function hasFormatter (item: Field) {
870
- return typeof item.formatter === "function";
871
- }
872
-
873
- function callFormatter (field: Field, item: any) {
874
- if (!hasFormatter(field)) return;
875
-
876
- if (typeof(field.formatter) === "function") {
877
- return field.formatter(getObjectValue(item, field.name), app?.proxy);
878
- }
879
- }
880
-
881
- function getObjectValue (object: any, path: string | DefineComponent, defaultValue?: any) {
882
- defaultValue = (typeof defaultValue === "undefined") ? null : defaultValue;
883
-
884
- let obj = object;
885
- if (path.trim() != "") {
886
- let keys = path.split(".");
887
- keys.forEach( (key: string) => {
888
- if (obj !== null && typeof obj[key] !== "undefined" && obj[key] !== null) {
889
- obj = obj[key];
890
- } else {
891
- obj = defaultValue;
892
- return;
893
- }
894
- });
895
- }
896
- return obj;
897
- }
898
-
899
- function selectId (key: string) {
900
- if (!isSelectedRow(key)) {
901
- selectedTo.value.push(key);
902
- }
903
- }
904
-
905
- function unselectId (key: string) {
906
- selectedTo.value = selectedTo.value.filter(item => {
907
- return item !== key;
908
- });
909
- }
910
-
911
- function isSelectedRow (key: string) {
912
- return selectedTo.value.indexOf(key) >= 0;
913
- }
914
-
915
- function clearSelectedValues () {
916
- selectedTo.value = [];
917
- }
918
-
919
- function gotoPreviousPage () {
920
- if (currentPage.value > firstPage) {
921
- currentPage.value -= 1;
922
- loadData();
923
- }
924
- }
925
-
926
- function gotoNextPage () {
927
- if (currentPage.value < tablePagination.value!.last_page) {
928
- currentPage.value += 1;
929
- loadData();
930
- }
931
- }
932
-
933
- function gotoPage (page: number) {
934
- if (page != currentPage.value && (page >= firstPage && page <= tablePagination.value!.last_page)) {
935
- currentPage.value = page;
936
- loadData();
937
- }
938
- }
939
-
940
- function isVisibleDetailRow (rowId: string) {
941
- return visibleDetailRows.value.indexOf(rowId) >= 0;
942
- }
943
-
944
- function showDetailRow (rowId: string) {
945
- if (!isVisibleDetailRow(rowId)) {
946
- visibleDetailRows.value.push(rowId);
947
- }
948
- checkScrollbarVisibility();
949
- }
950
-
951
- function hideDetailRow (rowId: string) {
952
- if (isVisibleDetailRow(rowId)) {
953
- visibleDetailRows.value.splice(
954
- visibleDetailRows.value.indexOf(rowId),
955
- 1
956
- );
957
- updateHeader();
958
- }
959
- }
960
-
961
- function toggleDetailRow (rowId: string) {
962
- if (isVisibleDetailRow(rowId)) {
963
- hideDetailRow(rowId);
964
- } else {
965
- showDetailRow(rowId);
966
- }
967
- }
968
-
969
- function showField (index: number) {
970
- if (index < 0 || index > tableFields.value.length) return;
971
-
972
- tableFields.value[index].visible = true;
973
- }
974
-
975
- function hideField (index: number) {
976
- if (index < 0 || index > tableFields.value.length) return;
977
-
978
- tableFields.value[index].visible = false;
979
- }
980
-
981
- function toggleField (index: number) {
982
- if (index < 0 || index > tableFields.value.length) return;
983
-
984
- tableFields.value[index].visible = !tableFields.value[index].visible;
985
- }
986
-
987
- function makePagination (total: number | null = null, per_page: number | null = null, current_page: number | null = null) {
988
- total = total === null ? 0 : total;
989
- per_page = per_page === null ? perPage : per_page;
990
- current_page = current_page === null ? currentPage.value : current_page;
991
-
992
- return {
993
- "total": total,
994
- "per_page": perPage,
995
- "current_page": currentPage.value,
996
- "last_page": Math.ceil(total / perPage) || 0,
997
- "next_page_url": "",
998
- "prev_page_url": "",
999
- "from": (current_page -1) * per_page +1,
1000
- "to": Math.min(current_page * per_page, total)
1001
- };
1002
- }
1003
-
1004
- function normalizeSortOrder () {
1005
- order.value.forEach(item => {
1006
- item.sortField = item.sortField || item.field;
1007
- });
1008
- }
1009
-
1010
- function handleDataMode () {
1011
- // data is array
1012
- if (data !== null && Array.isArray(data)) {
1013
- setData(data);
1014
- return;
1015
- }
1016
-
1017
- // data must be an object, check if dataManager is present
1018
- if (dataManager) {
1019
- callDataManager();
1020
- } else {
1021
- setData(data);
1022
- }
1023
- }
1024
-
1025
- function callDataManager () {
1026
- const result = dataManager!(order.value, makePagination()) as any;
1027
-
1028
- if (isPromiseObject(result)) {
1029
- result.then((data: any[]) => setData(data));
1030
- } else {
1031
- setData(result);
1032
- }
1033
- }
1034
-
1035
- function isObject (unknown: AxiosPromise) {
1036
- return typeof unknown === "object" && unknown !== null;
1037
- }
1038
-
1039
- function isPromiseObject (unknown: any) {
1040
- return isObject(unknown) && typeof(unknown.then) === "function";
1041
- }
1042
-
1043
- function onRowClass (dataItem: any, index: number) {
1044
- if (typeof rowClass === "function") {
1045
- return rowClass(dataItem, index);
1046
- }
1047
-
1048
- return rowClass;
1049
- }
1050
-
1051
- function onDetailRowClass (dataItem: any, index: number): string {
1052
- if (typeof(detailRowClass) === "function") {
1053
- return detailRowClass(dataItem, index);
1054
- }
1055
-
1056
- return detailRowClass;
1057
- }
1058
-
1059
- function onRowClicked (data: any, index: number, event: Event) {
1060
- emit("vuetable:row-clicked", { data, index, event });
1061
- return true;
1062
- }
1063
-
1064
- function onRowDoubleClicked (data: any, index: number, event: Event) {
1065
- emit("vuetable:row-dblclicked", { data, index, event });
1066
- }
1067
-
1068
- function onDetailRowClick (data: any, index: number, event: Event) {
1069
- emit("vuetable:detail-row-clicked", { data, index, event });
1070
- }
1071
-
1072
- function onCellClicked (data: any, index: number, field: Field, event: Event) {
1073
- emit("vuetable:cell-clicked", { data, index, field, event });
1074
- }
1075
-
1076
- function onCellDoubleClicked (data: any, index: number, field: Field, event: Event) {
1077
- emit("vuetable:cell-dblclicked", { data, index, field, event });
1078
- }
1079
-
1080
- function onCellRightClicked (data: any, index: number, field: Field, event: Event) {
1081
- emit("vuetable:cell-rightclicked", { data, index, field, event });
1082
- }
1083
-
1084
- function onMouseOver (data: any, index: number, event: Event) {
1085
- emit("vuetable:row-mouseover", { data, index, event });
1086
- }
1087
-
1088
- function onFieldEvent (type: string, payload: any) {
1089
- emit("vuetable:field-event", type, payload, app?.proxy);
1090
- }
1091
-
1092
- function onHeaderEvent (type: string, payload: any) {
1093
- emit("vuetable:header-event", type, payload, app?.proxy);
1094
- }
1095
-
1096
- function onCheckboxToggled (isChecked: boolean, fieldName: string, dataItem: any) {
1097
- if (dataItem[trackBy] === undefined) {
1098
- warn(`checkbox field: The "${ trackBy }" field does not exist! Make sure the field you specify in "track-by" prop does exist.`);
1099
- return;
1100
- }
1101
-
1102
- const key = dataItem[trackBy];
1103
- if (isChecked) {
1104
- selectId(key);
1105
- } else {
1106
- unselectId(key);
1107
- }
1108
-
1109
- emit("vuetable:checkbox-toggled", isChecked, fieldName);
1110
- }
1111
-
1112
- function onCheckboxToggledAll (isChecked: boolean) {
1113
- if (isChecked) {
1114
- tableData.value?.forEach( (dataItem) => {
1115
- selectId(dataItem[trackBy]);
1116
- });
1117
- } else {
1118
- tableData.value?.forEach( (dataItem) => {
1119
- unselectId(dataItem[trackBy]);
1120
- });
1121
- }
1122
-
1123
- emit("vuetable:checkbox-toggled-all", isChecked);
1124
- }
1125
-
1126
- /*
1127
- * API for externals
1128
- */
1129
- function changePage (page: string | number) {
1130
- if (page === "prev") {
1131
- gotoPreviousPage();
1132
- } else if (page === "next") {
1133
- gotoNextPage();
1134
- } else {
1135
- gotoPage(page as number);
1136
- }
1137
- }
1138
-
1139
- function reload () {
1140
- return loadData();
1141
- }
1142
-
1143
- function refresh () {
1144
- currentPage.value = firstPage;
1145
- return loadData();
1146
- }
1147
-
1148
- function resetData () {
1149
- tableData.value = null;
1150
- tablePagination.value = null;
1151
- emit("vuetable:data-reset");
1152
- }
1153
-
1154
- watch(() => multiSort, value => {
1155
- if (!value && order.value.length > 1) {
1156
- order.value.splice(1);
1157
- loadData();
1158
- }
1159
- });
1160
-
1161
- watch(() => apiUrl, (newValue, oldValue) => reactiveApiUrl && newValue !== oldValue && refresh());
1162
- watch(() => data, setData, { immediate: true });
1163
- watch(() => tableHeight, checkScrollbarVisibility);
1164
- watch(() => fields, normalizeFields, { immediate: true });
1165
- watch(() => perPage, reload);
1166
-
1167
- // No mutation props
1168
- watch(() => sortOrder, value => {
1169
- order.value = value;
1170
- }, { immediate: true });
1171
- watch(() => httpOptions, value => {
1172
- optionsHttp.value = value;
1173
- }, { immediate: true });
1174
- </script>
1175
-
1176
- <style>
1177
- [v-cloak] {
1178
- display: none;
1179
- }
1180
- table.vuetable.fixed-header {
1181
- table-layout: fixed;
1182
- }
1183
- .vuetable th.sortable:hover {
1184
- color: #2185d0;
1185
- cursor: pointer;
1186
- }
1187
- .vuetable-head-wrapper {
1188
- overflow-x: hidden;
1189
- }
1190
- .vuetable-head-wrapper table.vuetable {
1191
- border-bottom-left-radius: 0px;
1192
- border-bottom-right-radius: 0px;
1193
- }
1194
- .vuetable-body-wrapper.fixed-header {
1195
- position:relative;
1196
- overflow-y:auto;
1197
- }
1198
- .vuetable-body-wrapper table.vuetable.fixed-header {
1199
- border-top:none !important;
1200
- margin-top:0 !important;
1201
- border-top-left-radius: 0px;
1202
- border-top-right-radius: 0px;
1203
- }
1204
- .vuetable-empty-result {
1205
- text-align: center;
1206
- }
1207
- </style>