Can't get currency sorting to work

Can't get currency sorting to work

ClayClay Posts: 6Questions: 0Answers: 0
edited August 2009 in General
I've got a pretty standard USD currency column but sorting doesn't seem to detect that it's a currency. For my particular set of data, when I sort, I get the following:

$6.00
$500.00
$500.00
$50.00
$5.00
$5.00
$5.00
$4,072.66
$10,634,884.55

Do I need to enable something specific for type auto-detection?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi Clay,

    Did you include the currency type-detection and sorting plug-ins?

    Type detection: http://datatables.net/plug-ins/type-detection#currency
    Sorting: http://datatables.net/plug-ins/sorting#currency
    Example: http://datatables.net/examples/api/sorting_plugin.html

    Regards,
    Allan
  • ClayClay Posts: 6Questions: 0Answers: 0
    I think my confusion stems from how exactly to turn on the plugin...
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi Clay,

    If you've included the two plug-ins as shown in the example, then it should all just automatically work. The column type will be detected as 'currency' and the column will sort as required. Make sure that the plug-ins are added after the DataTables JS file, but before initialising the table.

    Allan
  • ClayClay Posts: 6Questions: 0Answers: 0
    Well, I've added that chunk of code to a .js that's loaded after jquery and data tables, and it doesn't look like it's being called at all (i threw a couple alerts in there)

    Is there anything I can do to debug this to see which functions it's calling to sort this? Perhaps it's detecting it wrong?
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi Clay,

    If you run "console.dir( oTable.fnSettings().aoColums );" in Firebug and look for the array element which contains the information for your currency column, that will tell you the type - and if it's been detected correctly or not. Failing that, then there might be a bug in the sorting functions themselves. Do you have a link you could provide - it might made debugging easier. If you have one but don't want to make it public, you can send it to me direct at http://datatables.net/contact .

    Regards,
    Allan
  • ClayClay Posts: 6Questions: 0Answers: 0
    Ok, looks like it's being detected as a string, where the column right next to it is correctly being detected as a date.

    Unfortunately this particular project is an internal company, so I am unable to let you take a look. I realize this makes it much more difficuilt for you :(

    Is there any way to debug the sort-detect function?

    Thanks!
  • ClayClay Posts: 6Questions: 0Answers: 0
    I did notice that in _oExt.aTypes in the datatables source, there's no references to currency, whereas there's stuff for date, string and numeric....

    I'm using v1.5.1
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    edited September 2009
    Hi Clay,

    Assuming you have the currency type detection plug-in installed correctly, it should add currency type detection to the aTypes array. The formatting for the currency detection is fairly strict, you can't have any white space in the table cell (you can modify the plug-ins to add support for this if you need), and only there characters -0123456789 and . or , are allowed, with a $ or £ at the start. Does that match your current formatting?

    Regards,
    Allan
  • ClayClay Posts: 6Questions: 0Answers: 0
    Welp, turns out I had the sort functions in there, but not the auto detection. Maybe they should be all together instead of split up? Or maybe I'm just dumb.

    Regardless, it works now so I'm going to go sit in the corner and be ashamed of myself for not figuring it out on my own.

    Thanks!
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi Clay,

    They could probably do with a link from one to the other on the plug-in pages other than just being mentioned in the text. I'll add it to the to-do list :-)

    Good to hear you got it sorted.

    Allan
This discussion has been closed.