@imaginario27/air-ui-utils 1.0.13 → 1.0.14
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.
- package/composables/useTable.ts +36 -0
- package/package.json +1 -1
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// composables/useTable.ts
|
|
2
|
+
|
|
3
|
+
export function useTable<T extends Record<string, string>>(data: Ref<T[]>) {
|
|
4
|
+
const sortKey = ref<keyof T | ''>('')
|
|
5
|
+
const sortAsc = ref(true)
|
|
6
|
+
|
|
7
|
+
const toggleSort = (key: keyof T) => {
|
|
8
|
+
if (sortKey.value === key) {
|
|
9
|
+
sortAsc.value = !sortAsc.value
|
|
10
|
+
} else {
|
|
11
|
+
sortKey.value = key
|
|
12
|
+
sortAsc.value = true
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const sortedData = computed(() => {
|
|
17
|
+
if (!sortKey.value) return data.value
|
|
18
|
+
|
|
19
|
+
return [...data.value].sort((a, b) => {
|
|
20
|
+
const key = sortKey.value
|
|
21
|
+
const valA = (a[key] ?? '').toLowerCase()
|
|
22
|
+
const valB = (b[key] ?? '').toLowerCase()
|
|
23
|
+
|
|
24
|
+
if (valA < valB) return sortAsc.value ? -1 : 1
|
|
25
|
+
if (valA > valB) return sortAsc.value ? 1 : -1
|
|
26
|
+
return 0
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
sortKey,
|
|
32
|
+
sortAsc,
|
|
33
|
+
toggleSort,
|
|
34
|
+
sortedData,
|
|
35
|
+
}
|
|
36
|
+
}
|