columns.data function works for display type but not for sorting?

columns.data function works for display type but not for sorting?

sj005sj005 Posts: 2Questions: 1Answers: 0

Hello All,

I'm using the DataTables 1.10.12 and it is an excellent plugin. I'm having a strange issue though. I have a table with parent rows and for each parent row, there is a table inside as a child row. The issue is that I can't sort a column with a date value. The table's data source is an array of objects and each object contains an object property called "evRcd" that contains a "timestamp" property (the record timestamp), a "dateFmt" property (the formatted date), and a "timeFmt" property (the formatted time). I have a column that displays the formatted date and time together separated by a space, with sorting done based on the timestamp property. Here is the function I've passed as the data property of the date/time column:

data: function(row, type, set, meta) {
var evRcd = row.evRcd;
return type=='sort' ? evRcd.timestamp : (evRcd.dateFmt + " " + evRcd.timeFmt);
}

When type is 'display', it works perfectly and shows the formatted date/time. But when type is 'sort', it doesn't work. The sort arrows flip but no sorting occurs. I have verified by logging that the function is indeed returning the timestamp when sorting and that the timestamps are valid positive numbers. If I return the timestamp in all cases (display,filter, and sort), the sorting works as expected. I have other tables on my site that use a similar function as the above and they work correctly. The only table that is not working is this one for some reason. Could anyone please offer any suggestions as to why this could be occurring? Thank you.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,055 Site admin
    Answer ✓

    I would strongly encorage you to not use columns.data as a function. I'm probably going to remove that from the documentation in fact. Use columns.render instead.

    Then use column().render() to get the rendered data.

    Allan

  • sj005sj005 Posts: 2Questions: 1Answers: 0

    Sorry for the late reply. Using columns.render seems to have fixed the issue. Thanks a lot Allan.

This discussion has been closed.