Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can I get the current sortOrder in the sortEnd event? #1208

Closed
finira opened this issue May 5, 2016 · 6 comments
Closed

how can I get the current sortOrder in the sortEnd event? #1208

finira opened this issue May 5, 2016 · 6 comments

Comments

@finira
Copy link

finira commented May 5, 2016

I want to get the current sortOrder in every sortend event.(desc or asc)
so how can I get it ?

@Mottie
Copy link
Owner

Mottie commented May 5, 2016

Hi @finira!

The sortList option is updated after every sort. It will contain an array of the current sort (demo):

$(function () {
  $('table')
    .on('sortEnd', function(){
      $('.sortlist').text(this.config.sortList);
    })
    .tablesorter({
      theme: 'blue'
    });
});

I'm not sure what you want to do with the information, but please be aware that the sortList contains an array of arrays. The inside array contains the zero-based index of the column and the sort direction (0 for ascending and 1 for descending).

console.log(this.config.sortList);
// [[0,1],[2,0]]
// [0,1] = column 0 (first column), descending sort
// [2,0] = column 2 (third column), ascending sort

Alternatively, the aria-label of the header, or even the header class name (tablesorter-headerAsc or tablesorter-headerDesc), can be used to provide additional information (demo):

$(function() {
  $('table')
    .on('sortEnd', function() {
      var c = this.config,
        list = '';
      $.each(c.sortList, function(i, v) {
        list += '<li>' + c.$headerIndexed[v[0]].attr('aria-label') + '</li>';
      });
      $('.sortlist').html('<ul>' + list + '</ul>');
    })
    .tablesorter({
      theme: 'blue'
    });
});

@finira
Copy link
Author

finira commented May 6, 2016

Hi @Mottie !
thanks for your answering.
I've found the sortList before ,and I added some function in your code .

At line 203 , I added a variable currentSortOrder to remenber the current order.
At 1607 ,I add a function putCurrentOrder to get the current order to currentSortOrder from sortList

putCurrentOrder:function(sortList) { if(sortList&&sortList.length){ ts.currentSortOrder = {'0':'asc','1':'desc','2':'unsort'}[sortList[ 0 ][ 1 ]]||''; } },

and a function getCurrentOrder which return the currentSortOrder .
getCurrentOrder:function() { return ts.currentSortOrder; },

and in function multisort ,I invoke the putCurrentOrder to set current order from sortList.
var tbodyIndex, sortTime, colMax, rows, table = c.table, dir = 0, textSorter = c.textSorter || '', sortList = c.sortList, sortLen = sortList.length, len = c.$tbodies.length; ts.putCurrentOrder(sortList);

(only multisort is used in my program so I invoke the method in this function)

and I can get the current order like this:
corder = $.tablesorter.getCurrentOrder();

That's my temp plan,and I complete it ,thank you ~

@Mottie
Copy link
Owner

Mottie commented May 6, 2016

I wouldn't recommend changing the plugin code. The reason is that when you update the tablesorter core code, all that extra code will need to be added back.

The best solution would be best to loop through each sortList column/sort direction pair inside a sortEnd callback function as I've already shared.

@finira
Copy link
Author

finira commented May 7, 2016

Thank you .
So , will you consider adding a function for getting current order in the next version?

@Mottie
Copy link
Owner

Mottie commented May 7, 2016

Hmmm, I might consider adding something like the following code... (demo):

$.tablesorter.currentSortLanguage = {
  0: 'asc',
  1: 'desc',
  2: 'unsort'
};

$.tablesorter.currentSort = function(table) {
  // allow passing DOM & jQuery object
  table = $(table)[0];
  var indx,
    c = table.config,
    lang = $.tablesorter.currentSortLanguage,
    unsort = lang[2],
    // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
    // order = new Array(c.columns).fill(unsort),
    // the above ES6 will not work in all browsers, so
    // we're stuck with this messy code to fill the array:
    order = Array
      .apply(null, Array(c.columns))
      .map(String.prototype.valueOf, unsort),
    sortList = c.sortList,
    len = sortList.length;
  for (indx = 0; indx < len; indx++) {
    order[sortList[indx][0]] = lang[sortList[indx][1]];
  }
  return order;
};

It can be used as follows:

$(function() {
  $('table')
    .on('sortEnd', function() {
      console.log($.tablesorter.currentSort(this));
    })
    .tablesorter({
      theme: 'blue',
      widgets: ['zebra']
    });
});

Or, you can call $.tablesorter.currentOrder(table) at any time after tablesorter initializes to get an array of sorts.

This code might just be more bloat added to the core, so maybe I'll add it into a separate tablesorter.utilities.js file... let me think about it.

@finira
Copy link
Author

finira commented May 7, 2016

Woo!!
that's cool .
waiting for the new version now.

@Mottie Mottie closed this as completed in 7109295 Jul 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants