|
1 |
| -/* |
| 1 | +/* |
2 | 2 | table-sort-js
|
3 | 3 | Author: Lee Wannacott
|
4 |
| -Licence: MIT License Copyright (c) 2021 Lee Wannacott |
5 |
| - |
| 4 | +Licence: MIT License Copyright (c) 2021 Lee Wannacott |
| 5 | +
|
6 | 6 | GitHub Repository: https://github.com/LeeWannacott/table-sort-js
|
7 | 7 | npm package: https://www.npmjs.com/package/table-sort-js
|
8 | 8 | Demo: https://leewannacott.github.io/Portfolio/#/GitHub
|
9 | 9 | Install:
|
10 | 10 | Frontend: <script src="https://leewannacott.github.io/table-sort-js/table-sort.js"></script> or
|
11 |
| -Download this file and add <script src="table-sort.js"></script> to your HTML |
12 |
| -Backend: npm install table-sort-js and use require("../node_modules/table-sort-js/table-sort.js") |
| 11 | +Download this file and add <script src="table-sort.js"></script> to your HTML |
| 12 | +Backend: npm install table-sort-js and use require("../node_modules/table-sort-js/table-sort.js") |
13 | 13 | Instructions:
|
14 | 14 | Add class="table-sort" to tables you'd like to make sortable
|
15 | 15 | Click on the table headers to sort them.
|
@@ -139,8 +139,10 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
|
139 | 139 | table.hasClass = {
|
140 | 140 | noClassInfer: sortableTable.classList.contains("no-class-infer"),
|
141 | 141 | cellsSort: sortableTable.classList.contains("cells-sort"),
|
142 |
| - tableArrows: sortableTable.classList.contains("table-arrows"), |
143 | 142 | rememberSort: sortableTable.classList.contains("remember-sort"),
|
| 143 | + tableArrows: Array.from(sortableTable.classList).filter((item) => |
| 144 | + item.includes("table-arrows") |
| 145 | + ), |
144 | 146 | };
|
145 | 147 | for (
|
146 | 148 | let headerIndex = 0;
|
@@ -390,47 +392,36 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
|
390 | 392 | return sortAscending(b, a);
|
391 | 393 | }
|
392 | 394 |
|
393 |
| - function clearArrows(arrowUp, arrowDown, initialArrow = "↕") { |
394 |
| - th.innerHTML = th.innerHTML.replace(initialArrow, ""); |
395 |
| - th.innerHTML = th.innerHTML.replace(arrowUp, ""); |
396 |
| - th.innerHTML = th.innerHTML.replace(arrowDown, ""); |
| 395 | + function clearArrows(arrow) { |
| 396 | + th.innerHTML = th.innerHTML.replace(arrow.neutral, ""); |
| 397 | + th.innerHTML = th.innerHTML.replace(arrow.up, ""); |
| 398 | + th.innerHTML = th.innerHTML.replace(arrow.down, ""); |
397 | 399 | }
|
398 | 400 |
|
399 | 401 | if (column.toBeSorted[0] === undefined) {
|
400 | 402 | return;
|
401 | 403 | }
|
402 | 404 |
|
403 |
| - function changeTableArrow(arrowDirection) { |
| 405 | + function changeArrowAndSort(arrowDirection, sortDirection) { |
404 | 406 | if (table.hasClass.tableArrows) {
|
405 |
| - clearArrows(arrow.up, arrow.down); |
| 407 | + clearArrows(arrow); |
406 | 408 | th.insertAdjacentText("beforeend", arrowDirection);
|
407 | 409 | }
|
408 |
| - } |
409 |
| - |
410 |
| - function sortColumn(sortDirection) { |
411 | 410 | column.toBeSorted.sort(sortDirection, {
|
412 | 411 | numeric: !isAlphaSort,
|
413 | 412 | ignorePunctuation: !isPunctSort,
|
414 | 413 | });
|
415 | 414 | }
|
416 | 415 |
|
417 | 416 | if (timesClickedColumn === 1) {
|
418 |
| - if (desc) { |
419 |
| - changeTableArrow(arrow.down); |
420 |
| - sortColumn(sortDescending); |
421 |
| - } else { |
422 |
| - changeTableArrow(arrow.up); |
423 |
| - sortColumn(sortAscending); |
424 |
| - } |
| 417 | + desc |
| 418 | + ? changeArrowAndSort(arrow.down, sortDescending) |
| 419 | + : changeArrowAndSort(arrow.up, sortAscending); |
425 | 420 | } else if (timesClickedColumn === 2) {
|
426 | 421 | timesClickedColumn = 0;
|
427 |
| - if (desc) { |
428 |
| - changeTableArrow(arrow.up); |
429 |
| - sortColumn(sortAscending); |
430 |
| - } else { |
431 |
| - changeTableArrow(arrow.down); |
432 |
| - sortColumn(sortDescending); |
433 |
| - } |
| 422 | + desc |
| 423 | + ? changeArrowAndSort(arrow.up, sortAscending) |
| 424 | + : changeArrowAndSort(arrow.down, sortDescending); |
434 | 425 | }
|
435 | 426 | return timesClickedColumn;
|
436 | 427 | }
|
@@ -524,12 +515,21 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
|
524 | 515 | columnIndexesClicked
|
525 | 516 | ) {
|
526 | 517 | const desc = th.classList.contains("order-by-desc");
|
527 |
| - const initialArrow = " ↕"; |
528 |
| - const arrow = { up: " ↑", down: " ↓" }; |
529 |
| - const fillValue = "!X!Y!Z!"; |
530 |
| - |
531 |
| - if (table.hasClass.tableArrows) { |
532 |
| - th.insertAdjacentText("beforeend", initialArrow); |
| 518 | + let fillValue = "!X!Y!Z!"; |
| 519 | + let arrow = { up: " ↑", neutral: " ↕", down: " ↓" }; |
| 520 | + if (table.hasClass.tableArrows[0]) { |
| 521 | + if (table.hasClass.tableArrows[0].split("-").length > 2) { |
| 522 | + // Array.from to support utf-8 strings e.g emojis |
| 523 | + let customArrow = Array.from( |
| 524 | + table.hasClass.tableArrows[0].split("-")[2] |
| 525 | + ); |
| 526 | + customArrow = customArrow.map((i) => " " + i); |
| 527 | + console.log(customArrow); |
| 528 | + if (customArrow.length === 3) { |
| 529 | + [arrow.up, arrow.neutral, arrow.down] = [...customArrow]; |
| 530 | + } |
| 531 | + } |
| 532 | + th.insertAdjacentText("beforeend", arrow.neutral); |
533 | 533 | }
|
534 | 534 |
|
535 | 535 | let timesClickedColumn = 0;
|
|
0 commit comments