How do I sort date columns?

How do I sort date columns?

totallyplandomtotallyplandom Posts: 27Questions: 1Answers: 1
edited October 2011 in DataTables 1.8
Hi All,

I've looked around and seen some amazingly complex solutions to the problem I have and am wondering what the most straightforward mechanism is. Basically, I need to have my date columns sort by something other than date as they are displayed in "m-d-Y" format and are sorting based on the month. I need to have another property (perhaps a "data" attribute with a totalseconds attribute) that the table uses to sort those particular columns. Is there anything remotely straightforward to do this?

If not, I've seen various "plugins", but I'm honestly unsure as to how to actually make use of these things. Any assistance would be most appreciated.

Best!

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    to use the plugins on the http://www.datatables.net/plug-ins/sorting page, you would include the code provided (either paste into your HTML or JS file or create a new JS file and link it in your main page).

    in your .dataTable() intialization, set "sType" for the column (in aoColumns or aoColumnDefs) you want to use that routine

    here's an example page: http://www.datatables.net/release-datatables/examples/plug-ins/sorting_sType.html
  • totallyplandomtotallyplandom Posts: 27Questions: 1Answers: 1
    Thanks for that reply. It makes a bit more sense now. The (what I considered) misuse of the term "plug-in" was confusing me. Anyway, I've made some progress, but I'm stumped again. I figured I'd start with something a *bit* more consistent -- currency. I attempted to use the currency "plug-in" on that page, but am receiving the following error when I actually attempt to click on the specified column:

    [code]TypeError: 'undefined' is not a function (evaluating 'k[(qa?qa:"string")+"-"+h[c][1]](H(a,O,F,"sort"),H(a,B,F,"sort"))')[/code]

    The table has six columns, and my initialization code looks like so:

    [code]

    $('#member_balance').dataTable({
    "aoColumns":[
    null,
    null,
    null,
    null,
    {"sType": 'currency-asc'},
    null
    ],
    "sScrollY": "400",
    "bScrollCollapse": true,
    "bPaginate": false,
    "bJQueryUI": true,
    "aaSorting": [[1,"asc"]]
    });
    [/code]

    Any thoughts?
  • RacingHippoRacingHippo Posts: 1Questions: 0Answers: 0
    You need to specify 'currency' as your sType, not 'currency-asc':
    [code]

    $('#member_balance').dataTable({
    "aoColumns":[
    null,
    null,
    null,
    null,
    {"sType": 'currency'},
    null
    ],
    "sScrollY": "400",
    "bScrollCollapse": true,
    "bPaginate": false,
    "bJQueryUI": true,
    "aaSorting": [[1,"asc"]]
    });

    [/code]
  • totallyplandomtotallyplandom Posts: 27Questions: 1Answers: 1
    *face palm* ... I'm sure that's in the documentation (somewhere), but I clearly missed it. It would be nice if all of that was mentioned explicitly (step-by-step) for conceptually hindered literalists like myself. =P

    Thanks again!
  • totallyplandomtotallyplandom Posts: 27Questions: 1Answers: 1
    Ok, I'm still having trouble with dates formatted "11-20-2011". They are sorting by the first number by default. Should I be using "11/20/2011" or some other format instead? Does it not sort US dates by default?
  • totallyplandomtotallyplandom Posts: 27Questions: 1Answers: 1
    That was, in fact, the problem. Again, I'm sure it was documented somewhere, but I have yet to come across it in my travels.
  • wadeowadeo Posts: 2Questions: 0Answers: 0
    edited November 2011
    My post was incorrect, I'm actually having problems with any sType, good luck with sorting out your date issue.
This discussion has been closed.