Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting Table Columns with jQuery Table Sorter

I was wondering if there's a way to sort columns with Table sorter
So that I could arrange the columns themselves according to some ID or anything.
alt text

So here for instance, If I want to sort the table so that the Apple column
Would be first, how do I do that?

like image 722
Asaf Avatar asked Nov 05 '22 11:11

Asaf


1 Answers

Demo: http://jsfiddle.net/fKMqD/

Code:

var rows = $('tr');

rows.eq(0).find('td').sort(function(a, b){

    return $.text([a]) > $.text([b]) ? 1: -1;

}).each(function(newIndex){

    var originalIndex = $(this).index();

    rows.each(function(){
        var td = $(this).find('td');
        if (originalIndex !== newIndex)
            td.eq(originalIndex).insertAfter(td.eq(newIndex));
    });

});

No plugins necessary.

like image 91
James Avatar answered Nov 12 '22 19:11

James