tablesorter
tablesorter copied to clipboard
How to always sord DESC on first click
Hello, the plugin is awesome :+1: but I need a little tune. Please help :smile: Now, if you click the first time on TH - sorting is ASC, the second click - it's DESC. I need always first click to be a DESC on every new TH. And if I click again on the same TH - than do ASC. How can I do that?
There is a sortInitialOrder
option that isn't listed in the documentation. Set it to "desc"
or 1
.
$(function(){
$('.tablesorter').tablesorter({
sortInitialOrder: 'desc'
});
});
See this blog post for more undocumented options.
@Mottie Cool, thanks, that works! Can you help me with second part of the question? I would like to have this behavior: always sort desc on first click of another column. If you click second time same column, then do wise-versa, e.g. acs
There isn't an easy method in place with this version of tablesorter. I do have a fork of tablesorter with a sortRestart
option that will do exactly what you are needing - demo.
@Mottie wow, that's awesome! Thanks!
upd: mate, if I could rise ur karma, I would do it :heart: thanks a lot!
@Mottie If you haven't already you should open a PR for sortRestart
— exactly what I'm looking for too.
Actually, now that I think about it, you could just bind to the "sortEnd" event (demo)
$('table')
.on('sortEnd', function() {
var c = this.config,
th = c.headerList,
initOrder = /^(a|0)/.test(c.sortInitialOrder) ? 0 : 1;
if (c.sortRestart) {
$.each(th, function(indx) {
// don't restart the sort on already sorted columns
if (!/headerSort/.test(this.className)) {
this.count = initOrder;
}
});
}
})
.tablesorter({
sortInitialOrder: 'desc', // or 1
sortRestart: true
});