byCoder
byCoder

Reputation: 9184

Jquery tablesorter sort via two fields

I have such html table: here: http://jsfiddle.net/zrjaM/1/

And such js for sorting:

$(document).ready(function() { 
    $(".sortable") 
    .tablesorter({sortList: [[4,0]], widgets: ['zebra']});
}); 

But i need to sort via 0-th column to.... i write:

$(document).ready(function() { 
        $(".sortable") 
        .tablesorter({sortList: [[0,0],[4,0]], widgets: ['zebra']});
    }); 

but it works strange...

Also i need to do that, tr's with price value (4-th column) are on top, but now, when i sort only by price - all is ok, but when via two columns, i could have tr's with price both in end or middle.... How to send them to top, and sort first via 0-th, then via sorter 0-th sort via 4-t column.... How to do it?

Upvotes: 0

Views: 2406

Answers (2)

Mottie
Mottie

Reputation: 86453

@charlietfl's solution would work even without the code to add ZZZZZZZ to empty cells. But because that demo is using the original version of tablesorter, the first tbody would have to be removed.

But, because it looks like you are using my fork of tablesorter, which does allow sorting multiple tbodies, all you need to do is set the emptyTo option to none.

The emptyTo option is set to bottom by default. So all empty cells will always sort to the bottom. The reason the third column doesn't look like it's sorting at all is because all links in that column have the same text. Here is a demo.

Update: also remove the tablesorter-headerSortDown from the fourth column, it's still in the css but the plugin is using tablesorter-headerDesc now.

Upvotes: 2

charlietfl
charlietfl

Reputation: 171690

I figured out a possible solution.

First check rows for no price, if no price add a hidden span before item name in first column the text ZZZZZZZ in it. This forces all the ZZZZZZZ items to bottom and you get a double sort alphabetic on first row

$('tbody tr').each(function(){
    if( $.trim( $(this).find('td').eq(4).text())==''){
       $(this).find('td').eq(0).prepend('<span style="display:none">ZZZZZZZZ</span>')
       }
});

$(".sortable") .tablesorter({
      sortList: [[0,0],[4,0]], widgets: ['zebra']});
}); 

DEMO: http://jsfiddle.net/zrjaM/6/

Upvotes: 1

Related Questions