Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
845 views
in Technique[技术] by (71.8m points)

jquery - how to get current sort order from tablesorter plugin?

I'm just starting to use Christian Bach's excellent TableSorter plugin, and I need to get a column's current sort direction. I have several columns:

  • ID
  • Name
  • Category

ID and Name are set to non-sortable using

headers:    { 0: {sorter: false}, 1: {sorter: false} }

I'm adding a click handler on Name so that it fires the sort event on the Category column. Using the example "Sort table using a link outside the table", I'm able to get the Name header to fire the Category sort -- but it's hard-coded to sort in one direction.

How can I get it to look at the current direction the Category column is currently sorted, and sort in the opposite direction? (I can handle flipping the values; since the sort order is 0 or 1, I can XOR the value to get the opposite, like var sort; sort ^= sort; -- my question is how to get the current value.

Here's the code that currently sets the click handler on the Name column:

$("#nameCol").click(function() {
    var sorting = [[2, 0]];     /* sort 3rd col (Category) descending */
    $("#SearchResults").trigger("sorton", [sorting] );  /* SearchResults is the ID of the sortable table */
    return false;               /* cancel default link action on a#nameCol */
});

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the built in sortEnd event to get the sortOrder as explained here: https://stackoverflow.com/a/4150187/363155

$('#yourtableId').on('sortEnd', function(event) {
    // Prints the current sort order to the console
    console.log(event.target.config.sortList);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...