-
Notifications
You must be signed in to change notification settings - Fork 149
feature: sort rows by column value #514
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,30 @@ | |
| .items-table td:first-child { | ||
| background-color: white; | ||
| } | ||
| .sortable-header { | ||
| cursor: pointer; | ||
| user-select: none; | ||
| white-space: nowrap; | ||
| } | ||
| .sort-arrows { | ||
| margin-left: 4px; | ||
| } | ||
| .sort-arrow { | ||
| opacity: 0.3; | ||
| transition: opacity 0.2s; | ||
| font-size: 16px; | ||
| } | ||
| .sort-arrow:hover { | ||
| opacity: 0.8; | ||
| } | ||
| .sort-arrow.active { | ||
| opacity: 1; | ||
| } | ||
| </style> | ||
| <%- include('partials/head-tail') %> | ||
| </head> | ||
|
|
@@ -403,7 +427,15 @@ | |
| } | ||
| function renderItems(data) { | ||
| $('#actions-row').append(data.uniqueKeys.map(key => '<th scope="col">' + key + '</th>')) | ||
| $('#actions-row').append(data.uniqueKeys.map(key => | ||
| `<th scope="col" class="sortable-header" data-column="${key}" aria-sort="none"> | ||
| ${key} | ||
| <span class="sort-arrows"> | ||
| <span class="sort-arrow sort-arrow-up" data-direction="asc" aria-label="Sort ascending" title="Sort ascending">↑</span> | ||
| <span class="sort-arrow sort-arrow-down" data-direction="desc" aria-label="Sort descending" title="Sort descending">↓</span> | ||
|
Comment on lines
+434
to
+435
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aria-label is not necessary when there is |
||
| </span> | ||
| </th>` | ||
| )) | ||
| if (data.Items.length) { | ||
| $('#items-container').append(data.Items.map(item => { | ||
|
|
@@ -478,6 +510,62 @@ | |
| $('#table-placeholder').addClass('d-none') | ||
| updateFilterAutocomplete(data.uniqueKeys) | ||
| $('.sort-arrow').on('click', function(e) { | ||
| e.stopPropagation() | ||
| const column = $(this).closest('.sortable-header').data('column') | ||
| const direction = $(this).data('direction') | ||
| $('.sort-arrow').removeClass('active') | ||
| $(this).addClass('active') | ||
| sortTableByColumn(column, direction) | ||
| }) | ||
| } | ||
| function sortTableByColumn(column, direction) { | ||
| const tbody = $('#items-container') | ||
| const rows = tbody.find('tr').toArray() | ||
| rows.sort(function(a, b) { | ||
| const aVal = getCellValue(a, column) | ||
| const bVal = getCellValue(b, column) | ||
| let result = 0 | ||
| if (aVal < bVal) result = -1 | ||
| if (aVal > bVal) result = 1 | ||
| return direction === 'asc' ? result : -result | ||
| }) | ||
| tbody.append(rows) | ||
| } | ||
| function getCellValue(row, column) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like this relying on specific presentation details to get the items from the DOM (like extracting stuff from inside |
||
| const cells = $(row).find('td') | ||
| const actionsCell = cells[0] | ||
| const headers = $('#actions-row th') | ||
| let columnIndex = -1 | ||
| headers.each(function(index) { | ||
| if ($(this).data('column') === column) { | ||
| columnIndex = index | ||
| return false | ||
| } | ||
| }) | ||
| if (columnIndex === -1) return '' | ||
| const dataCell = cells[columnIndex] | ||
| if (!dataCell) return '' | ||
| const jsonFormatter = $(dataCell).find('.json-formatter-row-value')[0] || $(dataCell).find('.json-formatter-string')[0] || $(dataCell).find('.json-formatter-number')[0] | ||
| if (jsonFormatter) { | ||
| return $(jsonFormatter).text().trim() | ||
| } | ||
| return $(dataCell).text().trim() | ||
| } | ||
| window.addEventListener('load', () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this should not use a pointer cursor since clicking this doesn't actually do anything. It should be on the arrows instead.