jQuery Tablesorter error
Asked Answered
Z

7

16

Just updated to the latest tablesorter and looks like its broken or something. Everytime i try to open my page, Firebug says:

table.config.parsers is undefined

And it just breaks all of my Javascript. If I revert the tablesorter version, it will work fine.

Javascript:

$("#List").tablesorter({ 
    widgets: ['zebra'],
    headers: { 
        4: { sorter: false }
    }
})

HTML:

<table id="List" class="tablesort ui-widget-content ui-corner-top">
    <thead>
      <tr class="ui-widget">
          <th>Pa&iacute;s</th>
          <th>ISO</th>
          <th>ISO3</th>
          <th>CODE</th>
          <th>&nbsp;</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
</table>
Zaidazailer answered 16/12, 2010 at 11:9 Comment(9)
go through the documentation of latest version...may it's possible that some function signature has been changed...or there is some change in configurationNebulize
just using basic stuff...they didnt changed the main tablesorter() function, im sure of it :PZaidazailer
can we see your JavaScript and html?Primogenial
I've copied your code into this (jsfiddle.net/Nalum/TDr4A) and got no errors. Do you have any other javascript on the page?Primogenial
As i said, everything works fine on previous version, it just breaks when i use the latest one so i doubt my js have anything with this.Zaidazailer
That may be the case but seeing any other JavaScript on the page would allow me and others to help you. What you have provided so far works so I can't really help you out any further with out more information.Primogenial
Can you provide a link to the page that is not working?Primogenial
pastebin.com/CRUQ0nix heres the JS. The page that doesnt work I cant provide a link since our development server aint public for internet =\ but pretty much all of my tablesortings works on previous version but not on latest. I'm using latest jquery/jqueryui if that matters.Zaidazailer
Did you ever figure this out? (I'm now having the same issue.)Anvers
S
14

Although the above answer doesn't mention it, I was able to replicate the error by first instantiating tablesorter() and then triggering a sort request.

This order of events would be necessary when appending or replacing existing table data with new data via AJAX or otherwise like so:

// populate our table body with rows
$("#myTable tbody").html(json.tbody);

// let the sorting plugin know that we made a update
$("#myTable").trigger("update");

// set sorting column and direction, this will sort on the first and third column
var sorting = [[2,1],[0,0]];

// sort
$("#myTable").trigger("sorton",[sorting]);

The combination of the "update" and the "sorton" event seems to be triggering the error. By the time the "sorton" event is handled the DOM hasn't been assigned the table.config.parsers - thus the error.

The fix is to wrap the "sorton" event handling in a 1 millisecond timeout.

Replace the existing "sorton" bind in jquery.tablesorter.js (line ~803) with the following:

}).bind("sorton", function (e, list) {
    var me = this;
    setTimeout(function () {
        $(this).trigger("sortStart");
        config.sortList = list;
        // update and store the sortlist
        var sortList = config.sortList;
        // update header count index
        updateHeaderSortCount(me, sortList);
        // set css for headers
        setHeadersCss(me, $headers, sortList, sortCSS);
        // sort the table and append it to the dom
        appendToTable(me, multisort(me, sortList, cache));
    }, 1);

tablesorter() is really a handy plugin. Thanks to Christian for releasing it.

Sheba answered 29/7, 2011 at 17:38 Comment(0)
A
13

Bit late but, it's because you have an empty/no <tr> element in the <tbody>, and it expects at least one <tr>.

Alive answered 26/3, 2011 at 21:46 Comment(1)
One way we managed to get around it is by starting off with a dummy <tr> regardless of whether the table has any data or not, and then hiding it via js on document load.According
V
1

I tried some of the answers above but they didn't help in every page we were using tablesorter. Primary reason I figured for error is that c=sortList[i][0] is undefined either because we have an empty TR or we don't same number of TD as that of TH.

I had 8 TH/TD in case I have table data and 8 TH & single TD in case I have nothing to show. I managed to check if I have no table data then don't call tablesorter to sort on specific columns which doesn't exist. This did the trick. Might help someone with similar scenario

if(tableData.length != 0){
  $("#myid").tablesorter( {sortList: [[2,0]]});
}
Vivanvivarium answered 23/3, 2015 at 6:27 Comment(1)
This helped me. Thanks! I just filtered them out, using .filter(function(){ return $(this).find("tbody > tr").length > 0; }).tablesorter(....). (I was using sortList, too. Not sure if that affects it.)Seizure
S
1

The problem seems to be that if the table is filled via JavaScript the tablesorter doesn't find the new content unless the browser has displayed the new content.

Firing the tablesorter inside a setTimeout() routine removed that error for me.

    function initPage() {
        fillMyTable();
        // Init table sorter, but give the browser a second to draw the new table
        setTimeout(function(){ $("#my_table").tablesorter(); }, 1000);
    }
Soddy answered 23/3, 2015 at 23:17 Comment(0)
E
0

Another answer just in case anyone ever runs into the same scenario I did. Apparently table sorter sometimes likes to have a matching number of empty <td> elements (the same as your header elements) inside the empty <tr>. My partial example is below

<thead>
    <tr>
        <th class="{sorter: 'text'}'' "><a href="javascript:;" title="Sort" class="arrow"><span>Network Name</span></a></th>
        <th class="{sorter: 'text'} "><a href="javascript:;" title="Sort" class="arrow"><span>Type</span></a></th>
        <th class="{sorter: false}"><a href="javascript:;" title="Sort" class="arrow"><span>Interconnections</span></a></th>
        <th class="{sorter: false}">&nbsp;</th>
    </tr>
</thead>

<tbody>
    <tr style="display:none"><td></td><td></td><td></td><td></td></tr>.........................
Eavesdrop answered 4/12, 2014 at 20:12 Comment(0)
K
0

Note that the tablesorter AJAX example only demonstrates a scenario in which new rows are appended to the existing ones. When the table is dynamically emptied and refilled with new rows, the above error surfaces.

Adding an empty row

<tr style="display:none"><td></td>...<td></td></tr>

with the same number of <td>s as the number of <th>s in the header removes the error but introduces another one: after the table is emptied and refilled with real rows, the new rows are appended to the old ones.

As an alternative to modifying the tablesorter source code, try the following invocation sequence:

$("#my-table").trigger("update");
setTimeout(function() { 
    /* e.g. sort by the second column in descending order */
    var sorting = [[1, 1]];
    $("#my-table").trigger("sorton", [sorting]);
}, 100);
Kamakura answered 30/11, 2015 at 23:38 Comment(0)
H
0

I encountered a case when I got that error message without specifically updating tablesorter, but just because the numbers of columns in the tfoot element was not matching the number of columns in the thead and tbody, so the tablesorter plugin go confused over that.

One may make that mistake on the same day as updating tablesorter. I thought I'd share this trick too in this subject, for future reference.

Hyoscyamus answered 1/5, 2018 at 2:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.