What is tablesort?
The tablesort npm package is a lightweight, dependency-free JavaScript library that allows you to add sorting functionality to HTML tables. It is designed to be simple to use and easily customizable.
What are tablesort's main functionalities?
Basic Table Sorting
This feature allows you to add basic sorting functionality to an HTML table. By including the Tablesort library and initializing it with the table element, users can click on the table headers to sort the rows.
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/tablesort@5.2.1/dist/tablesort.min.js"></script>
</head>
<body>
<table id="myTable">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice</td>
<td>30</td>
</tr>
<tr>
<td>Bob</td>
<td>25</td>
</tr>
</tbody>
</table>
<script>
new Tablesort(document.getElementById('myTable'));
</script>
</body>
</html>
Custom Sort Functions
This feature allows you to define custom sort functions for specific columns. In this example, a custom sort function is used to sort dates in the format MM/DD/YYYY.
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/tablesort@5.2.1/dist/tablesort.min.js"></script>
</head>
<body>
<table id="myTable">
<thead>
<tr>
<th data-sort-method="custom">Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>01/02/2020</td>
</tr>
<tr>
<td>12/31/2019</td>
</tr>
</tbody>
</table>
<script>
Tablesort.extend('custom', function(item) {
return new Date(item.split('/').reverse().join('-')).getTime();
});
new Tablesort(document.getElementById('myTable'));
</script>
</body>
</html>
Sorting with Data Attributes
This feature allows you to use data attributes to specify the sort method for a column. In this example, the 'data-sort-method' attribute is used to indicate that the 'Score' column should be sorted numerically.
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/tablesort@5.2.1/dist/tablesort.min.js"></script>
</head>
<body>
<table id="myTable">
<thead>
<tr>
<th>Name</th>
<th data-sort-method="number">Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice</td>
<td data-sort="90">90</td>
</tr>
<tr>
<td>Bob</td>
<td data-sort="85">85</td>
</tr>
</tbody>
</table>
<script>
new Tablesort(document.getElementById('myTable'));
</script>
</body>
</html>
Other packages similar to tablesort
tablesorter
The tablesorter jQuery plugin provides similar functionality to tablesort, allowing you to add sorting capabilities to HTML tables. It offers more advanced features such as multi-column sorting, custom parsers, and widgets for additional functionalities. However, it requires jQuery as a dependency, which makes it heavier compared to the lightweight, dependency-free tablesort.
datatables
DataTables is a powerful jQuery plugin that provides extensive features for enhancing HTML tables, including sorting, filtering, pagination, and more. It is highly customizable and suitable for complex table manipulations. However, it is more complex and heavier than tablesort, making it more suitable for advanced use cases.
sorttable
SortTable is a standalone JavaScript library that adds sorting functionality to HTML tables. It is similar to tablesort in that it is lightweight and does not require any dependencies. However, tablesort offers more customization options and a more modern API compared to SortTable.
tablesort
A small & simple sorting component for tables written in JavaScript.
Quick start
Download the ZIP of this repository or install via command line:
npm install tablesort
# Or if you're using Yarn
yarn add tablesort
<script src='tablesort.min.js'></script>
<script src='tablesort.number.js'></script>
<script src='tablesort.date.js'></script>
<script>
new Tablesort(document.getElementById('table-id'));
</script>
See usage and demos for more
Browser Support
| | | | |
---|
8+ ✔ | 3.6+ ✔ | 10+ ✔ | 11.50+ ✔ | 5.1+ ✔ |
Node/Browserify
var tablesort = require('tablesort');
tablesort(el, options);
Default CSS
Add the styling from tablesort.css file to your CSS or roll with your own.
Extending Tablesort
If you require a sort operation that does not exist
in the sorts
directory, you can add your own.
Tablesort.extend('name', function(item) {
return /foo/.test(item);
}, function(a, b) {
return n;
});
If you've made an extend function that others would benefit from pull requests
are gladly accepted!
Contributing
Tablesort relies on Grunt as its build tool. Simply run
npm run build
to package code from any contributions you make to src/tablesort.js
before submitting pull requests.
Tests are run via:
npm install && npm test
Licence
MIT
Bugs?
Create an issue