Skip to content

Commit a14d0ce

Browse files
committed
vuetify options computed helper coded
1 parent 2507664 commit a14d0ce

File tree

7 files changed

+132
-12
lines changed

7 files changed

+132
-12
lines changed

src/useDatatableUrlSync.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import {
44
generateQueryFromObject,
55
convertParamIfTypeInSchema,
66
getRemovedKeyBetweenTwoObject,
7-
//getDefaultValueForParam
7+
getDefaultValueForParam
88
} from "./utils/listPaginatedTools";
9+
import {
10+
getSortsArrayFromOrdering,
11+
getOrderingFromSortArray
12+
} from "./utils/helpers";
913
import cloneDeep from "lodash.clonedeep";
1014
import isEqual from "lodash.isequal";
1115

12-
import { ref, watch, nextTick } from 'vue-demi'
16+
import { ref, watch, nextTick, computed } from 'vue-demi'
1317
import { useRoute, useRouter } from 'vue-router'
14-
import {GenericDictionnary, VDUSConfiguration} from "./utils/VDUSTypes"
18+
import {GenericDictionnary, VDUSConfiguration, VuetifyOptions} from "./utils/VDUSTypes"
1519

1620
/*
1721
DOC here on params and return value
@@ -50,6 +54,45 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
5054
localQuery = triggerSearchIfNeeded(isFilter, getDatas);
5155
}, configurations?.debounceTime || 0);
5256

57+
// ----------------------------- COMPUTED ---------------------------
58+
59+
const vuetifyOptions = computed({
60+
get: ():VuetifyOptions => {
61+
let vuetifyOptions:VuetifyOptions = {
62+
page: 1,
63+
itemsPerPage: 10,
64+
sortBy: [],
65+
sortDesc: [],
66+
groupBy: [],
67+
groupDesc: [],
68+
multiSort: false,
69+
mustSort: false
70+
};
71+
72+
vuetifyOptions.page = options.page ?? getDefaultValueForParam("page", formSchema);
73+
vuetifyOptions.itemsPerPage = options.page_size ?? getDefaultValueForParam("page_size", formSchema);
74+
75+
let ordering:Array<string> =
76+
Array.isArray(options.ordering) &&
77+
options.ordering.length > 0
78+
? options.ordering
79+
: getDefaultValueForParam("ordering", formSchema);
80+
81+
let { sortBy, sortDesc } = getSortsArrayFromOrdering(ordering);
82+
83+
vuetifyOptions.sortBy = sortBy;
84+
vuetifyOptions.sortDesc = sortDesc;
85+
86+
return vuetifyOptions;
87+
},
88+
set: (newOptions:VuetifyOptions) => {
89+
// As we do not define options by default, to avoid reload from other component we doesn't want, we need to set data because they are not reactive
90+
options.page = newOptions.page;
91+
options.page_size = newOptions.itemsPerPage;
92+
options.ordering = getOrderingFromSortArray(newOptions.sortBy, newOptions.sortDesc)
93+
}
94+
})
95+
5396
// ----------------------------- WATCH ------------------------------
5497
watch(form, () => {
5598
debounceSearch(true);

src/utils/VDUSTypes.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,20 @@ type VDUSConfiguration = {
88
extraQueryParams: GenericDictionnary
99
}
1010

11-
export {GenericDictionnary, VDUSConfiguration}
11+
type VuetifySortArraysObject = {
12+
sortBy: Array<string>,
13+
sortDesc: Array<boolean>
14+
}
15+
16+
type VuetifyOptions = {
17+
page: number,
18+
itemsPerPage: number,
19+
sortBy: Array<string>,
20+
sortDesc: Array<boolean>,
21+
groupBy: Array<string>,
22+
groupDesc: Array<boolean>,
23+
multiSort: boolean,
24+
mustSort: boolean
25+
}
26+
27+
export {GenericDictionnary, VDUSConfiguration, VuetifySortArraysObject, VuetifyOptions}

src/utils/helpers.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
1+
import {VuetifySortArraysObject} from "./VDUSTypes";
2+
13
export const elementToArrayOfInt = (element: any):Array<number> => {
24
return ["number", "string"].includes(typeof element)
35
? [parseInt(element)]
46
: element.map((item:any) => parseInt(item));
57
};
68

7-
export const elementToArrayOfString = (element: any) => {
9+
export const elementToArrayOfString = (element: any): Array<string> => {
810
return element ? (typeof element === "string" ? [element] : element) : [];
911
};
1012

11-
export const extractBooleanValue = (value: any, defaultValue:Boolean = true) => {
13+
export const extractBooleanValue = (value: any, defaultValue:Boolean = true):Boolean => {
1214
return value ? value.toString() === "true" : defaultValue;
1315
};
1416

15-
export const extractIntegerValue = (value:any, defaultValue:number = 0) => {
17+
export const extractIntegerValue = (value:any, defaultValue:number = 0):number => {
1618
const parsed = parseInt(value);
1719
return isNaN(parsed) ? defaultValue : parsed;
1820
};
21+
22+
export const getSortsArrayFromOrdering = (ordering:Array<string>):VuetifySortArraysObject => {
23+
if (!ordering) {
24+
return { sortBy: [], sortDesc: [] };
25+
}
26+
let sortBy:Array<string> = [];
27+
let sortDesc:Array<boolean> = [];
28+
29+
ordering.forEach(orderItem => {
30+
let isDesc:boolean = false;
31+
if (orderItem.startsWith("-")) {
32+
orderItem = orderItem.replace("-", "");
33+
isDesc = true;
34+
}
35+
sortBy.push(orderItem);
36+
sortDesc.push(isDesc);
37+
});
38+
39+
return { sortBy, sortDesc };
40+
}
41+
42+
export const getOrderingFromSortArray = (sortBy:Array<string>, sortDesc:Array<boolean>):Array<string> => {
43+
let ordering:Array<string> = [];
44+
sortBy.forEach((orderItem, index) => {
45+
let isDesc:boolean = true;
46+
if (sortDesc.length > index) {
47+
isDesc = sortDesc[index];
48+
}
49+
ordering.push(`${isDesc ? "-" : ""}${orderItem}`);
50+
});
51+
return ordering;
52+
}

vue2-example/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vue2-example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"@vue/composition-api": "^1.0.0-rc.5",
1212
"core-js": "^3.6.5",
1313
"vue": "^2.6.11",
14-
"vue-datatable-url-sync": "^1.0.2",
14+
"vue-datatable-url-sync": "^1.0.3",
1515
"vue-router": "^3.2.0",
1616
"vuetify": "^2.4.0"
1717
},

vue2-example/src/components/HelloWorld.vue

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
<template>
22
<v-container>
33
<v-row class="text-center">
4+
<v-col cols="12">
5+
<v-text-field
6+
v-model="search"
7+
append-icon="mdi-magnify"
8+
label="Search"
9+
single-line
10+
hide-details
11+
></v-text-field>
12+
</v-col>
413
<v-col cols="12">
514
<v-data-table
615
:headers="headers"
716
:items="fakeData"
8-
:items-per-page="5"
17+
:options.sync="vuetifyOptions"
918
class="elevation-1"
19+
:search="search"
1020
></v-data-table>
1121
</v-col>
1222
</v-row>
@@ -16,6 +26,8 @@
1626
<script>
1727
import useDatatableUrlSync from 'vue-datatable-url-sync';
1828
import fakeData from "./data";
29+
import { ref } from '@vue/composition-api'
30+
1931
export default {
2032
name: 'HelloWorld',
2133
@@ -26,5 +38,17 @@ export default {
2638
],
2739
fakeData
2840
}),
41+
setup () {
42+
// --------------------- DATA ------------------------------------
43+
const form = ref({
44+
search: ""
45+
})
46+
const options = ref({
47+
page: 1,
48+
page_size: 10,
49+
ordering: []
50+
})
51+
const items = ref<any>([])
52+
}
2953
}
3054
</script>

vue2-example/src/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import Vue from 'vue'
22
import App from './App.vue'
33
import router from './router'
44
import vuetify from './plugins/vuetify';
5+
import VueCompositionAPI from '@vue/composition-api'
6+
7+
Vue.use(VueCompositionAPI)
58

69
Vue.config.productionTip = false
710

0 commit comments

Comments
 (0)