datatables tries to read by int index instead object name from data
datatables tries to read by int index instead object name from data
Hi,
i have implemented a clean webmethod in asp and datatables with the following code:
$('#list').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "FieldAgents.aspx/GetList",
"type": "POST",
"contentType": "application/json; charset=utf-8",
"dataType": "text",
"dataSrc": "data",
"data": function (d) {
return JSON.stringify({ "parameters": JSON.stringify(d) });
},
"columns": [
{ "data": "salutation" },
{ "data": "firstname" },
{ "data": "lastname" },
{ "data": "channel" },
{ "data": "company" },
{ "data": "region" }
],
"dataFilter": function (res) {
var parsed = JSON.parse(res);
return parsed.d;
}
}
});
the server returns this:
{"d":{"__type":"MyTestNameSpace.Classes.DataTableResponse","draw":1,"recordsTotal":1,"recordsFiltered":1,"data":[{"salutation":"Herr","firstname":"Max","lastname":"Payne","channel":"Channel X","company":"Company 1","region":"East"}]}}
everything seems to be fine, but i get this error:
DataTables warning: table id=list - Requested unknown parameter '0' for row 0, column 0.
Debugging dataTables.js i see it is calling _fnGetCellData which tries to fetch from row (which is an object) using int index and that obviously fails. It looks like dataTables isn't even checking for my columns setting it is expecting the row data to be an array. What is missing here?
This question has an accepted answers - jump to answer
Answers
Looks like your
dataFilter
function is not within theajax
option. What you might want to do is removedataFilter
and use that function as part of theajax.dataSrc
. You can supply a function toajax.dataSrc
. The docs have an example.Kevin
Nope, just tried dataSrc. Instead of the dataFilter option in ajax i used the following resulting in same error as above:
I think the problem is the
json
variable is a Javascript object not a JSON string when passed to thedataSrc
option. Try this:no it is a string, your solution did not work. it gives me json.d undefined because json is a string containing my result wrapped in "d" field
ajax.dataSrc
doesn't work very well with server-side processing at the moment - thedraw
etc properties need to be at the top level regardless ofajax.dataSrc
.Instead, remove the
ajax.dataSrc
option and use:inside the
ajax
object.That should then do it since the object inside
d
is what server-side processing needs.If that doesn't work, please link to a test case showing the issue so we can debug it.
Allan
i tried this already, but in your case you are returning a string, but looking at the DT code i see it is expecting the returning element in dataFilter to be valid JSON (not string containing JSON).
Im not sure how to create a test case, this is running as part of a big application, the data returned comes from a webmethod which fetches the data from DB.
And looking at the code i see it is always trying to fetch the cell content with data[0], data[1] etc. instead of data['salutation'], data['firstname'], etc. don't know why it is ignoring my columns definition.
I used dataSrc:"d.data" from my web service.
My sample is on GitHub.
https://github.com/bindrid/DataTablesServerSide/blob/master/Server%20Side%20Page
My reading of the jQuery docs was that
dataFilter
would return the modified data in its original state, but rereading it, that isn't the case, it says it can return anything. I haven't traced the jQuery code though, but it suggests thatreturn parsed.d
should work.Your
columns
array is inside yourajax
object. Move it outside of that and DataTables will pick up the columns array correctly.Allan
uuh, that one hurts. yeah, now it's working fine. thanks for pointing out. SOLVED.