0

I'm using jquery tablesorter to sort my table. I've tried to google my question, didn't find what I was looking for.

I've got a table which content changes dynamically so I want to change the way items are sorted.

One case I've got table populated with text which can be sorter with default .tablesorter() and I've got another case where digits are in table so I need to invoke tablesorter like this :

$('table').tablesorter({
        headers : { 
                0 : {sorter:'digit'},
                1 : {sorter:'digit'},
                2 : {sorter:'digit'},
                3 : {sorter:'digit'}
        }
    });

I have a method that does reload to table switching between numbers/text in table content, how can I change the way table is sorted.

In the example (pseudocode):

function reloadTableData
if table contains numbers user this tablesorter (I have a way to know if table contains numbers) 
$('table').tablesorter({
            headers : { 
                    0 : {sorter:'digit'},
                    1 : {sorter:'digit'},
                    2 : {sorter:'digit'},
                    3 : {sorter:'digit'}
            }
        });
if table contains text use ordinary table sorter 
$('table').tablesorter();
end

I can reload table data n times with either text/numbers.

I've tried the following :

function initTablesorter(n) {

    switch(n)
    {
    case "number":
      digitTableSorter();
      break;
    case "text":
     defaultTableSorter();
      break;
    default:

    }
}

function digitTableSorter(){
$('table').tablesorter({
        headers : { 
                0 : {sorter:'digit'},
                1 : {sorter:'digit'},
                2 : {sorter:'digit'},
                3 : {sorter:'digit'}
        }
    });
}

function defaultTableSorter(){
$('table').tablesorter();
}

Needless to say it's not working, I hope someone did something like this before, I'm stuck for some time now.

Gandalf StormCrow
  • 25,788
  • 70
  • 174
  • 263
  • So is it not working beacuse you are reinitialising tablesorter on a table? You may try to unbind tablesorter before rebinding it. Have a look at http://stackoverflow.com/questions/8171530/remove-jquery-tablesorter-from-table – iappwebdev Dec 18 '12 at 13:11
  • @Simon hey thanks it worked can you reply and I'd accept the answer – Gandalf StormCrow Dec 18 '12 at 15:58

2 Answers2

1

So is it not working because you are reinitialising tablesorter on a table? You may try to unbind tablesorter before rebinding it.

$('table')
 .unbind('appendCache applyWidgetId applyWidgets sorton update updateCell')
 .removeClass('tablesorter')
 .find('thead th')
 .unbind('click mousedown')
 .removeClass('header headerSortDown headerSortUp');

Have a look at Remove jQuery tablesorter from table.

Community
  • 1
  • 1
iappwebdev
  • 5,880
  • 1
  • 30
  • 47
0

I think you are looking for this - You will need MetatData plugin to get the th classes to like "{sorter: 'digit'}"

http://tablesorter.com/docs/example-trigger-sort.html

 $("#trigger-link").click(function() { 
    // set sorting column and direction, this will sort on the first and third column the column index starts at zero 
    var sorting = [[0,0],[2,0]]; 
    // sort on the first column 
    $("table").trigger("sorton",[sorting]); 
    // return false to stop default link action 
    return false; 
}); 
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    If you go to the [Download](http://tablesorter.com/docs/index.html#Download) section of the documentation, you'll see this message: jQuery Metadata 2.1 (3,7kb Required for setting inline options). You'll see that the "inline options" link there refers to the metadata examples. – Mottie Dec 18 '12 at 17:13