WordPress.org

Ready to get started?Download WordPress

Forums

WP-Table Reloaded
Sorting Currency Columns (4 posts)

  1. ncitravel
    Member
    Posted 4 years ago #

    Hello again Tobias - you're looking well ;0
    I have a currency column which is sorting as text (eg $1, $100, $2, $200). I have tried injecting the "aoColumns": [null, null, null, { "sType": "numeric" } code & also { "sSortDataType": "dom-text", "sType": "numeric" } but it does a strange thing sorting the first row. I suspect it needs a plugin but I'm not sure how to implement this with your wp-Table-Reloaded... eg http://datatables.net/plug-ins/sorting#how_to_type
    If a demo helps have a look at http://ibooknewzealand.com/category/activities/
    (although I might have to take out the above code to get a working table before you look at it)
    Kind Regards, Joan.

  2. TobiasBg
    Member
    Plugin Author

    Posted 4 years ago #

    Hi Joan,

    I had a quick look at your site. Could it be that that Currency Converter thing is messing with the sorting? Can you turn it of just for testing?

    Also, instead of using the "numeric" value in the "sType" field, try "currency". That value has to be made known to DataTables first though, using the following code which needs to be pasted into the page before the $("...").dataTable(...); call.
    That should not be a problem in your case, as you seem to have manually changed that code anyways.

    /*Sorting code from DataTables*/
    jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
        /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
        var x = a == "-" ? 0 : a.replace( /,/g, "" );
        var y = b == "-" ? 0 : b.replace( /,/g, "" );
    
        /* Parse and return */
        x = parseFloat( x );
        y = parseFloat( y );
        return x - y;
    };
    
    jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
        /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
        var x = a == "-" ? 0 : a.replace( /,/g, "" );
        var y = b == "-" ? 0 : b.replace( /,/g, "" );
    
        /* Remove the currency sign */
        x = x.substring( 1 );
        y = y.substring( 1 );
    
        /* Parse and return */
        x = parseFloat( x );
        y = parseFloat( y );
        return y - x;
    };

    Best wishes,
    Tobias

  3. gaso99
    Member
    Posted 4 years ago #

    I have the same problem as the OP, currency values with commas can't be sorted correctly. I'm a noob so can you tell me exactly where this code needs to be pasted and do I need to do any other modifications?

    Thanks.

  4. TobiasBg
    Member
    Plugin Author

    Posted 4 years ago #

    Hi,

    unfortunately, it is kind of hard to describe this, because you will need to use a plugin filter, which is done best with a WP-Table Reloaded Extension.
    So, my suggestion is: Write me an email and I will get the code to you
    (address is in the main plugin file).

    Best wishes,
    Tobias

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic