Does DataTable sort on value of data or rendered representation?
Does DataTable sort on value of data or rendered representation?
Example:
I have data coming from server via ajax in the form:
{
"data": [{
"DT_RowId": "row_1",
"comment": "Doing Well",
"due_date": 1450790180000,
"commenter": "Joe Blogs"
}, {
"DT_RowId": "row_2",
"comment": "try harder",
"due_date": 1503407780000,
"commenter": "Mary Smith"
}]
}
due_date are int (milliseconds since epoch)
In my DataTables options, I'm using a render function on the due_date column as follows:
function ( data, type, row ){
return (data) ? new Date(data).toDateString().slice(4) : 'No Due Date'
}
The result is my int values get rendered as I want (nice human-readable format of my choosing):
1450790180000 => Dec 22 2015
1503407780000 => Aug 22, 2017
However, sorting is not working as expected. The column gets sorted alphabetically
This suggests to me that the sorting functionality of DataTables works on what is rendered to the table, rather than the data that drives the table (the actual source data)
This question has accepted answers - jump to:
Answers
Maybe you just need to render for the
display
type. An example is shown here:https://datatables.net/reference/option/columns.render#Examples
Look for the example titled
Show ellipsis for long strings
.Kevin
Worth reading over the orthogonal data section of the manual which deals with this topic.
Allan
Thank you!
The example on Computed values is EXACTLY the use case I was looking for!