Default order AJAX datatable by data property
Default order AJAX datatable by data property
Hi,
I have a DataTable whose contents are being loaded via AJAX. I know that to default order a DataTable on load I can use the order: [[10,"asc"]] property, however, this requires that I don't later decide to move my columns about. Is there a way to order by specifying a column name or something that isn't tied to the column's position? I've tried the following but have had no luck. It doesn't have to be the data property, just anything that prevents me from having to specify the column's position, so that if I decide to change things around later, I don't have to change the order property too.
order: {
data: "studentId",
dir: "desc"
}
An example of one of my columns.
columns: [
{
data: "studentId",
title: "Student ID",
visible: true
}
]
Apologies if this has been covered elsewhere but I couldn't see anything on the order reference pages.
Answers
You can use the
columns.nameproperty for this. The final example on theorderreference page shows it:Possibly I should look at allowing the
dataproperty to be used as well to reduce possible boiler plate, although thedataproperty can be nested JSON keys ornull, so it wouldn't always work.Allan
Hi @allan,
Thanks for getting back to me.
I've added a 'name' property to the column I wish to order by:
and have changed my
orderto the following:But it doesn't work unfortunately. Is it because this is the only column I've given a
nameproperty to?What version of DataTables are you using? I've just tried with the current release (3.0.2 at the time of writing) and it appears to work as expected: https://live.datatables.net/bicadeje/1/edit .
Regards,
Allan
Hi @allan,
Ah ok, it's probably the version I'm using then. I'm developing for a platform, the owners of which, have forked dataTables and effectively put their own wrapper around it to better work with their product. Best I can tell, it's version 1.13.11, but this may not be accurate.
Regards,
Guy
Hi Guy,
That would do it. Per the note in that example on the
orderreference page, that ability was added in 2.0.You can use
DataTable.versionto check the version, but it certainly does sound like you might have an old one.Allan
Hi, @allan,
Thanks for your help on this. I'll have to stick to using the index. Unfortunately, I'm not in control over the version of DataTables the company providing the platform chooses to use. It would definitely be nice if they updated to a newer version!
Guy
Out of interest, are you at liberty to say what platform it is that you are using?