Skip to content

Commit c8d4273

Browse files
authored
Add support for "disable-sort" class per column (#57)
* Add support for "disable-sort" class per column * Update documentation
1 parent c7d0853 commit c8d4273

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

README.md

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

public/docs/classes.html

+23-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ <h2>table-sort-js</h2>
8787
<td>table-arrows</td>
8888
<td>
8989
Adds up or down arrows to indicate whether column is sorted in
90-
aescending or descending order.
90+
ascending or descending order.
91+
</td>
92+
</tr>
93+
<tr>
94+
<td>remember-sort</td>
95+
<td>
96+
If clicking on different columns remembers sort of the original column.
9197
</td>
9298
</tr>
9399
</table>
@@ -99,7 +105,7 @@ <h2>table-sort-js</h2>
99105
>
100106
<thead>
101107
<tr>
102-
<th>&lt;th class=""></th>
108+
<th>&lt;th class=""&gt;</th>
103109
<th>Description</th>
104110
</tr>
105111
</thead>
@@ -113,7 +119,7 @@ <h2>table-sort-js</h2>
113119
<tr>
114120
<td>file-size</td>
115121
<td>
116-
Supports sorting files sizes from bytes to Tebibytes (B -> TiB).
122+
Supports sorting files sizes from bytes to Tebibytes (B -&gt; TiB).
117123
Can be Metric prefix (e.g 10KB), or Binary prefix(e.g 25KiB) and
118124
can contain a space or no space between number and unit type
119125
(e.g 25KB or 25 KB). It converts units into numbers (bytes) does
@@ -143,6 +149,12 @@ <h2>table-sort-js</h2>
143149
not supported by table-sort-js.
144150
</td>
145151
</tr>
152+
<tr>
153+
<td>disable-sort</td>
154+
<td>
155+
Disallow sorting the table by this specific column.
156+
</td>
157+
</tr>
146158
</table>
147159
<br>
148160
<h3>Example using tags:</h3>
@@ -156,23 +168,27 @@ <h3>Example using tags:</h3>
156168
&lt;th class="data-sort"&gt;data&lt;/th&gt;
157169
&lt;th class="file-size"&gt;file sizes&lt;/th&gt;
158170
&lt;th class="order-by-desc"&gt;birth date&lt;/th&gt;
171+
&lt;th class="disable-sort"&gt;unsortable&lt;/th&gt;
159172
&lt;/tr&gt;
160173
&lt;/thead&gt;
161174
&lt;tbody&gt;
162175
&lt;tr&gt;
163176
&lt;td data-sort="2"&gt;sort&lt;/td&gt;
164177
&lt;td&gt;10KiB&lt;/td&gt;
165178
&lt;td&gt;1706/1/17&lt;/td&gt;
179+
&lt;td&gt;1&lt;/td&gt;
166180
&lt;/tr&gt;
167181
&lt;tr&gt;
168182
&lt;td data-sort="3"&gt;js&lt;/td&gt;
169183
&lt;td&gt;1MB&lt;/td&gt;
170184
&lt;td&gt;1835/11/25&lt;/td&gt;
185+
&lt;td&gt;2&lt;/td&gt;
171186
&lt;/tr&gt;
172187
&lt;tr&gt;
173188
&lt;td data-sort="1"&gt;table&lt;/td&gt;
174189
&lt;td&gt;1TB&lt;/td&gt;
175190
&lt;td&gt;1835/11/30&lt;/td&gt;
191+
&lt;td&gt;3&lt;/td&gt;
176192
&lt;/tr&gt;
177193
&lt;/tbody&gt;
178194
&lt;/table&gt;
@@ -184,23 +200,27 @@ <h3>Example using tags:</h3>
184200
<th class="data-sort">data</th>
185201
<th class="file-size">file sizes</th>
186202
<th class="order-by-desc">birth date</th>
203+
<th class="disable-sort">unsortable</th>
187204
</tr>
188205
</thead>
189206
<tbody>
190207
<tr>
191208
<td data-sort="2">sort</td>
192209
<td>10KiB</td>
193210
<td>1706/1/17</td>
211+
<td>1</td>
194212
</tr>
195213
<tr>
196214
<td data-sort="3">js</td>
197215
<td>1 MB</td>
198216
<td>1835/11/25</td>
217+
<td>2</td>
199218
</tr>
200219
<tr>
201220
<td data-sort="1">table</td>
202221
<td>1TB</td>
203222
<td>1835/11/30</td>
223+
<td>3</td>
204224
</tr>
205225
</tbody>
206226
</table>

public/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,44 @@ <h1>Testing table sort js</h1>
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>
1718
</tr>
1819
</thead>
1920
<tr>
2021
<td>Franklin</td>
2122
<td>benjamin</td>
2223
<td>1706,1,17</td>
2324
<td></td>
25+
<td>C-level</td>
2426
</tr>
2527
<tr>
2628
<td>da Vinci</td>
2729
<td>Zarlo</td>
2830
<td>1452.4.15</td>
2931
<td></td>
32+
<td></td>
3033
</tr>
3134
<tr>
3235
<td>Statham</td>
3336
<td></td>
3437
<td>1967-7-26</td>
3538
<td></td>
39+
<td>HR</td>
3640
</tr>
3741
<tr>
3842
<td>Micheal</td>
3943
<td></td>
4044
<td>1958/8/21</td>
4145
<td>54</td>
46+
<td>Marketing</td>
4247
</tr>
4348

4449
<tr>
4550
<td>Ben</td>
4651
<td></td>
4752
<td>1994/9/23</td>
4853
<td>134</td>
54+
<td>Marketing</td>
4955
</tr>
5056
</table>
5157
</body>

public/table-sort.js

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

test/table.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,18 @@ test("visible-tr-sort: example sort only visible trs", () => {
295295
)
296296
).toStrictEqual(["row5", "row4", "row2"]);
297297
});
298+
299+
test("disable-sort: disable sorting on a column", () => {
300+
expect(
301+
createTestTable(
302+
[
303+
"row2",
304+
"row1",
305+
"row4",
306+
"row3",
307+
],
308+
"disable-sort"
309+
)
310+
).toStrictEqual(["row2", "row1", "row4", "row3"]);
311+
});
312+

0 commit comments

Comments
 (0)