Skip to content

Commit b24dec1

Browse files
custom arrows working.
1 parent 2ffdb2f commit b24dec1

File tree

4 files changed

+28
-36
lines changed

4 files changed

+28
-36
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ Examples on using table-sort-js with frontend frameworks such as [React.js](http
5454
| <table> classes | Description |
5555
| --------------------- | ------------------------------------------------------------------------------------------------------------- |
5656
| "table-sort" | Make the table sortable! (Words, numbers, dates, file sizes)... |
57-
| "no-class-infer" | Turns off inference for adding sort classes automatically i.e (file-size-sort, runtime-sort, dates-dmy-sort). |
58-
| "table-arrows" | Display ascending or descending triangles. |
57+
| "table-arrows" | Display ascending or descending arrows. Supports custom arrows; example: "table-arrows-⇈⇆⇊" |
58+
| "no-class-infer" | Turns off inference for adding sort classes automatically e.g (file-size-sort, dates-dmy-sort), etc. |
5959
| "remember-sort" | If clicking on different columns remembers sort of the original column. |
6060
| "cells-sort" | sort cells (td) rather than table rows (tr); useful for keeping table rows with classes/attributes in place. |
6161

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<script type="text/javascript" src="./table-sort.js"></script>
88

99
<h1>Manual testing of table sort js</h1>
10-
<table class="table-sort table-arrows">
10+
<table class="table-sort table-arrows-⇈⇆⇊">
1111
<tr>
1212
<td>Last Name</td>
1313
<td>First Name</td>

public/table-sort.js

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
139139
table.hasClass = {
140140
noClassInfer: sortableTable.classList.contains("no-class-infer"),
141141
cellsSort: sortableTable.classList.contains("cells-sort"),
142-
tableArrows: sortableTable.classList.contains("table-arrows"),
143142
rememberSort: sortableTable.classList.contains("remember-sort"),
143+
// tableArrows: sortableTable.classList.contains("table-arrows"),
144+
tableArrows: Array.from(sortableTable.classList).filter((item) =>
145+
item.includes("table-arrows")
146+
),
144147
};
145148
for (
146149
let headerIndex = 0;
@@ -400,37 +403,26 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
400403
return;
401404
}
402405

403-
function changeTableArrow(arrowDirection) {
406+
function changeArrowAndSort(arrowDirection, sortDirection) {
404407
if (table.hasClass.tableArrows) {
405408
clearArrows(arrow);
406409
th.insertAdjacentText("beforeend", arrowDirection);
407410
}
408-
}
409-
410-
function sortColumn(sortDirection) {
411411
column.toBeSorted.sort(sortDirection, {
412412
numeric: !isAlphaSort,
413413
ignorePunctuation: !isPunctSort,
414414
});
415415
}
416416

417417
if (timesClickedColumn === 1) {
418-
if (desc) {
419-
changeTableArrow(arrow.down);
420-
sortColumn(sortDescending);
421-
} else {
422-
changeTableArrow(arrow.up);
423-
sortColumn(sortAscending);
424-
}
418+
desc
419+
? changeArrowAndSort(arrow.down, sortDescending)
420+
: changeArrowAndSort(arrow.up, sortAscending);
425421
} else if (timesClickedColumn === 2) {
426422
timesClickedColumn = 0;
427-
if (desc) {
428-
changeTableArrow(arrow.up);
429-
sortColumn(sortAscending);
430-
} else {
431-
changeTableArrow(arrow.down);
432-
sortColumn(sortDescending);
433-
}
423+
desc
424+
? changeArrowAndSort(arrow.up, sortAscending)
425+
: changeArrowAndSort(arrow.down, sortDescending);
434426
}
435427
return timesClickedColumn;
436428
}
@@ -524,20 +516,20 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
524516
columnIndexesClicked
525517
) {
526518
const desc = th.classList.contains("order-by-desc");
527-
const custom_arrows = th.classList.contains("custom-arrows");
528-
// for (let word in th.classList) {
529-
// console.log(word);
530-
// }
531-
let arrow = { neutral: " ↕", up: " ↑", down: " ↓" };
532519
let fillValue = "!X!Y!Z!";
533-
if (custom_arrows) {
534-
console.log(custom_arrows);
535-
console.log(custom_arrows.split("-"));
536-
[arrow.up, arrow.neutral, arrow.down] = custom_arrows.split();
537-
th.classList.add("table-arrows");
538-
}
539-
540-
if (table.hasClass.tableArrows) {
520+
let arrow = { up: " ↑", neutral: " ↕", down: " ↓" };
521+
if (table.hasClass.tableArrows[0]) {
522+
if (table.hasClass.tableArrows[0].split("-").length > 2) {
523+
var customArrow = table.hasClass.tableArrows[0].split("-")[2];
524+
if (customArrow.length === 3) {
525+
console.log(table.hasClass.tableArrows[0].split("-"));
526+
[arrow.up, arrow.neutral, arrow.down] = [
527+
" " + customArrow[0],
528+
" " + customArrow[1],
529+
" " + customArrow[2],
530+
];
531+
}
532+
}
541533
th.insertAdjacentText("beforeend", arrow.neutral);
542534
}
543535

src/test-table.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class App extends Component {
8484
Statistics on public repositories pulled from the GitHub API v3:
8585
</h6>
8686
<div>
87-
<table className="table table-sort custom-arrows table-bordered table-responsive table-hover">
87+
<table className="table table-sort table-arrows-jkl table-bordered table-responsive table-hover">
8888
<thead className="cw-light">
8989
<tr>
9090
<th>Repository Name</th>

0 commit comments

Comments
 (0)