Sort tables fast with Sortable.js
github.hubspot.com
github.hubspot.com
Sorting is great functionality but if you really want adoption, I suggest you add the richness required by a variety of use cases. . Sorting . Filtering (regex, case etc) . Searching (column or full table) . Show/Hide columns
I don't mean to undermine to work put in here. It looks great and good luck.
Also, datatables has a large user base which means you can find many examples of it on stackoverflow or other such sites. Lists.js on the other hand has limited examples on their site and even fewer in the wild.
I'm not an everyday programmer and needed all the help I could get. Also, considering I'm not a developer, I found datatables quite easy to use.
Your comment prompted me to take another look at DataTables. So far I'm impressed. I think it will simplify my code and help clean up some long standing bugs, mainly by replacing some of my CSS visibility hacks with filter callbacks.
With most of our open-source projects at HubSpot (http://github.hubspot.com), we've tried to keep things modular and small, doing one thing as well as it possibly could be done.
Adding filtering and searching (fuzzy, regex, etc.) is definitely something we'd like to add. (https://github.com/HubSpot/sortable/issues/4) ;)
http://os.alfajango.com/dynatable if you're interested in checking it out.
EDIT: I don't mean to hijack this thread to just plug our project, just seemed relevant to the parent content. I actually really like the idea of modular single-purpose javascripts that are really well done. The issue becomes when each library has to duplicate functions from other libraries due to them being intended for individual use. For example, I imagine a lot of functions in listjs that duplicate some of the functions in jquery (I haven't actually looked through the individual functions in listjs yet, so this is just conjecture), which is great for the times when you're not using jquery, but other times, it's just a bunch of duplicated code.
A few things I really like 1. Applies to tables, lists etc 2. Nice progress indicator. This is missing in datatables and is a pain point in my current deployment. 3. stylized lists and charts. Very cool
what i didnt see 1. filtering. eg: find in column x where string not equal to blah 2. Searching specific column rather than entire table.
I apologize if this exists and I missed it. Overall, look great. Best wishes on this.
what i didnt see 1. filtering. eg: find in column x where string not equal to blah 2. Searching specific column rather than entire table.
See the "Year" and "Max Price" examples at the bottom of the "Querying" sub-section under "Operations", right before "Pagination". You would do that by adding your own query function:
$('#my-table')
.bind('dynatable:init', function(e, dynatable) {
dynatable.queries.functions['myQueryName'] = function(record, queryValue) {
return record.myAttribute != queryValue;
};
})
.dynatable();
Then, you'd invoke the query like this: dynatable.queries.add('myQueryName', 'hello');
dynatable.process();
And that will execute your query function which finds all records for which `myAttribute` is not equal to "hello". Of course, you'd need to bind the above query execution to some user input, like the `change` event of a text field or something like that, and instead of 'hello', you could pass in the value of the input.This is a common scenario, so dynatable provides a shortcut to this style of invocation of your custom query functions by passing dom elements to the `inputs` config option. So, you could replace both of the above with this:
var dynatable = $('#my-table')
.bind('dynatable:init', function(e, dynatable) {
dynatable.queries.functions['myQueryName'] = function(record, queryValue) {
return record.myAttribute != queryValue;
};
})
.dynatable({
inputs: {
queries: $('#my-input')
}
});
This will now cause the `myQueryName` function to be run on the dataset with the value of the `#my-input` dom element and process the results on the `blur` or `change` events of that dom element (the events that trigger the query processing can be changed via the `inputs.queryEvent` config option).I think this answers both of your questions. If there's a different sort of syntax you had in mind for providing an easier or more straight forward way of doing this, please let me know!
EDIT: I took a look at the source. There's an extra "data-" field to use as the value to sort with which is hard coded to "0" for the 2.44% row. This also answers my question of how it was sorting the textual date fields properly (each has the year in that data- field).
See: https://github.com/HubSpot/sortable/blob/e07209b89deac83950c...
How does yours differ from that one in terms of features/capabilities?
Is it a rewrite with more modern techniques?
Here is a short summary of some of the major changes:
– Tables with `data-sortable` attribute are automatically initialized.
– The sort arrows are made with CSS-border triangles instead of special characters.
– The sort arrows are always present but hidden for non-sorted columns so that columns don't resize when sorting.
– `sorttable_`-prefixed class names are now `data-` attributes, so they are valid HTML5.
– The library doesn't attempt to recognize date formats. Instead, it recommends you add `data-value="#{ timeInMillis }"` to your <td> containing a human-readable date string.
I'm sure there are more that I can't think of at the moment, but that's a decent list.
- Webdings: https://github.com/HubSpot/sortable/blob/37894eb51bda9f0e438...
- Using class names, rather than data attributes, for everything: https://github.com/HubSpot/sortable/blob/37894eb51bda9f0e438...
- Hardcoded date formats which would make localization tough: https://github.com/HubSpot/sortable/blob/37894eb51bda9f0e438...
- Sorting in JavaScript (not using Array.prototype.sort): https://github.com/HubSpot/sortable/blob/37894eb51bda9f0e438...
- Overriding event bindings in a destructive manner: https://github.com/HubSpot/sortable/blob/37894eb51bda9f0e438...
Not to mention stuff I just find objectionable:
- Useless comments: https://github.com/HubSpot/sortable/blob/37894eb51bda9f0e438...
- Mistabbing: https://github.com/HubSpot/sortable/blob/37894eb51bda9f0e438...
It also looks like this one is 1/3 the length.
To be fair though, the original was written seven years ago, and still works, so it's not really fair to criticize it by modern standards. Suffice it to say that it was time for something new.
But I think you're right that it would be great to have a pairable tool which adds filtering and possibly another which adds client-side pagination.