JavaScript jQuery tablesorter sorting formatted currency jquery tablesorter sorting string,jquery tablesorter sort by data attribute,

EDIT, new code based on Mottie's answer:

jQuery.tablesorter.addParser({
    id: "monetaryValue",
    is: function (s) {
        return false;
    }, format: function (s) {
        return s.replace('$','').replace(/,/g,'');
    }, type: "numeric"
});
var tablezor = jQuery(".tablesorter");
tablezor.tablesorter({
        headers: {
            4 : { sorter: "monetaryValue" }
        },
        sortList: [[7,0]]
});

and HTML:

<table class="tablesorter">
<thead>
    <tr>
        ...
        <th>Processing Fees</th>
        ...
    </tr>
</thead>
<tbody>
{% for cs in customer_stats %}
    <tr class="js_striped">
        ...
        <td>
            <script>
                var options = {style:"currency", currency:"USD", minimumFractionDigits: 2,maximumFractionDigits: 2};
                document.write(new Intl.NumberFormat("en-US", options).format({{ customer_stats[cs]['processing_fee'] }}));
            </script>
        </td>
        ....
    </tr>
{% endfor %}
</tbody>
</table>

I've tried a dozen examples to fix this issue, and nothing is working.

I'm using jQuery.tablesorter to sort a table, and the currency columns will not sort correctly when I format the numbers as currency (US Dollars, in the format $1,945.00)

It's a Django template, and the template variables render as unformatted numbers like 1945.0, so I need to add formatting using javascript. I wanted to use the Humanize template filters library, but we use Jinja, which doesn't have that. I need to do the formatting on the client, so python suggestions probably aren't going to work.

When it's just a number, it sorts fine. When I use a little script tag (yes, I know this isn't the best way, it's a short-term fix untill we re-write the front-end with Backbone) to format the number as currency, the sort doesn't work. It sorts like this:

$3,380.00

$350.00

$353.24

$3,535.24

etc.

here's the function to format as currency:

function formatDollar(num) {
    var p = num.toFixed(2).split(".");
    return '$' + p[0].split("").reverse().reduce(function(acc, num, i, orig) {
        return  num + (i && !(i % 3) ? "," : "") + acc;
    }, "") + "." + p[1];
}

Here's the HTML:

<table class="tablesorter">
    <thead>
        <tr>
            ...
            <th class="{sorter:'monetaryValue'}">Processing Fees</th>
            ...
        </tr>
    </thead>
    <tbody>
    {% for cs in customer_stats %}
        <tr class="js_striped">
            ...
            <td>
                <script>
                    var money = formatDollar({{ customer_stats[cs]['processing_fee'] }});
                    document.write(money);
                </script>
            </td>
            ....
        </tr>
    {% endfor %}
    </tbody>
</table>

and the custom parser for tablesorter:

var tablezor = jQuery(".tablesorter");
tablezor.tablesorter({
        sortList: [[1,1]]
});
tablezor.addParser({
    id: "monetaryValue",
    is: function (s) {
        return false;
    }, format: function (s) {
        return s.replace('$','').replace(/,/g,'');
    }, type: "numeric"
});

Feel free to tell me how terrible any of this is; Any and all criticizm is welcome if you can tell me how to get tablesorter to sort currency correctly.

Thanks,

Answer:1

The main issue here is that the parser needs to be added using $.tablesorter.addParser(), not $('.tablesorter').addParser().

jQuery.tablesorter.addParser({
    id: "monetaryValue",
    is: function (s) {
        return false;
    }, format: function (s) {
        var n = parseFloat( s.replace('$','').replace(/,/g,'') );
        return isNaN(n) ? s : n;
    }, type: "numeric"
});
var tablezor = jQuery(".tablesorter");
tablezor.tablesorter({
        headers: {
            0 : { sorter: "monetaryValue" }
        },
        sortList: [[1,1]]
});

If you are using my fork of tablesorter, the default (automatically detected) currency parser will work with the provided currency values (demo).

For currency from other countries, just set the usNumberFormat option to false.

Answer:2

Sorry, for that (but I´m a regex noob) in js how would one replace the first folder in a url string. Pseudo Code: Replace at start of string: /any-amount-of-characters/ with nothing Examples $...

Sorry, for that (but I´m a regex noob) in js how would one replace the first folder in a url string. Pseudo Code: Replace at start of string: /any-amount-of-characters/ with nothing Examples $...

  1. regex replace first occurrence
  2. regex replace first character
  3. regex replace first and last character
  4. regex replace first group
  5. regex replace first occurrence python
  6. regex replace first line
  7. regex replace first character if
  8. regex replace first 5 characters
  9. regex replace first character with uppercase
  10. regex replace first letter to uppercase
  11. regex replace first occurrence of character
  12. regex replace first space with comma
  13. regex replace first character lowercase
  14. regex replace first capture group
  15. regex replace first occurrence c#
  16. regex replace first n characters
  17. regex replace first two characters
  18. regex replace first 3 characters
  19. regex replace first tab
  20. regex replace first comma

What are the scenarios when you want to use CDN to load a javascript file? And which files you might want to lazy load. Or perhaps which scripts are the best candidate to be combined and minified? ...

What are the scenarios when you want to use CDN to load a javascript file? And which files you might want to lazy load. Or perhaps which scripts are the best candidate to be combined and minified? ...

I'm working on a jQuery plugin that does not have a selector. When initializing it, I instanciate an object that has functions. In these functions, I need to use closures. In these closures, I would ...

I'm working on a jQuery plugin that does not have a selector. When initializing it, I instanciate an object that has functions. In these functions, I need to use closures. In these closures, I would ...

  1. call javascript class from html
  2. call javascript class function
  3. call javascript class
  4. call javascript class method
  5. javascript call class from another file
  6. javascript call class method from another method
  7. javascript call class method by string
  8. javascript call class constructor
  9. javascript call class method by name
  10. javascript call class method from constructor
  11. javascript call class method from callback
  12. javascript call class without new
  13. javascript call class static method
  14. javascript call class by string
  15. javascript call class function from string
  16. javascript call class method from html
  17. javascript call class function by name
  18. javascript call class function from event
  19. javascript call class css
  20. javascript call class attribute

I need to store client side data temporarily. The data will be trashed on refresh or redirect. What is the best way to store the data? using javascript by putting the data inside a variable var data ...

I need to store client side data temporarily. The data will be trashed on refresh or redirect. What is the best way to store the data? using javascript by putting the data inside a variable var data ...