mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 03:06:41 +00:00
Sort data using locale in data tables (#17781)
* Sort data using locale in data tables * Only use string compare is both values are string
This commit is contained in:
parent
5da4e1860a
commit
fe3a63af80
@ -458,7 +458,8 @@ export class HaDataTable extends LitElement {
|
|||||||
filteredData,
|
filteredData,
|
||||||
this._sortColumns[this._sortColumn],
|
this._sortColumns[this._sortColumn],
|
||||||
this._sortDirection,
|
this._sortDirection,
|
||||||
this._sortColumn
|
this._sortColumn,
|
||||||
|
this.hass.locale.language
|
||||||
)
|
)
|
||||||
: filteredData;
|
: filteredData;
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// To use comlink under ES5
|
// To use comlink under ES5
|
||||||
import "proxy-polyfill";
|
|
||||||
import { expose } from "comlink";
|
import { expose } from "comlink";
|
||||||
|
import "proxy-polyfill";
|
||||||
|
import { stringCompare } from "../../common/string/compare";
|
||||||
import type {
|
import type {
|
||||||
ClonedDataTableColumnData,
|
ClonedDataTableColumnData,
|
||||||
DataTableRowData,
|
DataTableRowData,
|
||||||
@ -39,7 +40,8 @@ const sortData = (
|
|||||||
data: DataTableRowData[],
|
data: DataTableRowData[],
|
||||||
column: ClonedDataTableColumnData,
|
column: ClonedDataTableColumnData,
|
||||||
direction: SortingDirection,
|
direction: SortingDirection,
|
||||||
sortColumn: string
|
sortColumn: string,
|
||||||
|
language?: string
|
||||||
) =>
|
) =>
|
||||||
data.sort((a, b) => {
|
data.sort((a, b) => {
|
||||||
let sort = 1;
|
let sort = 1;
|
||||||
@ -58,13 +60,8 @@ const sortData = (
|
|||||||
if (column.type === "numeric") {
|
if (column.type === "numeric") {
|
||||||
valA = isNaN(valA) ? undefined : Number(valA);
|
valA = isNaN(valA) ? undefined : Number(valA);
|
||||||
valB = isNaN(valB) ? undefined : Number(valB);
|
valB = isNaN(valB) ? undefined : Number(valB);
|
||||||
} else {
|
} else if (typeof valA === "string" && typeof valB === "string") {
|
||||||
if (typeof valA === "string") {
|
return sort * stringCompare(valA, valB, language);
|
||||||
valA = valA.toUpperCase();
|
|
||||||
}
|
|
||||||
if (typeof valB === "string") {
|
|
||||||
valB = valB.toUpperCase();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure "undefined" and "null" are always sorted to the bottom
|
// Ensure "undefined" and "null" are always sorted to the bottom
|
||||||
|
@ -27,10 +27,12 @@ export const filterData = (
|
|||||||
filter: FilterDataParamTypes[2]
|
filter: FilterDataParamTypes[2]
|
||||||
): Promise<ReturnType<FilterDataType>> =>
|
): Promise<ReturnType<FilterDataType>> =>
|
||||||
getWorker().filterData(data, columns, filter);
|
getWorker().filterData(data, columns, filter);
|
||||||
|
|
||||||
export const sortData = (
|
export const sortData = (
|
||||||
data: SortDataParamTypes[0],
|
data: SortDataParamTypes[0],
|
||||||
columns: SortDataParamTypes[1],
|
columns: SortDataParamTypes[1],
|
||||||
direction: SortDataParamTypes[2],
|
direction: SortDataParamTypes[2],
|
||||||
sortColumn: SortDataParamTypes[3]
|
sortColumn: SortDataParamTypes[3],
|
||||||
|
language?: SortDataParamTypes[4]
|
||||||
): Promise<ReturnType<SortDataType>> =>
|
): Promise<ReturnType<SortDataType>> =>
|
||||||
getWorker().sortData(data, columns, direction, sortColumn);
|
getWorker().sortData(data, columns, direction, sortColumn, language);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user