Skip to content

Commit 1acb377

Browse files
Update npm package to v1.8.0
1 parent 6bfac8f commit 1acb377

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

npm/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Refer to the documenation for examples how to use table-sort-js with [HTML.](htt
5252
| "order-by-desc" | Order by descending on first click. (default is aescending) |
5353
| "file-size" | Sort file sizes(B->TiB) uses the binary prefix. (e.g KiB) |
5454
| "data-sort" | Sort by [data attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes), e.g <td data-sort="42"> |
55+
| "disable-sort" | Disallow sorting the table by this specific column
5556

5657
#### Development:
5758

npm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "table-sort-js",
3-
"version": "1.6.9",
3+
"version": "1.8.0",
44
"description": "A JavaScript client-side HTML table sorting library with no dependencies required.",
55
"license": "MIT",
66
"repository": "LeeWannacott/table-sort-js",

npm/table-sort.js

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
5757

5858
const tableHead = sortableTable.querySelector("thead");
5959
const tableHeadHeaders = tableHead.querySelectorAll("th");
60-
tableHead.style.cursor = "pointer";
6160

6261
for (let [columnIndex, th] of tableHeadHeaders.entries()) {
63-
makeEachColumnSortable(th, columnIndex, tableBody, sortableTable);
62+
if (!th.classList.contains("disable-sort")) {
63+
th.style.cursor = "pointer";
64+
makeEachColumnSortable(th, columnIndex, tableBody, sortableTable);
65+
}
6466
}
6567
}
6668

@@ -86,6 +88,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
8688
}
8789

8890
function sortFileSize(tableRows, columnData) {
91+
// Handle filesize sorting (e.g KB, MB, GB, TB) - Turns data into KiB.
8992
const numberWithUnitType =
9093
/[.0-9]+(\s?B|\s?KB|\s?KiB|\s?MB|\s?MiB|\s?GB|\s?GiB|T\s?B|\s?TiB)/i;
9194
const unitType =
@@ -152,6 +155,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
152155
let columnIndexesClicked = [];
153156

154157
function rememberSort(timesClickedColumn, columnIndexesClicked) {
158+
// Check if user has clicked different column from the first column if
159+
// yes reset times clicked.
155160
columnIndexesClicked.push(columnIndex);
156161
if (timesClickedColumn === 1 && columnIndexesClicked.length > 1) {
157162
const lastColumnClicked =
@@ -165,12 +170,32 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
165170
}
166171
}
167172

168-
function getTableData(tableRows, columnData, isFileSize, isDataAttribute) {
173+
function getColSpanData(sortableTable, colSpanData, colSpanSum) {
174+
sortableTable.querySelectorAll("th").forEach((th, index) => {
175+
colSpanData[index] = th.colSpan;
176+
if (index === 0) colSpanSum[index] = th.colSpan;
177+
else colSpanSum[index] = colSpanSum[index - 1] + th.colSpan;
178+
});
179+
}
180+
181+
function getTableData(
182+
tableRows,
183+
columnData,
184+
isFileSize,
185+
isDataAttribute,
186+
colSpanData,
187+
colSpanSum
188+
) {
169189
for (let [i, tr] of tableRows.entries()) {
170190
// inner text for column we click on
171191
let tdTextContent = tr
172192
.querySelectorAll("td")
173-
.item(columnIndex).textContent;
193+
.item(
194+
colSpanData[columnIndex] === 1
195+
? colSpanSum[columnIndex] - 1
196+
: colSpanSum[columnIndex] - colSpanData[columnIndex]
197+
).textContent;
198+
174199
if (tdTextContent.length === 0) {
175200
tdTextContent = "";
176201
}
@@ -328,7 +353,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
328353

329354
th.addEventListener("click", function () {
330355
const columnData = [];
331-
// To make it work even if there is a tr with display: none; in the table, only the tr that is currently displayed is subject to sorting.
356+
const colSpanData = {};
357+
const colSpanSum = {};
358+
332359
const visibleTableRows = Array.prototype.filter.call(
333360
tableBody.querySelectorAll("tr"),
334361
(tr) => {
@@ -337,28 +364,32 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
337364
);
338365

339366
let isDataAttribute = th.classList.contains("data-sort");
340-
// Check if using data-sort attribute; if so sort by value of data-sort
341-
// attribute.
342367
if (isDataAttribute) {
343368
sortDataAttributes(visibleTableRows, columnData);
344369
}
345370

346371
let isFileSize = th.classList.contains("file-size");
347-
// Handle filesize sorting (e.g KB, MB, GB, TB) - Turns data into KiB.
348372
if (isFileSize) {
349373
sortFileSize(visibleTableRows, columnData);
350374
}
351375

352-
// Checking if user has clicked different column from the first column if
353-
// yes reset times clicked.
354376
let isRememberSort = sortableTable.classList.contains("remember-sort");
355377
if (!isRememberSort) {
356378
rememberSort(timesClickedColumn, columnIndexesClicked);
357379
}
358380

359381
timesClickedColumn += 1;
360382

361-
getTableData(visibleTableRows, columnData, isFileSize, isDataAttribute);
383+
getColSpanData(sortableTable, colSpanData, colSpanSum);
384+
// TODO: refactor function to take object.
385+
getTableData(
386+
visibleTableRows,
387+
columnData,
388+
isFileSize,
389+
isDataAttribute,
390+
colSpanData,
391+
colSpanSum
392+
);
362393
updateTable(visibleTableRows, columnData, isFileSize);
363394
});
364395
}

0 commit comments

Comments
 (0)