Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit 47ba5af

Browse files
authored
Merge pull request #9 from davidnixon/fix-data-sort
fix: add code for the event data sort to fix #6
2 parents 72e15f7 + edb772c commit 47ba5af

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

composables/useObject.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Substitute for lodash object _get
2+
// Here is a nice one-liner from the article "Safely Accessing Deeply Nested Values In JavaScript"
3+
export const useObject = () => {
4+
return {
5+
_get: (object, path, defval = null) => {
6+
if (typeof path === 'string') path = path.split('.')
7+
return path.reduce((xs, x) => (xs && xs[x] ? xs[x] : defval), object)
8+
},
9+
}
10+
}

pages/Events.vue

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ const i18nPagination = computed(() => {
2323
}
2424
})
2525
26-
const sortKeys = ref({ index: '0', order: 'none', name: null })
27-
function onSort(keys) {
28-
sortKeys.value = keys
29-
}
30-
3126
const searchFilter = ref('')
3227
/**
3328
* Set search filter
@@ -36,8 +31,11 @@ const searchFilter = ref('')
3631
function onSearch(val) {
3732
searchFilter.value = val?.trim()
3833
}
34+
35+
const { _get } = useObject()
3936
const showHidden = ref(false)
4037
const hiddenItems = ref(new Set())
38+
const sortKeys = ref({ index: '0', order: 'none', name: null })
4139
const filteredData = computed(() => {
4240
// start with all the data
4341
let show = marveEventsList.value || []
@@ -54,19 +52,31 @@ const filteredData = computed(() => {
5452
// If we are sorting the data, do that here
5553
if (sortKeys.value.order !== 'none') {
5654
show.sort((a, b) => {
57-
const _a = a[sortKeys.value.name] // title or characters
58-
const _b = b[sortKeys.value.name] // title or characters
55+
// This code depends on the name value in cv-data-table-heading matching the path value of the
56+
// data to sort. So, `<cv-data-table-heading name="something" .../>` would mean that we expect to find a key
57+
// "something" in the data. A path is ok too like "hello.world".
58+
// {
59+
// "something": 1,
60+
// "hello": { "world": 5 },
61+
// ...
62+
// }
63+
let _a = _get(a, sortKeys.value.name, 0) // value of sort key
64+
let _b = _get(b, sortKeys.value.name, 0)
5965
let cmp = 0
60-
// sort by number of characters (or some other number value that may get added later)
66+
if (typeof _a !== typeof _b) {
67+
if (typeof _a === 'string') _b = ''
68+
else if (typeof _b === 'string') _a = ''
69+
else console.warn('sort values have different types')
70+
}
71+
// sort by a number value
6172
if (typeof _a === 'number') {
6273
cmp = _a - _b
6374
}
64-
// or sort by name
65-
else if (sortKeys.value.name === 'title') {
66-
const nameA = _a.title || ''
67-
const nameB = _b.title || ''
68-
cmp = nameA.localeCompare(nameB, locale)
75+
// or sort by a string
76+
else if (typeof _a === 'string') {
77+
cmp = _a.localeCompare(_b, locale)
6978
}
79+
7080
// reverse the sort
7181
if (sortKeys.value.order === 'descending') cmp = -cmp
7282
return cmp
@@ -79,7 +89,7 @@ function toggleShowAll() {
7989
showHidden.value = !showHidden.value
8090
}
8191
82-
const currentPagination = ref({ start: 1, length: 7 })
92+
const currentPagination = ref({ start: 1, page: 1, length: 7 })
8393
const paginated = computed(() => {
8494
const change = currentPagination.value
8595
return filteredData.value.slice(
@@ -90,6 +100,11 @@ const paginated = computed(() => {
90100
function onPagination(change) {
91101
currentPagination.value = change
92102
}
103+
function onSort(keys) {
104+
sortKeys.value = keys
105+
onPagination({ ...currentPagination.value, start: 1, page: 1 })
106+
}
107+
93108
const selectedItems = ref([])
94109
function onHideSelected() {
95110
for (let i = 0; i < selectedItems.value.length; i++) {
@@ -222,15 +237,18 @@ provide('show-description', showDescription)
222237
id="heading-duration"
223238
:heading="$t('Duration')"
224239
name="duration"
225-
sortable
226240
/>
227241
<cv-data-table-heading
228242
id="heading-characters"
229243
:heading="$t('characters')"
244+
name="characters.available"
245+
sortable
230246
/>
231247
<cv-data-table-heading
232248
id="heading-comics"
233249
:heading="$t('comics')"
250+
name="comics.available"
251+
sortable
234252
/>
235253
</template>
236254
<template #data>

0 commit comments

Comments
 (0)