-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVuetifyDatatable.vue
131 lines (115 loc) · 3.72 KB
/
VuetifyDatatable.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
<template>
<v-row>
<v-col cols="12">
<v-form>
<v-text-field
v-model="form.search"
label="search"
/>
<v-checkbox
v-model="form.is_answered"
label="is Answered"
/>
</v-form>
</v-col>
</v-row>
<v-row>
<v-col cols="12">
<v-card
class="py-4"
rounded="lg"
variant="outlined"
>
<template #text>
<v-data-table
v-model:options="vuetifyOptions"
:items="items"
:page="vuetifyOptions.page"
:items-per-page="vuetifyOptions.itemsPerPage"
:sort-by="vuetifyOptions.sortBy"
/>
</template>
</v-card>
</v-col>
</v-row>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import useDatatableUrlSync from '../../../src/useDatatableUrlSync';
import type { GenericDictionnary, VDUSDatatableOptions, VDUSFormSchema, VDUSConfiguration } from '../../../src/utils/VDUSTypes';
// import useDatatableUrlSync from 'vue-datatable-url-sync';
// import type { GenericDictionnary, VDUSDatatableOptions, VDUSFormSchema } from 'vue-datatable-url-sync/src/utils/VDUSTypes';
import fakeData from "../assets/data/data.js";
import { useRoute, useRouter } from "vue-router";
type FakeDataItem = {
id: string;
title: string;
is_answered: boolean;
};
// --------------------- PROPS ------------------------------------
const props = defineProps({
prefix: {
type: String,
default: ""
},
})
// --------------------- DATA ------------------------------------
const form = ref<GenericDictionnary>({
search: "",
is_answered: false
});
const options = ref<VDUSDatatableOptions>({
page: 1,
page_size: 10,
ordering: []
});
const configurations = ref<VDUSConfiguration>({
prefix: props.prefix,
serveurDefaultPageSize: 10,
})
const items = ref<FakeDataItem[]>([]);
const formSchema = ref<VDUSFormSchema>({
is_answered: { type: "boolean" },
});
// --------------------- METHODS ------------------------------------
const filterData = (data: Array<FakeDataItem>, queryAsObject: GenericDictionnary): Array<FakeDataItem> => {
let filteredData = data;
// Filter by search
if (typeof queryAsObject.search !== "undefined") {
filteredData = filteredData.filter(item => {
let respondToFilter = false;
Object.values(item).forEach((value: unknown) => {
const strValue = typeof value === "string" ? value : String(value);
if (strValue.includes(queryAsObject.search)) {
respondToFilter = true;
}
});
return respondToFilter;
});
}
// Filter by is_answered
if (typeof queryAsObject.is_answered !== "undefined" && queryAsObject.is_answered !== null) {
filteredData = filteredData.filter(item => item.is_answered === queryAsObject.is_answered);
}
// Ordering
if (typeof queryAsObject.ordering !== "undefined" && queryAsObject.ordering.length > 0) {
let orderingKey: string = queryAsObject.ordering[0];
const reverse: boolean = orderingKey.startsWith("-");
if (reverse) {
orderingKey = orderingKey.replace("-", "");
}
filteredData = filteredData.sort((a: GenericDictionnary, b: GenericDictionnary) => {
if (reverse) {
return `${a[orderingKey]}`.localeCompare(b[orderingKey]);
}
return `${b[orderingKey]}`.localeCompare(a[orderingKey]);
});
}
return filteredData;
};
const fetchDatas = (queryParams: string, queryAsObject: GenericDictionnary) => {
items.value = filterData(fakeData, queryAsObject);
};
// --------------------- CREATED ------------------------------------
const {vuetifyOptions} = useDatatableUrlSync(useRoute(), useRouter(), form, fetchDatas, options, formSchema.value, null, configurations.value);
</script>