Search code examples
phpsortingblocktablesorter

How to block dashes in the bottom of the table


So, my question is in the title. I have a table with this data :

9
6
5
'-'
5
2.3
987
'-'
'-'
54.2
41
52
66
55

Note: I surround my dash with simple quote to have a best readability. and when I use the tablesorter, all works fine, but either my dashes are on the top and my number are sorted asc and conversely .

OR, I would like my dashes stay on the bottom of my table , whatsoever asc or desc sort..

It's my simple js :

 $("#table_conso_visu").tablesorter({
        widgets        : ['zebra', 'columns'],
        usNumberFormat : false,
        sortReset      : true,
        sortRestart    : true,
               })

Thank you for your help.


Solution

  • I found how to resolve my probleme. I create a filter which return me a minimum value . So my dashes stay on the bottom when my sort is asc et stay on top when my sort is desc. I think it's more logical that the dash is on the top when my sort is desc beacause it's like if i put 0 ;)

    So it's my filter :

    $.tablesorter.addParser({
                id: "dashSorter",
                format: function(s) {
                  return ($.trim(s) === '-') ? Number.MIN_VALUE : $.tablesorter.formatFloat(s.replace(/[,:]/g, ""));
                },
                type: "numeric"
              });
    

    i put ':' in my regex beacause i have time in format HH:mm:ss (you can remove it) and my script top sort my table :

    $("table[name=table_sorter]").tablesorter({
    
            // initialize zebra striping and filter widgets
            widgets :  ['zebra', 'filter'],
            headers : {
                1 : {sorter:'dashSorter'},
                2 : {sorter:'dashSorter'},
                3 : {sorter:'dashSorter'}
            },
            widgetOptions : {
                // css class applied to the table row containing the filters & the inputs within that row
                filter_cssFilter   : 'tablesorter-filter',
    
                // If there are child rows in the table (rows with class name from "cssChildRow" option)
                // and this option is true and a match is found anywhere in the child row, then it will make that row
                // visible; default is false
                filter_childRows   : false,
    
                // if true, filters are collapsed initially, but can be revealed by hovering over the grey bar immediately
                // below the header row. Additionally, tabbing through the document will open the filter row when an input gets focus
                filter_hideFilters : false,
    
                // Set this option to false to make the searches case sensitive
                filter_ignoreCase  : true,
    
                // jQuery selector string of an element used to reset the filters
                filter_reset : '.reset',
    
                // Delay in milliseconds before the filter widget starts searching; This option prevents searching for
                // every character while typing and should make searching large tables faster.
                filter_searchDelay : 300,
    
                // Set this option to true to use the filter to find text from the start of the column
                // So typing in "a" will find "albert" but not "frank", both have a's; default is false
                filter_startsWith  : false
            }
        });
    

    i hope that help people and see you soon ;)