5

The plugin is working fine when I only have 1 table on a page.

But with two, I get an error:

Uncaught TypeError: Cannot set property 'count' of undefined

That's because of the sortList option set below. I have it set to sort on the 4th column, and the aux_table, displayed first, only has 3 columns. But it works and the main_table doesn't. How do I get them both to work, or just the second, more important main_table?

Both tables are class tablesorter and they have different ids (main_table and aux_table).

The first table on the page works, and the second doesn't. Here is the JS from my <head> tag:

$(document).ready(function() { 
    // call the tablesorter plugin 
    $("table").tablesorter({ 
        // sort on the 4th column (index 3) column, DESC (1). ASC is (0). 
        sortList: [[3,1]] 
    });    
});

3 Answers 3

7

You need to change your code to instantiate the table sorter on each individual table, so that you can specify separate settings for each.

$("#table1").tablesorter({ 
   sortList: [[3,1]] 
}); 

$("#table2").tablesorter();
Sign up to request clarification or add additional context in comments.

4 Comments

I knew it would be simple. Thanks!
This is correct if you know the tables ahead of time. If one script is handling dynamically generated tables on several pages, I found it is helpful to surround each table in some kind of container and then use $(".table-sorting-instance").each(function () and $(this).find(".tablesorter-table"); to find the tables. Just sharing!
This does not work. I have 2 tables with separate ids and I did instantiate tablesorter separately as you show above but only the first table sorts.
@KimWilson You would be best to start a new question about that. It's possible this no longer applies to newer versions of tablesorter
0

How about selecting on class?

$(".tablesorter").tablesorter(); 

if need to sort on multiple tables..

1 Comment

As far as I know (dealing with that right now), using a class, it sorts only the last table in the page.
0

Yes you can add multiple tables in one page. You can add each table in wrap as shown below and it is possible to add the pagination and sort functionality separately.

 $(document).ready(function() {
   $('.pearl-container').each(function(i) {
     $(this).children(".myTable")
       .tablesorter({
         widthFixed: true,
         widgets: ['zebra']
       })
       .tablesorterPager({
         container: $(this).children(".pager"),
         size: 5
       });
   });

 });

1 Comment

Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.