ordering changes after using render on column?

ordering changes after using render on column?

mihomesmihomes Posts: 150Questions: 19Answers: 0
edited April 2014 in DataTables 1.10
The 8th column in my dt is a timestamp column and I am using this as the default order as so :

[code]"order": [[8,'desc']][/code]

An example of the output in this column correctly shows in descending order.

2014-04-01 20:05:00
2014-04-01 16:27:00
2014-04-01 12:27:00
2014-04-01 09:28:00
2014-03-31 20:13:00
2014-03-31 18:41:00
2014-03-31 14:13:00

Now, if I render this column to show a more user friendly output with :
[code]
{
"render": function ( data, type, row ) {
if (data == '')
{
return 'n/a';
}
else return $.format.date(data, 'MMMM D, yyyy @ h:mm a');
}
}
[/code]

the output in my table becomes :

March 31st, 2014 @ 8:13 PM
March 31st, 2014 @ 6:41 PM
March 31st, 2014 @ 2:13 PM
April 1st, 2014 @ 9:28 AM
April 1st, 2014 @ 8:05 PM
April 1st, 2014 @ 4:27 PM
April 1st, 2014 @ 12:27 PM

which is no longer in the same order. Shouldn't ordering stay the same (using the default data shown above) regardless of what rendering I apply to it? Is there any way I can correctly order these rendered dates?

This particular table is using dom elements (whereas all my others user server-side)... is that the reason why ordering is behaving in this manner? The reason I am using dom is because I could not sort non-database values I was inserting into the table.

Replies

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    > Shouldn't ordering stay the same (using the default data shown above) regardless of what rendering I apply to it?

    No, because your rendering function is returning a date format that the `Date.parse()` function in Javascript doesn't understand, and that data is being used for the order.

    However, you can use the `render` function to return different data for type detection and sorting - see http://next.datatables.net/manual/orthogonal-data#Computed-values

    Allan
  • mihomesmihomes Posts: 150Questions: 19Answers: 0
    WONDERFUL! I had no idea you could do this with rendering.

    I don't suppose there is a way to do this with server-side is there? I have a situation where I have one value (an ip address) in the database. I use this ip address in another function to return its city, state, country, etc. which are not in the database. I can do this, but of course the sorting and searching does not work as it sees the ip and not the newly rendered value.

    Hope that makes sense.
This discussion has been closed.