Sorting Date Table

Sorting Date Table

eTrackITeTrackIT Posts: 2Questions: 1Answers: 0

I am totally new to datatables and I want to sort my date table from the most recent date to the oldest

I am trying to play with the code as below:

$('#datatable2').dataTable({
  "aoColumnDefs": [{
    'bSortable': false,
    'aTargets': [-2]
  }],
  "oLanguage": {
    "oPaginate": {
      "sPrevious": "",
      "sNext": ""
    }
  },
  "iDisplayLength": 10,
  "aLengthMenu": [
    [5, 10, 25, 50, -1],
    [5, 10, 25, 50, "All"]
  ],
  "sDom": '<"dt-panelmenu clearfix"lfr>t<"dt-panelfooter clearfix"ip>',
  "oTableTools": {
    "sSwfPath": "vendor/plugins/datatables/extensions/TableTools/swf/copy_csv_xls_pdf.swf"
  }
});

bSortable as far as I understand is false to prevent a specific coulomb to sort, but where would I enter the sorting on a coloumb where my date is in. If I understand the coloumbs work from 0 --> and my date field will be in 2

I have tried ordering in my SQL query but it doesn't work looks like it is being ignored totally and the date is ordered from old to new on the bottom

Answers

  • kthorngrenkthorngren Posts: 20,294Questions: 26Answers: 4,768

    What version of Datatables are you using?

    You are using legacy commands which is ok but if you are using 1.10.x then you might want to consider changing $('#datatable2').dataTable({ to $('#datatable2').DataTable({. Please see the first FAQ here for an explanation.
    https://datatables.net/faqs/index#Most-common-FAQs

    Also if you are using 1.10.x then TableTools has been retired in favor of using the Buttons extension.

    The generally recommended way to sort dates is explained in this blog:
    https://datatables.net/blog/2014-12-18

    Kevin

  • eTrackITeTrackIT Posts: 2Questions: 1Answers: 0

    Ok so by the looks of it the date field might be more complicated to sort, I have an ID field as well that comes from my database. How would I sort that ID Field rather as this would be a straight numeric number

    I am not sure what Version I am using as I inherited the website

This discussion has been closed.