Skip to content

Commit

Permalink
Feature: added the class onload-sort for <th> table headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeWannacott committed May 7, 2023
1 parent 6fac2d4 commit dda333c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
6 changes: 3 additions & 3 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ <h1>Testing table sort js</h1>
<table class="table-sort table-arrows">
<thead>
<tr>
<th>Last Name</th>
<th class="onload-sort">Last Name</th>
<th>First Name</th>
<th class="order-by-desc">Birth Date</th>
<th>Employee ID</th>
<th class="disable-sort">Department</th>
<th>Department</th>
</tr>
</thead>
<tr>
<td>Franklin</td>
<td>benjamin</td>
<td>1706,1,17</td>
<td></td>
<td>C-level</td>
<td>k-level</td>
</tr>
<tr>
<td>da Vinci</td>
Expand Down
4 changes: 4 additions & 0 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
getTableData(tableProperties);
updateTable(tableProperties);
});
let isOnloadSort = th.classList.contains("onload-sort");
if (isOnloadSort) {
th.click();
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/order-by-desc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,10 @@ test("visible-tr-sort: example sort only visible trs", () => {
)
).toStrictEqual({ col0: ["row5", "row4", "row2"] });
});


test("onload-sort: testing that it sorts without a click from user", () => {
expect(
createTestTable({ col0: [5, 3, 4, 1, 2] }, { classTags: "order-by-desc onload-sort" })
).toStrictEqual({ col0: ["5", "4", "3", "2", "1"] });
});
4 changes: 3 additions & 1 deletion test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ function createTestTable(
}
} else {
for (let i = 0; i < numberOfTableColumns; i++) {
dom.window.document.querySelectorAll("table th")[i].click();
if (!thAttributes.classTags.includes("onload-sort")) {
dom.window.document.querySelectorAll("table th")[i].click();
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,14 @@ test("testing that multiple columns works", () => {
col1: ["carrie", "fisher", "doris"],
});
});

test("onload-sort: testing that it sorts without a click from user", () => {
expect(
createTestTable(
{ col0: ["echo", "alpha", "bravo", "charlie", "delta"] },
{
classTags: "onload-sort",
}
)
).toStrictEqual({ col0: ["alpha", "bravo", "charlie", "delta", "echo"] });
});

0 comments on commit dda333c

Please sign in to comment.