Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery tablesorter sorting empty table cells last

I have a table with a column with numbers spanning approximately -10 to 10 and some empty columns.

I'd like tablesorter to always put the empty columns at the bottom of the table. Even if I sort ascending or descending.

Like this:

-1
0
2
3
<empty cell>
<empty cell>

or like this:

3
2
0
-1
<empty cell>
<empty cell>

Note that my problem is the same as question 4792426 except that I want empty cells on the bottom both when sorting order is descending and ascending.

Deleting rows is not an option.

like image 851
Bård Avatar asked Feb 16 '12 14:02

Bård


1 Answers

I found a way to solve my problem.

The solution provided by ssell did not work. The table data is parsed only once, so you cannot use a global variable to control it. Nice try though! :-)

This is the code needed. As you see I overload the formatFloat and formatInt functions so that they return null instead of the default value: 0. The sorting function inside tablesorter does the rest magically for me.

        $(document).ready(function () {
        $.tablesorter.formatInt = function (s) {
            var i = parseInt(s);
            return (isNaN(i)) ? null : i;
        };
        $.tablesorter.formatFloat = function (s) {
            var i = parseFloat(s);
            return (isNaN(i)) ? null : i;
        };
        $("#table").tablesorter();
    });

As far as I know my solution is undocumented, but it works fine.

like image 156
Bård Avatar answered Oct 18 '22 12:10

Bård