Add option for custom group order to data table (#20582)

This commit is contained in:
Bram Kragten 2024-04-22 18:28:50 +02:00 committed by GitHub
parent eb1354d229
commit 1b54d51e4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 4 deletions

View File

@ -133,6 +133,8 @@ export class HaDataTable extends LitElement {
@property() public groupColumn?: string;
@property({ attribute: false }) public groupOrder?: string[];
@property() public sortColumn?: string;
@property() public sortDirection: SortingDirection = null;
@ -254,6 +256,7 @@ export class HaDataTable extends LitElement {
properties.has("sortColumn") ||
properties.has("sortDirection") ||
properties.has("groupColumn") ||
properties.has("groupOrder") ||
properties.has("_collapsedGroups")
) {
this._sortFilterData();
@ -530,13 +533,24 @@ export class HaDataTable extends LitElement {
const sorted: {
[key: string]: DataTableRowData[];
} = Object.keys(grouped)
.sort((a, b) =>
stringCompare(
.sort((a, b) => {
const orderA = this.groupOrder?.indexOf(a) ?? -1;
const orderB = this.groupOrder?.indexOf(b) ?? -1;
if (orderA !== orderB) {
if (orderA === -1) {
return 1;
}
if (orderB === -1) {
return -1;
}
return orderA - orderB;
}
return stringCompare(
["", "-", "—"].includes(a) ? "zzz" : a,
["", "-", "—"].includes(b) ? "zzz" : b,
this.hass.locale.language
)
)
);
})
.reduce((obj, key) => {
obj[key] = grouped[key];
return obj;

View File

@ -165,6 +165,8 @@ export class HaTabsSubpageDataTable extends LitElement {
@property() public initialGroupColumn?: string;
@property({ attribute: false }) public groupOrder?: string[];
@state() private _sortColumn?: string;
@state() private _sortDirection: SortingDirection = null;
@ -422,6 +424,7 @@ export class HaTabsSubpageDataTable extends LitElement {
.sortColumn=${this._sortColumn}
.sortDirection=${this._sortDirection}
.groupColumn=${this._groupColumn}
.groupOrder=${this.groupOrder}
>
${!this.narrow
? html`