tablesort icon indicating copy to clipboard operation
tablesort copied to clipboard

Allow sort types to be included with Browserify

Open ryangiglio opened this issue 9 years ago • 10 comments

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.

ryangiglio avatar Nov 19 '15 22:11 ryangiglio

I was just looking to do the same thing.

zygous avatar Nov 30 '15 16:11 zygous

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

ryangiglio avatar Nov 30 '15 16:11 ryangiglio

Excellent; thanks!

zygous avatar Nov 30 '15 16:11 zygous

Is this issue going to be resolved?

katiepeters avatar Apr 04 '16 14:04 katiepeters

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.

tristen avatar Feb 24 '17 13:02 tristen

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.

olets avatar Jul 31 '18 16:07 olets

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?

ActionScripted avatar Sep 21 '18 15:09 ActionScripted

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.

ActionScripted avatar Sep 21 '18 15:09 ActionScripted

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.

iamallyniam avatar Oct 19 '18 11:10 iamallyniam