Skip to content

Commit dda333c

Browse files
Feature: added the class onload-sort for <th> table headers.
1 parent 6fac2d4 commit dda333c

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

public/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ <h1>Testing table sort js</h1>
1010
<table class="table-sort table-arrows">
1111
<thead>
1212
<tr>
13-
<th>Last Name</th>
13+
<th class="onload-sort">Last Name</th>
1414
<th>First Name</th>
1515
<th class="order-by-desc">Birth Date</th>
1616
<th>Employee ID</th>
17-
<th class="disable-sort">Department</th>
17+
<th>Department</th>
1818
</tr>
1919
</thead>
2020
<tr>
2121
<td>Franklin</td>
2222
<td>benjamin</td>
2323
<td>1706,1,17</td>
2424
<td></td>
25-
<td>C-level</td>
25+
<td>k-level</td>
2626
</tr>
2727
<tr>
2828
<td>da Vinci</td>

public/table-sort.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
326326
getTableData(tableProperties);
327327
updateTable(tableProperties);
328328
});
329+
let isOnloadSort = th.classList.contains("onload-sort");
330+
if (isOnloadSort) {
331+
th.click();
332+
}
329333
}
330334
}
331335

test/order-by-desc.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,10 @@ test("visible-tr-sort: example sort only visible trs", () => {
175175
)
176176
).toStrictEqual({ col0: ["row5", "row4", "row2"] });
177177
});
178+
179+
180+
test("onload-sort: testing that it sorts without a click from user", () => {
181+
expect(
182+
createTestTable({ col0: [5, 3, 4, 1, 2] }, { classTags: "order-by-desc onload-sort" })
183+
).toStrictEqual({ col0: ["5", "4", "3", "2", "1"] });
184+
});

test/table.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ function createTestTable(
7878
}
7979
} else {
8080
for (let i = 0; i < numberOfTableColumns; i++) {
81-
dom.window.document.querySelectorAll("table th")[i].click();
81+
if (!thAttributes.classTags.includes("onload-sort")) {
82+
dom.window.document.querySelectorAll("table th")[i].click();
83+
}
8284
}
8385
}
8486

test/table.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,14 @@ test("testing that multiple columns works", () => {
219219
col1: ["carrie", "fisher", "doris"],
220220
});
221221
});
222+
223+
test("onload-sort: testing that it sorts without a click from user", () => {
224+
expect(
225+
createTestTable(
226+
{ col0: ["echo", "alpha", "bravo", "charlie", "delta"] },
227+
{
228+
classTags: "onload-sort",
229+
}
230+
)
231+
).toStrictEqual({ col0: ["alpha", "bravo", "charlie", "delta", "echo"] });
232+
});

0 commit comments

Comments
 (0)