-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correct sorting of numbers in list grid
- Loading branch information
kim.tran
committed
Oct 26, 2023
1 parent
48458d3
commit 50ab81d
Showing
14 changed files
with
1,693 additions
and
1,319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
libs/portal-integration-angular/src/lib/core/components/data-sort-base/data-sort-base.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import { TranslateService } from '@ngx-translate/core' | ||
import { Observable, map, of } from 'rxjs' | ||
import { flattenObject } from '../../../functions/flatten-object' | ||
import { ColumnType } from '../../../model/column-type.model' | ||
import { DataSortDirection } from '../../../model/data-sort-direction' | ||
import { DataTableColumn } from '../../../model/data-table-column.model' | ||
import { ListGridData } from '../../components/data-list-grid/data-list-grid.component' | ||
import { Row, Filter } from '../../components/data-table/data-table.component' | ||
import { ObjectUtils } from '../../utils/objectutils' | ||
|
||
type RowListGridData = ListGridData | Row | ||
|
||
export class DataSortBase { | ||
constructor(protected locale: string, protected translateService: TranslateService) {} | ||
|
||
translateItems( | ||
[items, filters, sortColumn, sortDirection]: [ | ||
RowListGridData[], | ||
Filter[], | ||
string, | ||
DataSortDirection, | ||
], | ||
columns: DataTableColumn[], | ||
clientSideFiltering: boolean, | ||
clientSideSorting: boolean | ||
): Observable<[RowListGridData[], Filter[], string, DataSortDirection, Record<string, Record<string, string>>]> { | ||
if (clientSideFiltering || clientSideSorting) { | ||
let translationKeys: string[] = [] | ||
const translatedColumns = columns.filter((c) => c.columnType === ColumnType.TRANSLATION_KEY) | ||
translatedColumns.forEach((c) => { | ||
translationKeys = [...translationKeys, ...items.map((i) => ObjectUtils.resolveFieldData(i, c.id)?.toString())] | ||
}) | ||
if (translationKeys.length) { | ||
return this.translateService.get(translationKeys).pipe( | ||
map((translatedValues: Record<string, string>) => { | ||
const translations: Record<string, Record<string, string>> = {} | ||
translatedColumns.forEach((c) => { | ||
translations[c.id] = Object.fromEntries( | ||
items.map((i) => [ | ||
ObjectUtils.resolveFieldData(i, c.id)?.toString() || '', | ||
translatedValues[ObjectUtils.resolveFieldData(i, c.id)?.toString()], | ||
]) | ||
) | ||
}) | ||
return [items, filters, sortColumn, sortDirection, translations] | ||
}) | ||
) | ||
} | ||
} | ||
return of([items, filters, sortColumn, sortDirection, {}]) | ||
} | ||
|
||
filterItems( | ||
[items, filters, sortColumn, sortDirection, translations]: [ | ||
RowListGridData[], | ||
Filter[], | ||
string, | ||
DataSortDirection, | ||
Record<string, Record<string, string>> | ||
], | ||
clientSideFiltering: boolean | ||
): [RowListGridData[], Filter[], string, DataSortDirection, Record<string, Record<string, string>>] { | ||
if (!clientSideFiltering) { | ||
return [items, filters, sortColumn, sortDirection, translations] | ||
} | ||
return [ | ||
items.filter((item) => | ||
filters | ||
.map((filter) => filter.columnId) | ||
.filter((value, index, self) => self.indexOf(value) === index && value != null) | ||
.every((filterColumnId) => | ||
filters | ||
.filter((filter) => filter.columnId === filterColumnId) | ||
.some( | ||
(filter) => | ||
( | ||
translations[filter.columnId]?.[ObjectUtils.resolveFieldData(item, filter.columnId).toString()] || | ||
ObjectUtils.resolveFieldData(item, filter.columnId) | ||
).toString() === filter.value.toString() | ||
) | ||
) | ||
), | ||
filters, | ||
sortColumn, | ||
sortDirection, | ||
translations, | ||
] | ||
} | ||
|
||
sortItems( | ||
[items, filters, sortColumn, sortDirection, translations]: [ | ||
RowListGridData[], | ||
Filter[], | ||
string, | ||
DataSortDirection, | ||
Record<string, Record<string, string>> | ||
], | ||
columns: DataTableColumn[], | ||
clientSideSorting: boolean | ||
): [RowListGridData[], Filter[], string, DataSortDirection, Record<string, Record<string, string>>] { | ||
if (!clientSideSorting || sortColumn === '') { | ||
return [items, filters, sortColumn, sortDirection, translations] | ||
} | ||
let translatedColValues: Record<string, string> = Object.fromEntries( | ||
items.map((i) => [ | ||
ObjectUtils.resolveFieldData(i, sortColumn)?.toString(), | ||
ObjectUtils.resolveFieldData(i, sortColumn)?.toString(), | ||
]) | ||
) | ||
if (columns.find((h) => h.id === sortColumn)?.columnType === ColumnType.TRANSLATION_KEY) { | ||
translatedColValues = translations[sortColumn] | ||
} | ||
return [ | ||
[...items].sort(this.createCompareFunction(translatedColValues, sortColumn, sortDirection)), | ||
filters, | ||
sortColumn, | ||
sortDirection, | ||
translations, | ||
] | ||
} | ||
|
||
flattenItems(items: RowListGridData[]) { | ||
return items.map((i) => flattenObject(i)) | ||
} | ||
|
||
createCompareFunction( | ||
translatedColValues: Record<string, string>, | ||
sortColumn: string, | ||
sortDirection: DataSortDirection, | ||
): (a: Record<string, any>, b: Record<string, any>) => number { | ||
let direction = 0 | ||
if (sortDirection === DataSortDirection.ASCENDING) { | ||
direction = 1 | ||
} else if (sortDirection === DataSortDirection.DESCENDING) { | ||
direction = -1 | ||
} | ||
return (data1, data2) => { | ||
if (direction === 0) { | ||
return 0 | ||
} | ||
let result | ||
const value1 = translatedColValues[ObjectUtils.resolveFieldData(data1, sortColumn)] | ||
const value2 = translatedColValues[ObjectUtils.resolveFieldData(data2, sortColumn)] | ||
|
||
if (value1 == null && value2 != null) result = -1 | ||
else if (value1 != null && value2 == null) result = 1 | ||
else if (value1 == null && value2 == null) result = 0 | ||
else if (typeof value1 === 'string' && typeof value2 === 'string') | ||
result = value1.localeCompare(value2, [this.locale, 'en', 'de'], { numeric: true }) | ||
else { | ||
if (value1 < value2) { | ||
result = -1 | ||
} else if (value1 > value2) { | ||
result = 1 | ||
} else { | ||
result = 0 | ||
} | ||
} | ||
return direction * result | ||
} | ||
} | ||
} |
Oops, something went wrong.