Datatables only sorts by int with error
Datatables only sorts by int with error
I have a datatables set up to read JSON from the server. One of the columns however should be sorted by it's hidden index versus what is displayed on the screen.
This is what I have to display the table:
$("#ad-table").dataTable({
"lengthMenu": [[100, 50, 25], [100, 50, 25]],
"ajax": {
"url": "assessmentsduetable?peid="+peId,
"dataSrc": ""
},
dom: 'Bfrtip',
"buttons": ['print', 'pageLength'],
"order": [[4, "asc"]],
"columns": [
{ "data": "Consumer ID" },
{ "data": "Last Name" },
{
"data": {
_: "WindowNameInfo.WindowDue",
sort: "WindowNameInfo.WindowDueIndex"
}
},
{ "data": "Window End Date" },
{ "data": "Days Left In Window" },
{ "data": "Assessment Name" },
{ "data": "Date of Last Assessment" },
{ "data": "# of Assessments" },
{ "data": "Clinician" },
{ "data": "Clinic" }
]
});
});
The problem is with WindowNameInfo.WindowDue and WindowNameInfo.WindowDueIndex. For some reason, it seems to be sorting on WindowDueIndex as if it were a string even though it is an integer. However, when I do this:
"data": {
_: "WindowNameInfo.WindowDue",
sort: "WindowNameInfo.WindowDueIndex",
type "int"
}
it throws an error, then continues to display the table, this time with it sorting properly!
This question has accepted answers - jump to:
Answers
Set that to be:
The
type
option also tells DataTables where to get the data to use for the type detection.If you do want to specifically set a type use
columns.data
, althoughint
is not one of the build in types. Best to just let the built in type detection do its thing with your data.Allan
Yes, that did fix the issue! However, there was a typo in that response. What did the trick was:
Thank you!
Oops - good spotting! Good to hear it helped though :-)
Allan