My goal is to be able to sort DEST or ASC the table. To do this, in the array 'tableItems' I get unformatted numbers, which I format to have the dot for thousands and the comma for decimals with the 'numericValues' method. This method returns numeric data, but in strings.
numericValues() {
const numberFormat = new Intl.NumberFormat('de');
this.tableItems.forEach((item) => {
for (const key in item) {
if (typeof item[key] === 'number') {
const formattedValue = numberFormat.format(item[key]);
item[key] = formattedValue;
}
}
});
}
I would like to be able to sort the table these strings but as if they were numbers. To do this, I have added the line ':custom-sort="customSort"' in my template.
The problem of having added the 'customSort' method, is that sort no longer works in my table, when I click on the headers
So, I show what I have tried to do with the method I add in custom-sort:
This is my template:
<v-data-table
:headers="tableHeaders"
:items="tableItems"
:items-per-page="18"
:search="search"
class="elevation-1"
:style="getTableHeight()"
:custom-sort="customSort"
></v-data-table>
This is my method customSort:
customSort(tableItems: any[], index: number, isDescending: boolean): any[] {
return tableItems.sort((a, b) => {
console.log(a)
const valueA = a;
const valueB = b;
if (typeof valueA === 'string' && typeof valueB === 'string') {
const numA = Number(valueA);
const numB = Number(valueB);
if (!isNaN(numA) && !isNaN(numB)) {
return isDescending ? numB - numA : numA - numB;
}
}
return isDescending ? String(valueB).localeCompare(valueA) : String(valueA).localeCompare(valueB);
});
}
Can anyone give me some advice on what I am doing wrong?