-
Notifications
You must be signed in to change notification settings - Fork 59
Replace tablesorter lib with custom sorter #1435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c76e203
Replace tablesorter lib with custom sorter
FelixPflaum 7a9c0fc
Remove commented out remnant
FelixPflaum 4475b54
Apply some suggestions, minor tweaks
FelixPflaum 27b1e7b
Use generic for querySelector
FelixPflaum 1b6c1c0
Regex replace typecasting to generics for all querySelector usage
FelixPflaum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
104 changes: 104 additions & 0 deletions
104
ui/core/components/detailed_results/metrics_table/table_sorter.ts
This file contains hidden or 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,104 @@ | ||
| type TableSorterRowData = { | ||
| readonly values: ReadonlyArray<string | number>; | ||
| readonly rowElement: HTMLTableRowElement; | ||
| }; | ||
|
|
||
| type TableSorterConfig = { | ||
| tableHead: HTMLTableRowElement; | ||
| tableBody: HTMLTableSectionElement; | ||
| dataSetKey: string; | ||
| childRowClass: string; | ||
| defaultSortCol: number; | ||
| defaultSortDesc: boolean; | ||
| }; | ||
|
|
||
| export class TableSorter { | ||
| private readonly cfg: Readonly<TableSorterConfig>; | ||
| private readonly rowData: Array<TableSorterRowData & { children?: Array<TableSorterRowData> }> = []; | ||
| private sortCol = -1; | ||
| private sortDesc: Array<boolean>; | ||
|
|
||
| constructor(config: TableSorterConfig) { | ||
| if (config.tableHead.cells[config.defaultSortCol] === undefined) throw new Error('Default sort column must be a valid header cell index!'); | ||
|
|
||
| this.cfg = config; | ||
|
|
||
| this.sortCol = this.cfg.defaultSortCol; | ||
| this.sortDesc = Array(config.tableHead.cells.length).fill(true); | ||
| this.sortDesc[config.defaultSortCol] = config.defaultSortDesc; | ||
|
|
||
| Array.from(config.tableHead.cells).forEach((cell, i) => { | ||
| cell.addEventListener('click', () => this.setSort(i)); | ||
| }); | ||
| } | ||
|
|
||
| private sortFunc = (a: TableSorterRowData, b: TableSorterRowData) => { | ||
| const aValue = a.values[this.sortCol]; | ||
| const bValue = b.values[this.sortCol]; | ||
| const asc = !this.sortDesc[this.sortCol]; | ||
| if (typeof aValue === 'number' && typeof bValue === 'number') { | ||
| return asc ? aValue - bValue : bValue - aValue; | ||
| } else { | ||
| return asc ? aValue.toString().localeCompare(bValue.toString()) : bValue.toString().localeCompare(aValue.toString()); | ||
| } | ||
| }; | ||
|
|
||
| private sort() { | ||
| if (!this.rowData.length || !(this.sortCol in this.rowData[0].values)) return; | ||
|
|
||
| const sortedRowElems: Array<HTMLTableRowElement> = []; | ||
|
|
||
| this.rowData.sort(this.sortFunc); | ||
| for (const row of this.rowData) { | ||
| sortedRowElems.push(row.rowElement); | ||
| if (row.children) { | ||
| row.children.sort(this.sortFunc); | ||
| sortedRowElems.push(...row.children.map(v => v.rowElement)); | ||
| } | ||
| } | ||
|
|
||
| this.cfg.tableBody.replaceChildren(...sortedRowElems); | ||
| } | ||
|
|
||
| /** | ||
| * Set column to sort by. If set to the current sort column the order will be reversed. | ||
| * @param column If omitted use default column. | ||
| */ | ||
| setSort(column = -1) { | ||
| if (this.sortDesc[column] === undefined) column = this.cfg.defaultSortCol; | ||
| this.sortDesc[column] = !this.sortDesc[column]; | ||
| this.sortCol = column; | ||
| this.sort(); | ||
| } | ||
|
|
||
| private parseRowValues(rowElement: HTMLTableRowElement): Array<number | string> { | ||
| const values: Array<string | number> = []; | ||
| for (const cell of rowElement.cells) { | ||
| const val = cell.dataset[this.cfg.dataSetKey] ?? cell.innerText; | ||
| const numVal = parseFloat(val); | ||
| values.push(!isNaN(numVal) ? numVal : val); | ||
| } | ||
| return values; | ||
| } | ||
|
|
||
| /** | ||
| * Update internal data structure for changed table data. | ||
| */ | ||
| update() { | ||
| this.rowData.length = 0; | ||
|
|
||
| for (const rowElement of this.cfg.tableBody.rows) { | ||
| const values = this.parseRowValues(rowElement); | ||
| if (!rowElement.classList.contains(this.cfg.childRowClass)) { | ||
| this.rowData.push({ values, rowElement }); | ||
| } else { | ||
| const parentData = this.rowData[this.rowData.length - 1]; | ||
| if (!parentData) throw new Error('Child row has no parent!'); | ||
| if (!parentData.children) parentData.children = []; | ||
| parentData.children.push({ values, rowElement }); | ||
| } | ||
| } | ||
|
|
||
| this.sort(); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.