1
votes

I am using jquery table sorting plugin.

The table columns are generated based on the sql query.

How can I sort (asc and desc) based on attribute value.

Note: i dont want like the following code, because the table column is generate mulitiple(some times 50 columns), so i need to sort all column based on same attribute name.

$.tablesorter.addParser({ 
    // set a unique id 
    id: 'myParser', 
    is: function(s) { 
      // return false so this parser is not auto detected 
      return false; 
    }, 
    format: function(s, table, cell, cellIndex) { 
      // get data attributes from $(cell).attr('data-something');
      // check specific column using cellIndex
      return $(cell).attr('data-something');
    }, 
    // set type, either numeric or text 
    type: 'text' 
  });
1

1 Answers

2
votes

If you are using my fork of tablesorter, you can set the textAttribute option to whatever data-attribute you are using in your table. By default, it is set to data-text:

<td data-text="1">First</td>

That cell will then be sorted using the value from data-text instead of "First".