Jquery tablesorter with account number format

I am using jquery tablesorter and I am trying to sort a column with values ​​similar to this:

$ 100,000 $ 38,000 ($ 4,000) $ 2000

The data inside () is negative numbers. When using the default sort function, it doesn't recognize ($ 4000) as a negative number, so I'm trying to create a custom function for that.

The solution below is what I came up with and it works great. But I'm wondering if there is a better solution? I am new to jquery and just looking for information.

$(document).ready(function() 
    { 
        $.tablesorter.addParser({
                // set a unique id
                id: 'currency-column',
                is: function(s) {
                        // return false so this parser is not auto detected
                        return false;
                },
                format: function(s) {
                         s = s.replace(/$/g,"");
                         s = s.replace(/\(/g,"-");
                         s = s.replace(/\)/g,"");
                         return $.tablesorter.formatFloat(s.replace(new RegExp(/[^0-9.-]/g),""));
                },

                type: 'numeric'
        });

        $("#myTable").tablesorter({
            headers: {
                3: { sorter:'currency-column' },
                4: { sorter:'currency-column' },
                5: { sorter:'currency-column' },
                6: { sorter:'currency-column' },
                7: { sorter: false },
                10: { sorter:'currency-column' }
            }
        }); 

    } 
);

      

Again, it does what is required and I'm happy with the results (and maybe I should just leave it there ...), but if there is a better solution I'd love to hear it.

thanks

+1


source to share


1 answer


How abour remove ","

s = s.replace(/,/g,"");

      

So your function will become:



format: function(s) {
         s = s.replace(/$/g,"");
         s = s.replace(/\(/g,"-");
         s = s.replace(/\)/g,"");
         s = s.replace(/,/g,"");
         return $.tablesorter.formatFloat(s.replace(new RegExp(/[^0-9.-]/g),""));
},

      

^. ^

+2


source







All Articles