Sort for date Column not working correctly even after using data-order attribute

Sort for date Column not working correctly even after using data-order attribute

maneeshnanumaneeshnanu Posts: 1Questions: 0Answers: 0

Hello All,

I have been looking through the Discussion threads for solution of date sorting. One solution I found was using data-order and it worked in some locations where the content was generated from server side by setting data-order attribute while creating table and then the dataTable was implemented and it worked. But there were places where the table was generated dynamically and was created using javascript. One problem in creating the dataTable from script was having trouble to add data-order attribute. I was not able to add the attribute in the columns property(e.g. columns variable below). I was somehow able to add the attribute to column by using createRow. But still the column was not sorting correctly.
e.g. the sorting was functioning like this :
'11-06-2016'
'12-07-2016'
'15-06-2016'

var columns = [
{
    "className" : "column-1",
    "data" : "itemId"
}, {
    "className" : "column-2",
    "data" : "Date"
}, {
    "className" : "column-3",
    "data" : "itemName"
},  {
    "className" : "column-4",
    "data" : "itemCount"
}
];
$request_table = $('#request-table');
var RequestTable = $request_table.DataTable({
    "processing" : true,
    "ajax" : {
        "url" : RequestUrl,
        "dataSrc" : function(data) {
            return data.data;
        }
    },
    "columns" : columns,
    "pageLength" : 10,
    "order" : [ [ 1, "asc" ] ],
    "aaSorting" : [],
    "rowId" : 'rowId',
    "searching" : false,
    "responsive" : true,
    "createdRow": function ( row, data, index ) {
        $('td', row).eq(1).attr('data-order',data.sortDate);
    }
});

Any help would be appreciated.

Replies

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26

    The only date format that datatables can reliably sort with is ISO-8601. You need to use a sorting plugin, such as this plugin

    You can't use createdRow to add the date-order parameter because the createdRow method might be triggered after the data has already been sorted, ie it's too late.

    Thanks

    Tom

This discussion has been closed.