Skip to content

Commit db8da7c

Browse files
Support for custom table-arrows (#139)
* Progress on custom arrows pt1. * custom arrows working. * Make handle some emojis. * tidy up some stuff.
1 parent 327dfd1 commit db8da7c

File tree

4 files changed

+40
-39
lines changed

4 files changed

+40
-39
lines changed

Diff for: README.md

+2-2
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

Diff for: public/index.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
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+
<!-- -->
11+
<table class="table-sort table-arrows-👆🤙👇">
1112
<tr>
1213
<td>Last Name</td>
1314
<td>First Name</td>

Diff for: public/table-sort.js

+35-35
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
/*
1+
/*
22
table-sort-js
33
Author: Lee Wannacott
4-
Licence: MIT License Copyright (c) 2021 Lee Wannacott
5-
4+
Licence: MIT License Copyright (c) 2021 Lee Wannacott
5+
66
GitHub Repository: https://github.com/LeeWannacott/table-sort-js
77
npm package: https://www.npmjs.com/package/table-sort-js
88
Demo: https://leewannacott.github.io/Portfolio/#/GitHub
99
Install:
1010
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")
1313
Instructions:
1414
Add class="table-sort" to tables you'd like to make sortable
1515
Click on the table headers to sort them.
@@ -139,8 +139,10 @@ 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: Array.from(sortableTable.classList).filter((item) =>
144+
item.includes("table-arrows")
145+
),
144146
};
145147
for (
146148
let headerIndex = 0;
@@ -390,47 +392,36 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
390392
return sortAscending(b, a);
391393
}
392394

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, "");
397399
}
398400

399401
if (column.toBeSorted[0] === undefined) {
400402
return;
401403
}
402404

403-
function changeTableArrow(arrowDirection) {
405+
function changeArrowAndSort(arrowDirection, sortDirection) {
404406
if (table.hasClass.tableArrows) {
405-
clearArrows(arrow.up, arrow.down);
407+
clearArrows(arrow);
406408
th.insertAdjacentText("beforeend", arrowDirection);
407409
}
408-
}
409-
410-
function sortColumn(sortDirection) {
411410
column.toBeSorted.sort(sortDirection, {
412411
numeric: !isAlphaSort,
413412
ignorePunctuation: !isPunctSort,
414413
});
415414
}
416415

417416
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);
425420
} else if (timesClickedColumn === 2) {
426421
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);
434425
}
435426
return timesClickedColumn;
436427
}
@@ -524,12 +515,21 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
524515
columnIndexesClicked
525516
) {
526517
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);
533533
}
534534

535535
let timesClickedColumn = 0;

Diff for: src/test-table.js

+1-1
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 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)