Currency Sorting Example

Currency Sorting Example

tjrowetjrowe Posts: 4Questions: 0Answers: 0
edited August 2010 in General
Are there any examples of the currency sorting and the currency type detection functions posted anywhere?

I have seen the code snippets on the plug-ins pages, but I am looking for a full blown examples like those in the examples section. For some reason I haven't been able to get the currency sorting working and I wanted a live sample to compare it to in an attempt to discover what I am doing wrong.

Love DataTables by the way, everything else is working really well.

Replies

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    There isn't an example of the currency sorting itself, but there is an example of how to get sorting with type detection going: http://datatables.net/examples/plug-ins/sorting_plugin.html

    If that doesn't work, perhaps you can post your initialisation code here.

    Allan
  • tjrowetjrowe Posts: 4Questions: 0Answers: 0
    Allan, I looked over the example in the link you provided, and to my newbie eyes, it seems that I am setting everything up properly.

    Here is my initialisation code.

    [code]

    jQuery.fn.dataTableExt.aTypes.push(
    function ( sData )
    {
    var sValidChars = "0123456789.-,";
    var Char;

    /* Check the numeric part */
    for ( i=1 ; i
  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    I would agree that looks right. Are you using DataTables 1.7? If not then do you have white space in your table - which would stop this from working (1.7 will strip the white space automatically), and DataTables would treat it as a string which is what appears to be happening here...

    Allan
  • tjrowetjrowe Posts: 4Questions: 0Answers: 0
    I am indeed using DataTables 1.7.

    Table code looks like this:
    [code]




    First Name
    Last Name
    Current Vehicle
    Book Value
    Equity
    Payoff



    Nancy
    Bell
    2006 Honda Civic Si
    $11,323.34
    $7,856.23
    $3,265.45


    Robert
    Berray
    2007 Honda Pilot
    $15,232.33
    $13,523.96
    $2,523.21


    [/code]

    This is just a sample table so the links don't go anywhere right now.
  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    Oh - it's the links which are "killing" it. The auto detection won't strip out the links using the code you've got there. So DataTables is indeed seeing it as a string, since it doesn't match any of the known types.

    What you need to do is to combine the currency plug-in you've got there, with the "number with HTML" plug-in: http://datatables.net/plug-ins/sorting#numbers_html . The auto type detection will need a similar, but small, modification.

    Regards,
    Allan
  • tjrowetjrowe Posts: 4Questions: 0Answers: 0
    Thank, Allan. I hadn't considered the links as being a source of the problem.
  • jphornjphorn Posts: 2Questions: 0Answers: 0
    Hi Allan, tjrowe,

    I've got a table similar to tjrowe's with href links, but with euro currency instead of dollar signs and I just can't get it to sort right. Is there a full blown example which combines the two plugins 'currency sorting' and 'number with html', preferably with euro signs as well?
  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    Rather than search for just the € character, you might need to search for "& euro;" (without the space), and remove that, since it's an HTML entity. I've made a note to look at setting up a currency sorting example at some point and updating the plug-in with this.

    Allan
  • jphornjphorn Posts: 2Questions: 0Answers: 0
    edited August 2010
    Hi allan,

    Thanks. What I don't get is how to combine the two plugins. Do I need to run Numbers with HTML first and Euro currency afterwards or do they need to be combined in one
  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    Yes they will need to be combined together. So the first thing to do is to strip the HTML from the input. Something like this: "x = a.replace( /<.*?>/g, "" );". Then parse x as currency, like the above plug-in.

    Allan
  • matthart1983matthart1983 Posts: 1Questions: 0Answers: 0
    edited December 2010
    Our team encountered an issue using the code above which stemmed from having currency being rendered in formed of -$XXXX which ultimately caused the javascript to fail. A fix for this can be found below.

    [code]
    var x = a == "-" ? 0 : a.replace( /,/g, "" ).replace(/\$/g,"");
    var y = b == "-" ? 0 : b.replace( /,/g, "" ).replace(/\$/g,"");

    //x = x.substring( 1 );
    //y = y.substring( 1 );
    [/code]
This discussion has been closed.