vendredi 1 juillet 2016

Sorting and Formating with TableSorter


i'm trying to format some data of a table that is constantly updated by ajax.

I want that the float data become this

1.100.500,00 (currency)

after I sort it.

The problem is that tablesorter only sorts the float values correctly if they are in the first format. What I need to do is:

  1. When ajax loads the data, show it like (currency), as currency.
  2. When a table <th> is clicked to sort data, remove the (currency) currency format and sort the data correctly.
  3. After sorted correctly, re-format the data like (currency), as currency.

I already tried this:( that I've found that it's the solution to many questions here in SO)

$.tablesorter.addParser({ 
        // set a unique id 
        id: 'thousands',
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) {
            // format your data for normalization 
            return s.replace('.','').replace(/,/g,'');
        }, 
        // set type, either numeric or text 
        type: 'numeric' 
        }); 


        $("#table_1").tablesorter({
            headers: {
                2: {//zero-based column index
                    sorter:'thousands'
                }
            }
    });

but it's not working.

Any ideas? Thanks.


Aucun commentaire:

Enregistrer un commentaire