tablesort
tablesort copied to clipboard
Allow sort types to be included with Browserify
Currently you can require('tablesort') to get the basic functionality, but there's no way to require the additional sorting functions into a Browserify project.
I was just looking to do the same thing.
I had a conversation with @tristen about this on Twitter - the short term solution would be to use .extend to add the sort manually. Here's an example of how to do that for the "number" sort https://gist.github.com/tristen/e79963856608bf54e046
Excellent; thanks!
Is this issue going to be resolved?
still no news on this?
One idea I could see taking is the monorepo approach using a tool like lerna. I think this would mean restructuring how the extend method works a little. I can jump on it when I have some spare time but would otherwise welcome a pull request.
A fancy refactor would be neat, but I wonder if a much simpler solution would be just as good.
@tristen how would you feel about simply adding oob support for all the sorts? In this day and age of import
/require
, this issue is going to be a common stumbling block. To my thinking the value of just adding them all to the base tool would easily outweigh the drag of increased size.
Love the idea of this solution, but doing the following and have it technically work but not actually do anything is pretty disappointing:
import TableSort from 'tablesort';
function setupTableSort(selector) {
document.querySelectorAll(selector).forEach(table => {
TableSort(table);
table.addEventListener('beforeSort', evt => {
console.log('Sorting table...');
});
table.addEventListener('afterSort', evt => {
console.log('Table sorted.');
});
});
}
document.addEventListener('DOMContentLoaded', evt => {
setupTableSort('.table.is-sortable');
});
Since there's already a module
check in the main script, why not expand that to include/require/export the sort modules?
I realize this isn't as trivial as I make it sound. The anonymous functions and global assumptions for "Tablesort" would have to be reworked.
I managed to find a solution for including this fantastic script into my browserify build process.
var Tablesort = require('tablesort');
this.tableSorter = new Tablesort(elem, options);
window.Tablesort = Tablesort;
require("../../../../node_modules/tablesort/src/sorts/tablesort.date.js");
require("../../../../node_modules/tablesort/src/sorts/tablesort.dotsep.js");
require("../../../../node_modules/tablesort/src/sorts/tablesort.number.js");
require("../../../../node_modules/tablesort/src/sorts/tablesort.filesize.js");
require("../../../../node_modules/tablesort/src/sorts/tablesort.monthname.js");
By setting the required tablesort module back to the window you can then require the sort scripts you need from the node_modules directory. Builds and runs without issue.