Specify different sort orders after click with jQuery tablesorter -
i started using excellent jquery tablesorter script. might dumb question, cannot figure out how specify order in columns sorted after clicking. not referring whether columns sorted in ascending or descending order, rather sequence in columns sorted.
for example, if 1 clicks on column 2, might want sort column 2, column 4, column 5. however, if 1 clicks on column 3, might want sort column 3, column 5, column 4.
thanks in advance advice!
--mc
i can guess asking how sort multiple columns? or mean want other columns automatically sort based on first column clicked?
if want know how sort multiple columns then, default, hold down shift key click on columns sort in order. can change key changing sortmultisortkey
option (demo).
if want automatically sort multiple columns based on chosen column, recommend adding sort links/buttons you, otherwise taking choices away users. try (demo):
html
<button class="sort1">sort 1-3</button> <button class="sort2">sort 4-6</button>
script
$('table').tablesorter(); $('.sort1').click(function(){ $('table').trigger('sorton', [[ [1,0],[0,0],[2,0] ]]); }); $('.sort2').click(function(){ $('table').trigger('sorton', [[ [3,1],[4,0],[5,0] ]]); });
alternatively, can use sortappend
option adds sorting of column end of every sort. if want particular column sorted (e.g. producers) alphabetically after user sorts column, initialize tablesorter option:
$('table').tablesorter({ sortappend: [ [3,0] ] // 4th column (zero-based index) sort appended });
so if user sorts second column, fourth column sort added after. it's triggering sort this: [[ [1,0],[3,0] ]]
.
sadly, sortappend
option exists in original tablesorter, the code missing (ref). have fixed problem in fork of tablesorter - see this demo.
alternatively, if want sort dynamic, i.e. sortappend
change depending on column selected, you'll need bind sortbegin
event add sort (demo; ref):
$('table').bind('sortbegin', function(e, tbl) { var c = tbl.config, list = c.sortlist; // add third column sort if not initial sort, otherwise sort second column // (zero based index) list.push((list[0] && list[0][0] !== 2) ? [2, 0] : [1, 0]); });
i plan on making easier in future (see issue) when have time.
Comments
Post a Comment