Skip to content

Commit 79dfe03

Browse files
authored
Merge pull request #34 from HC200ok/enhance/support-nested-header-value
Fix: support nested sorting
2 parents 1b4aa3e + b6edbe3 commit 79dfe03

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"author": "HC200ok",
44
"description": "A customizable and easy-to-use data table component made with Vue.js 3.x.",
55
"private": false,
6-
"version": "1.2.7",
6+
"version": "1.2.8",
77
"types": "./types/main.d.ts",
88
"license": "MIT",
99
"files": [

src/components/DataTable.vue

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -453,16 +453,20 @@ const headersForRender = computed((): HeaderForRender[] => {
453453
454454
const headerColumns = computed((): string[] => headersForRender.value.map((header) => header.value));
455455
456-
const generateColumnContent = (column: string, item: Item) => {
457-
let content: any = '';
456+
const getItemValue = (column: string, item: Item) => {
458457
if (column.includes('.')) {
459-
const propertyArr = column.split('.');
460-
propertyArr.forEach((property, index) => {
461-
content = (index === 0 ? item[property] : content[property]);
458+
let content: any = '';
459+
const keysArr = column.split('.');
460+
keysArr.forEach((key, index) => {
461+
content = (index === 0 ? item[key] : content[key]);
462462
});
463-
} else {
464-
content = item[column];
463+
return content;
465464
}
465+
return item[column];
466+
};
467+
468+
const generateColumnContent = (column: string, item: Item) => {
469+
const content = getItemValue(column, item);
466470
return Array.isArray(content) ? content.join(',') : content;
467471
};
468472
@@ -597,8 +601,8 @@ const itemsSorting = computed((): Item[] => {
597601
const itemsFilteringSorted = [...itemsFiltering.value];
598602
// eslint-disable-next-line vue/no-side-effects-in-computed-properties
599603
return itemsFilteringSorted.sort((a, b) => {
600-
if (a[sortBy] < b[sortBy]) return sortDesc ? 1 : -1;
601-
if (a[sortBy] > b[sortBy]) return sortDesc ? -1 : 1;
604+
if (getItemValue(sortBy, a) < getItemValue(sortBy, b)) return sortDesc ? 1 : -1;
605+
if (getItemValue(sortBy, a) > getItemValue(sortBy, b)) return sortDesc ? -1 : 1;
602606
return 0;
603607
});
604608
});

0 commit comments

Comments
 (0)