Tablesorter - JS error on empty table update/sort

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Tablesorter - JS error on empty table update/sort

nealc99
Possible bug - not sure.

I have a table that is dynamically inserted & deleted from.  On rare occasions the table may end up empty.

I use the sequence of:

    var sorting = [[0,0]];
        $("table").trigger("update");
        $("#restable").trigger("sorton",[sorting]);

after every insertion/deletion to resort the table.  If it is empty I will get an JavaScript error.  Not sure where it is from the "update" process or the "trigger".

Are there any suggestions to check to see if #restable has any TD's before triggering the update & sort - at least this would work around the situation.

Need something like:
    if($("#restable td")) then sort else don't
Reply | Threaded
Open this post in threaded view
|

Re: Tablesorter - JS error on empty table update/sort

Dave Sussman
I found this problem yesterday and it's because the table sorter assumes there are rows in the table body. I've solved my issue by modifying the table sorter script, editing buildParseCache() and buildCache() to check table.tBodies[0]:

            function buildParserCache(table, $headers) {

                if (table.config.debug) { var parsersDebug = ""; }

                // Check the body exists
                if (!table.tBodies[0])
                    return;
               
                var rows = table.tBodies[0].rows;

and

            function buildCache(table) {

                if (table.config.debug) { var cacheTime = new Date(); }

                // Check the body exists
                if (!table.tBodies[0])
                    return;

                var totalRows = (table.tBodies[0] && table.tBodies[0].rows.length) || 0,
                                        totalCells = (table.tBodies[0].rows[0] && table.tBodies[0].rows[0].cells.length) || 0,
                                        parsers = table.config.parsers,
                                        cache = { row: [], normalized: [] };



nealc99 wrote
Possible bug - not sure.

I have a table that is dynamically inserted & deleted from.  On rare occasions the table may end up empty.

I use the sequence of:

    var sorting = [[0,0]];
        $("table").trigger("update");
        $("#restable").trigger("sorton",[sorting]);

after every insertion/deletion to resort the table.  If it is empty I will get an JavaScript error.  Not sure where it is from the "update" process or the "trigger".

Are there any suggestions to check to see if #restable has any TD's before triggering the update & sort - at least this would work around the situation.

Need something like:
    if($("#restable td")) then sort else don't
Reply | Threaded
Open this post in threaded view
|

Re: Tablesorter - JS error on empty table update/sort

09058209051
In reply to this post by nealc99
Hello I'm having the same problem. . Tried you solution still doesn't work. . Any help ??


nealc99 wrote
Possible bug - not sure.

I have a table that is dynamically inserted & deleted from.  On rare occasions the table may end up empty.

I use the sequence of:

    var sorting = [[0,0]];
        $("table").trigger("update");
        $("#restable").trigger("sorton",[sorting]);

after every insertion/deletion to resort the table.  If it is empty I will get an JavaScript error.  Not sure where it is from the "update" process or the "trigger".

Are there any suggestions to check to see if #restable has any TD's before triggering the update & sort - at least this would work around the situation.

Need something like:
    if($("#restable td")) then sort else don't
Reply | Threaded
Open this post in threaded view
|

Re: Tablesorter - JS error on empty table update/sort

Paolo76
This post was updated on .
Hello had the same problem. I've solved my issue by modifying the tablesorter script, editing appendToTable() to check cache.normalized[0]:

function appendToTable(table,cache) {

    if(table.config.debug) {var appendTime = new Date()}
                               
        // Check the column exists
        if (!cache.normalized[0])
        return;
       
        var c = cache,
        r = c.row,
             n= c.normalized,
        totalRows = n.length,
        checkCell = (n[0].length-1),
        tableBody = $(table.tBodies[0]),
        rows = [];


09058209051 wrote
Hello I'm having the same problem. . Tried you solution still doesn't work. . Any help ??


nealc99 wrote
Possible bug - not sure.

I have a table that is dynamically inserted & deleted from.  On rare occasions the table may end up empty.

I use the sequence of:

    var sorting = [[0,0]];
        $("table").trigger("update");
        $("#restable").trigger("sorton",[sorting]);

after every insertion/deletion to resort the table.  If it is empty I will get an JavaScript error.  Not sure where it is from the "update" process or the "trigger".

Are there any suggestions to check to see if #restable has any TD's before triggering the update & sort - at least this would work around the situation.

Need something like:
    if($("#restable td")) then sort else don't