OneTwo
OneTwo

Reputation: 2483

How can I use different data for displaying and sorting a column with jquery Datatables?

I have a datatable which has a Date/Time column. But I have my dates formated so that they show as "Three hours from now" or 'Three days ago'. This means when I click on that column to sort by it it will sort these by alphabetical order. I want to sort this column by the actual date. Can I set the data of this column to the actual date and set the label of it as this nicer date? My datasource for the datatables is HTML DOM not ajax.

Upvotes: 0

Views: 68

Answers (1)

OneTwo
OneTwo

Reputation: 2483

Figured it out. I added the actual date as an extra column and made it invisible. Then I used orderData to make the nice date order based on the extra date column.

I added the "columnDefs": part to my initialisation to do this.

pendingTasksTable = $("#requestsTable").DataTable({
     order: [2, 'desc'],
     "bPaginate": true,
     "bLengthChange": false,
     "bFilter": true,
     "bInfo": true,
     "bAutoWidth": false,
     pageLength: 12,
     "columnDefs": [
        {"orderData": [4], "targets": 2},
        {"visible": false, "targets": [4]}
     ]

});

Upvotes: 2

Related Questions