Skip to content

Commit b2d0e1f

Browse files
committed
updated readme
1 parent 1bcf0cf commit b2d0e1f

File tree

1 file changed

+67
-22
lines changed

1 file changed

+67
-22
lines changed

README.md

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<h1>sortable</h1>
33
<h5>- a tiny, vanilla/plain JavaScript table sorter</h5>
44

5-
![GitHub Issues or Pull Requests](https://img.shields.io/github/issues/tofsjonas/sortable) ![NPM Version](https://img.shields.io/npm/v/sortable-tablesort) ![NPM Downloads](https://img.shields.io/npm/dw/sortable-tablesort) ![GitHub Repo stars](https://img.shields.io/github/stars/tofsjonas/sortable) [![jsdelivr](https://data.jsdelivr.com/v1/package/gh/tofsjonas/sortable/badge)](https://www.jsdelivr.com/package/gh/tofsjonas/sortable)
5+
[![GitHub Issues or Pull Requests](https://img.shields.io/github/issues/tofsjonas/sortable)](https://github.com/tofsjonas/sortable) [![NPM Version](https://img.shields.io/npm/v/sortable-tablesort)](https://www.npmjs.com/package/sortable-tablesort) [![NPM Downloads](https://img.shields.io/npm/dw/sortable-tablesort)](https://www.npmjs.com/package/sortable-tablesort) [![GitHub Repo stars](https://img.shields.io/github/stars/tofsjonas/sortable)](https://github.com/tofsjonas/sortable) [![jsdelivr](https://data.jsdelivr.com/v1/package/gh/tofsjonas/sortable/badge)](https://www.jsdelivr.com/package/gh/tofsjonas/sortable)
66

77
Makes any table with **class="sortable"**, er, sortable. The user can click on a table header and change the sorting of the table rows.
88

@@ -27,6 +27,7 @@ You can find a simple demo on <https://tofsjonas.github.io/sortable/>
2727
- [...using `td` instead of `th`](#using-td-instead-of-th)
2828
- [Indicators/arrows on the left side](#indicatorsarrows-on-the-left-side)
2929
- [NOTE ABOUT CSS/SCSS](#note-about-cssscss)
30+
- [Sticky headers](#sticky-headers)
3031
- [Sorting sizes, dates and such](#sorting-sizes-dates-and-such)
3132
- [Alternative sorting](#alternative-sorting)
3233
- [Colspans/Sort on specific column](#colspanssort-on-specific-column)
@@ -35,12 +36,13 @@ You can find a simple demo on <https://tofsjonas.github.io/sortable/>
3536
- [Tiebreaker / secondary sort](#tiebreaker--secondary-sort)
3637
- [Empty/null rows always last](#emptynull-rows-always-last)
3738
- [Accessibility](#accessibility)
39+
- [Sort Events](#sort-events)
3840
- [Sort on load](#sort-on-load)
3941
- [Thank you...](#thank-you)
4042

4143
## Factoids
4244

43-
- **1.02 KB** minified. (790 bytes gzipped)
45+
- **1.52KB** minified. (795 bytes gzipped)
4446

4547
- Works with **JavaScript generated tables**. (since we are using an eventListener)
4648

@@ -60,12 +62,6 @@ You can find a simple demo on <https://tofsjonas.github.io/sortable/>
6062

6163
There are three ways to use sortable, all of which have their pros and cons. [S Anand](https://github.com/sanand0) and [dkhgh](https://github.com/dkhgh) had some [interesting thoughts](https://github.com/tofsjonas/sortable/issues/28) about it.
6264

63-
1. Include a link to [jsDelivr](https://www.jsdelivr.com/package/gh/tofsjonas/sortable). (easiest)
64-
65-
2. Copy the file from [jsDelivr](https://www.jsdelivr.com/package/gh/tofsjonas/sortable) or [Github](https://github.com/tofsjonas/sortable) and put it in your assets folder. (in between)
66-
67-
3. Install the [npm package](https://www.npmjs.com/package/sortable-tablesort). (most reliable)
68-
6965
### 1. link to jsDelivr
7066

7167
```html
@@ -87,8 +83,8 @@ There are three ways to use sortable, all of which have their pros and cons. [S
8783
</tr>
8884
</tbody>
8985
</table>
90-
<link href="https://cdn.jsdelivr.net/gh/tofsjonas/sortable@latest/sortable.min.css" rel="stylesheet" />
91-
<script src="https://cdn.jsdelivr.net/gh/tofsjonas/sortable@latest/sortable.min.js"></script>
86+
<link href="https://cdn.jsdelivr.net/gh/tofsjonas/sortable@latest/dist/sortable.min.css" rel="stylesheet" />
87+
<script src="https://cdn.jsdelivr.net/gh/tofsjonas/sortable@latest/dist/sortable.min.js"></script>
9288
```
9389

9490
The `span` on line four is just there to prove that you can have elements inside `th`!
@@ -97,7 +93,7 @@ The `span` on line four is just there to prove that you can have elements inside
9793

9894
### 2. copy file to assets folder
9995

100-
Same as above, but link to your own files
96+
Same as above, but link to your own files from the `dist` directory
10197

10298
```html
10399
...
@@ -116,16 +112,16 @@ npm install sortable-tablesort
116112
# pnpm install sortable-tablesort
117113
```
118114

119-
Now you can
115+
Now you can:
120116

121117
#### a) use links in the html
122118

123-
Same as above, with links to files
119+
Same as above, with links to files from the `dist` directory
124120

125121
```html
126122
...
127-
<link href="./node_modules/sortable-tablesort/sortable.min.css" rel="stylesheet" />
128-
<script src="./node_modules/sortable-tablesort/sortable.min.js"></script>
123+
<link href="./node_modules/sortable-tablesort/dist/sortable.min.css" rel="stylesheet" />
124+
<script src="./node_modules/sortable-tablesort/dist/sortable.min.js"></script>
129125
...
130126
```
131127

@@ -135,8 +131,8 @@ or
135131

136132
```javascript
137133
// main.js
138-
import 'sortable-tablesort/sortable.min.css'
139-
import 'sortable-tablesort/sortable.min.js'
134+
import 'sortable-tablesort/dist/sortable.min.css'
135+
import 'sortable-tablesort/dist/sortable.min.js'
140136
```
141137

142138
## Non-sortable field
@@ -222,6 +218,28 @@ That said, if you're feeling lazy, here are two stylesheets you can use:
222218
<link href="https://cdn.jsdelivr.net/gh/tofsjonas/sortable@latest/sortable.min.css" rel="stylesheet" />
223219
```
224220

221+
### Sticky headers
222+
223+
I'm not sure if it's a good idea to have it in the main css, BUT if you are using the above `sortable(.min).css` file (not the -base files) and want sticky headers, you can simply add the class `sticky` to the table.
224+
225+
Blame [razorkyle](https://github.com/razorkyle), it was his idea! 😜
226+
227+
```html
228+
<table class="sortable sticky">
229+
...
230+
</table>
231+
```
232+
233+
If you are not using the css file, you can use the following css:
234+
235+
```css
236+
.sortable thead th {
237+
position: sticky;
238+
top: 0;
239+
z-index: 1;
240+
}
241+
```
242+
225243
## Sorting sizes, dates and such
226244

227245
Using the `data-sort` attribute in `tbody` > `td` you can have one visible value and one sortable value. This is useful in case you have for instance sizes like kb, Mb, GB, or really weird date formats. 😉
@@ -410,9 +428,9 @@ Sortable is not very accessible in its raw form. It does not support screen read
410428
<table class="sortable">
411429
...
412430
</table>
413-
<link href="sortable.min.css" rel="stylesheet" />
414-
<script src="sortable.min.js"></script>
415-
<script src="sortable.a11y.min.js"></script>
431+
<link href="dist/sortable.min.css" rel="stylesheet" />
432+
<script src="dist/sortable.min.js"></script>
433+
<script src="dist/sortable.a11y.min.js"></script>
416434
```
417435

418436
By including the file the global function `enhanceSortableAccessibility` will automatically run through all existing `.sortable` tables, but you can also run it manually, like so:
@@ -423,13 +441,38 @@ enhanceSortableAccessibility([table1, table2,...etc.])
423441

424442
The function adds an `aria-label` to each th, as well as `tabindex="0"` to each th in the thead of each table, making it possible to tab through the headers. It updates the `aria-label` depending on the direction.
425443

426-
if you want to import it instead this _should_ work: (I haven't tested it)
444+
If you want to import it instead:
427445

428446
```ts
429-
import { enhanceSortableAccessibility } from 'sortable-tablesort/enhanceSortableAccessibility'
447+
import { enhanceSortableAccessibility } from 'sortable-tablesort/dist/esm/enhanceSortableAccessibility'
430448
enhanceSortableAccessibility([table1, table2,...etc.])
431449
```
432450

451+
## Sort Events
452+
453+
The table element dispatches two custom events that bubble up the DOM tree:
454+
455+
- `sort-start`: Fired when sorting begins
456+
- `sort-end`: Fired when sorting is complete
457+
458+
You can listen for these events on any parent element, including the document itself:
459+
460+
```js
461+
// Listen for events from any sortable table
462+
document.addEventListener('sort-start', function (e) {
463+
console.log('Sorting started on:', e.target) // logs the table element
464+
})
465+
466+
document.addEventListener('sort-end', function (e) {
467+
console.log('Sorting complete on:', e.target) // logs the table element
468+
})
469+
470+
// Or listen to a specific table
471+
const table = document.querySelector('.sortable')
472+
table.addEventListener('sort-start', () => console.log('Sorting started'))
473+
table.addEventListener('sort-end', () => console.log('Sorting complete'))
474+
```
475+
433476
## Sort on load
434477

435478
If you wish to sort a table on load, I would recommend doing something like this:
@@ -494,3 +537,5 @@ Combine this with `<table class="sortable asc">` to reverse the sort order. Or d
494537
- ...[Jojo-IO](https://github.com/Jojo-IO) for the finding the "`parseFloat` messes up time values" bug!
495538

496539
- ...[Steve Wirt](https://github.com/swirtSJW) for lifting the Accessibility issue! 🦾️
540+
541+
- ...[GazHay](https://github.com/gazhay) for the [sort events](#sort-events) idea!

0 commit comments

Comments
 (0)