mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-22 18:56:15 +02:00
edit to uswds js in order to fix sort when no column data
This commit is contained in:
parent
68ea6db752
commit
ddd12f0e2f
1 changed files with 25 additions and 3 deletions
|
@ -5506,6 +5506,8 @@ const SORT_BUTTON = `.${SORT_BUTTON_CLASS}`;
|
||||||
const SORTABLE_HEADER = `th[data-sortable]`;
|
const SORTABLE_HEADER = `th[data-sortable]`;
|
||||||
const ANNOUNCEMENT_REGION = `.${PREFIX}-table__announcement-region[aria-live="polite"]`;
|
const ANNOUNCEMENT_REGION = `.${PREFIX}-table__announcement-region[aria-live="polite"]`;
|
||||||
|
|
||||||
|
// ---- DOTGOV EDIT
|
||||||
|
|
||||||
/** Gets the data-sort-value attribute value, if provided — otherwise, gets
|
/** Gets the data-sort-value attribute value, if provided — otherwise, gets
|
||||||
* the innerText or textContent — of the child element (HTMLTableCellElement)
|
* the innerText or textContent — of the child element (HTMLTableCellElement)
|
||||||
* at the specified index of the given table row
|
* at the specified index of the given table row
|
||||||
|
@ -5514,7 +5516,19 @@ const ANNOUNCEMENT_REGION = `.${PREFIX}-table__announcement-region[aria-live="po
|
||||||
* @param {array<HTMLTableRowElement>} tr
|
* @param {array<HTMLTableRowElement>} tr
|
||||||
* @return {boolean}
|
* @return {boolean}
|
||||||
*/
|
*/
|
||||||
const getCellValue = (tr, index) => tr.children[index].getAttribute(SORT_OVERRIDE) || tr.children[index].innerText || tr.children[index].textContent;
|
const getCellValue = (tr, index) => {
|
||||||
|
if (tr.children[index])
|
||||||
|
return tr.children[index].getAttribute(SORT_OVERRIDE) || tr.children[index].innerText || tr.children[index].textContent;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// const getCellValue = (tr, index) => tr.children[index].getAttribute(SORT_OVERRIDE) || tr.children[index].innerText || tr.children[index].textContent;
|
||||||
|
// DOTGOV: added check for tr.children[index] to protect from absent cells
|
||||||
|
|
||||||
|
// ---- END DOTGOV EDIT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compares the values of two row array items at the given index, then sorts by the given direction
|
* Compares the values of two row array items at the given index, then sorts by the given direction
|
||||||
|
@ -5526,7 +5540,6 @@ const compareFunction = (index, isAscending) => (thisRow, nextRow) => {
|
||||||
// get values to compare from data attribute or cell content
|
// get values to compare from data attribute or cell content
|
||||||
const value1 = getCellValue(isAscending ? thisRow : nextRow, index);
|
const value1 = getCellValue(isAscending ? thisRow : nextRow, index);
|
||||||
const value2 = getCellValue(isAscending ? nextRow : thisRow, index);
|
const value2 = getCellValue(isAscending ? nextRow : thisRow, index);
|
||||||
|
|
||||||
// if neither value is empty, and if both values are already numbers, compare numerically
|
// if neither value is empty, and if both values are already numbers, compare numerically
|
||||||
if (value1 && value2 && !Number.isNaN(Number(value1)) && !Number.isNaN(Number(value2))) {
|
if (value1 && value2 && !Number.isNaN(Number(value1)) && !Number.isNaN(Number(value2))) {
|
||||||
return value1 - value2;
|
return value1 - value2;
|
||||||
|
@ -5601,7 +5614,16 @@ const sortRows = (header, isAscending) => {
|
||||||
const thisHeaderIndex = allHeaders.indexOf(header);
|
const thisHeaderIndex = allHeaders.indexOf(header);
|
||||||
allRows.sort(compareFunction(thisHeaderIndex, !isAscending)).forEach(tr => {
|
allRows.sort(compareFunction(thisHeaderIndex, !isAscending)).forEach(tr => {
|
||||||
[].slice.call(tr.children).forEach(td => td.removeAttribute("data-sort-active"));
|
[].slice.call(tr.children).forEach(td => td.removeAttribute("data-sort-active"));
|
||||||
tr.children[thisHeaderIndex].setAttribute("data-sort-active", true);
|
|
||||||
|
// ---- DOTGOV EDIT
|
||||||
|
|
||||||
|
// tr.children[thisHeaderIndex].setAttribute("data-sort-active", true);
|
||||||
|
if (tr.children[thisHeaderIndex])
|
||||||
|
tr.children[thisHeaderIndex].setAttribute("data-sort-active", true);
|
||||||
|
// DOTGOV added conditional to protect from tr.children[thisHeaderIndex] being absent
|
||||||
|
|
||||||
|
// ---- END DOTGOV EDIT
|
||||||
|
|
||||||
tbody.appendChild(tr);
|
tbody.appendChild(tr);
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue