0

There was a question on SO relating to 'server-side' datatables but I'm not doing any server side in my DataTables plugin functionality.

I'm using the jquery plugin Datatables.js to do an $.ajax 'get' request, store the returned response in a hidden variable (for use later on in my application), and display the data in an html table. Everything works well except for the initial sort order, which I set to a date column. I then set the 'order' option of datatables to 'desc' (to display the date that is most recent, so October 10, 2017 would be displayed above September 26, 2017 and so on). The problem seems to be that my date response comes back in datetime format (2017-07-15 00:00:00.000), Datatables sorts it incorrectly, for example displaying 2017-10-04 00:00:00.000 AFTER 2017-07-15 00:00:00.000, instead of the other way around.

I'm not too familiar w/ datatables plugin, and was wondering if someone had a solution to fix this issue, as well as have the fix that is cross-browser as well. It would be nice if I could somehow convert this 'joined_date' field as date instead of a datetime as well. Following is the code I have:

$(document).ready(function() {

 jQuery.extend( jQuery.fn.dataTableExt.oSort, {
  "date-uk-pre": function ( a ) {
   var ukDatea = a.split('/');
   return (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
   },

   "date-uk-asc": function ( a, b ) {
    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
   },

  "date-uk-desc": function ( a, b ) {
   return ((a < b) ? 1 : ((a > b) ? -1 : 0));
  }
  } );
 $.ajax({
    type: "GET",
    url: "https://sonosfarm.com/prod/authors.cfc?method=Authors",
    dataType: "json",
    cache: false,
    success: function(response){
        if(response.length != 0){
            $("#authorsHiddenVar").val("");
            $("#authorsHiddenVar").val(JSON.stringify(response));
            $("#Authors").DataTable({
                data: response,
                columns:[
                    {
                        data: 'author_id',
                        "render": function(data, type, row, meta){
                            if(type== 'display'){
                                data = '<a href="' + "author.html" + '">' + 
     data + '</a>';
                            }

                            return data;
                        }
                    },
                    {
                        data: 'joined_date' 
                    },
                    {data: 'country'}
                ],
                responsive: true,
                order: [1, 'desc']
            });

        }
        else{
            console.log("There was no response from server!");
        }
    },
    error: function(XMLHttpRequest, textStatus, errorThrown){
        console.log("An Ajax server error was returned");
        console.log(XMLHttpRequest);
        console.log(textStatus);
        console.log(errorThrown);
    }
 });

});
Roger Dodger
  • 927
  • 2
  • 16
  • 37
  • 1
    https://stackoverflow.com/questions/12003222/datatable-date-sorting-dd-mm-yyyy-issue Have a look here, I think you're going to have to extend datatable – Steven Nov 14 '17 at 15:26
  • @Stephen - will take a look at it and see if I can figure it out; will update shortly; thx! – Roger Dodger Nov 14 '17 at 15:28
  • That link has "sType": "date-uk" but I can't see where that date-uk part is coming from (in the .extend func) - can you clarify? Also, will this work for US date format as well? – Roger Dodger Nov 14 '17 at 15:51
  • http://legacy.datatables.net/usage/columns sType is just a specification how the column is supposed to sort. Because he is extending the plugin, he creates a new stype that can be used. The date-uk is the algorithm he created for the purpose of what the dev asked for. So basically you can name it anyway you want – Steven Nov 14 '17 at 15:56
  • I added the extend function in my code but didn't seem to work. Updated the code in my original post to reflect what I tried. – Roger Dodger Nov 14 '17 at 16:04
  • 1
    well obviously, the stackoverflow I showed you is for a specific format that you don't follow. It was more to point you in the right direction – Steven Nov 14 '17 at 16:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158982/discussion-between-stephen-and-roger-dodger). – Steven Nov 14 '17 at 16:19

0 Answers0