vuetable-next 1.0.10 → 1.0.12

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