Sorting date column

Sorting date column

rosuandreimihairosuandreimihai Posts: 18Questions: 3Answers: 1

Hi all,
I am trying to sort a date column with the format 'DD MMM YYYY' using the moment.js plugin and following the example from https://datatables.net/plug-ins/sorting/datetime-moment but I cannot make it work.

Here is a debug from the datatables page:
https://debug.datatables.net/acunon

I use the following code:

$(document).ready(function() {

$.fn.dataTable.moment('DD MMM YYYY');
$.fn.dataTable.moment('DD MMM YYYY HH:mm');

var $table = $('table.datatable');
$table.DataTable();

});

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    edited October 2018

    Hi @rosuandreimihai ,

    We're happy to take a look. As per the forum rules, if you could link to a running test case showing the issue we can offer some help. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • rosuandreimihairosuandreimihai Posts: 18Questions: 3Answers: 1
    edited April 2018

    Hi Colin,

    Here is the code:
    http://live.datatables.net/giyerebi/1/edit

    But, it's not working.. it still does not sort the column correctly..

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @rosuandreimihai ,

    Can you give this a try, please - I think this is doing what you're after. I changed the way the table was being initialise in the function dt_inquiries_list. I've given it a try, and all ordering seems to be behaving as I'd expect.

    Please let me know how it goes.

    Cheers,

    Colin

  • rosuandreimihairosuandreimihai Posts: 18Questions: 3Answers: 1

    Hi @colin,

    Thank you for the answer, it does work like that, but I still don't get it why the plugin above is not working.. That plugin doesn't alter any of the functionalities..
    I still would like to use it, it has a lot of things I need within the app that uses the DataTable..

    Is there a way to prevent that behaviour?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    It should still work with that plugin - I've updated the code here, it just needs those definitions added before the table gets initialised.

    C

  • rosuandreimihairosuandreimihai Posts: 18Questions: 3Answers: 1

    Hi @colin,

    Thank you for all the help until now but still, even your last example, using the plugin provided is not sorting the dates as it should.. I don't know why, I tried adding the definition before the table is initialised but nothing..

    Thanks for all your support!

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    It appears to be working okay in Colin's example. Is it not working there for you, or when you implement it on your own page?

    Allan

  • rosuandreimihairosuandreimihai Posts: 18Questions: 3Answers: 1

    Hi @allan,

    It's not working in this example: http://live.datatables.net/kirixeqi/3/edit
    And I don't understand the reason..

    Any help would be appreciated!
    Thank you!

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    "serverSide": true, // enable/disable server side ajax loading

    That means that all sorting, filtering and paging is to be done by the server-side script. The client-side Moment sorting will not have any effect. You would need to update your server-side processing script to perform the ordering you want.

    Allan

  • rosuandreimihairosuandreimihai Posts: 18Questions: 3Answers: 1

    Hi @allan,
    Then, hoow should I do it keeping in mind that I need the date to be in the DD MMM YYYY format?

    Thank you!

  • rosuandreimihairosuandreimihai Posts: 18Questions: 3Answers: 1
    Answer ✓

    Ok, I got in thanks to you @allan!
    This is the part of code that orders the server side:
    if(isset($_POST['order'])) // here order processing
    {
    $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
    }
    else if(isset($this->order))
    {
    $order = $this->order;
    $this->db->order_by(key($order), $order[key($order)]);
    }

    The only thing that I can signal to anyone else is that the columns from the table MUST be in the same order as the ones declared in the server side. That is why it was not working form me!

    Thank you for all the support!

This discussion has been closed.