ajax.dataSrc not working as expected (issue #468 on github)
ajax.dataSrc not working as expected (issue #468 on github)
The answer provided on Github worked partially. The data was loaded into the table, but the footer (paging and items count) didn't work. keeps showing "filtered from NaN total entries".
This discussion has been closed.
Answers
Have you moved the
drawand other properties to the top level of the JSON object, as I noted was required in the Github issue?Allan
Well, I can't do that. Thats the situation:
The server-side service always return an object of type
Responseto all requests.So there's this
Errorproperty that indicates if there was any server-side error, the array of error messages and de genericDataobject where the data itself is set, in case of success.This model works fine in all ohter parts of the system, i.e., I send a
Userobject in theDataand the client cast this generic object to theUsertype.In the case of DataTables, I have the type
DataTablesResponsewhich looks like this:And I send this in my generic
Dataobject. Thedrawis still the first parameter, but inside theData.That's why I oppened an Issue on Git instead of asking here.. I reasearch a lot and found this as a limitation. I reckon that many applications use this generic
Response.Datamodel so would be cool if we could set the root of the entireDataTablesResponseonajax.dataSrc, not only the array of data that is shown in the table.Thanks
The problem with using
ajax.dataSrcis that it points to the root array of the data for the table - not the root object. This is the documented implementation and changing that would break everything that already depends uponajax.dataSrc.However, there is another option also involving
ajax.dataSrc- use it as a function and map the parameters as needed, manipulating the JSON.I've yet to work out a good way of offering the
dataSrclike ability for the server-side processing parameters...Allan
Hi Allan,
I tried doing that, but the function _fnAjaxUpdateDraw ( settings, json ) is getting the old json. In other words I can't get *json[modern]* because *json['draw']* is undefined. I can only access it through *json.d['draw']* (notice the 'd')
My situation is very similar:
Good point! The
draw, etc, parameters are being evaluated before thedataSrc](https://github.com/DataTables/DataTablesSrc/blob/master/js/core/core.ajax.js#L287) at the moment. I don't see any good reason why that shouldn't be swapped around. I'll commit that in, which will allow my above suggestion to work (apologies, I thought it was already like this!).Allan
Hello Allan,
I have an ajax response form the server which reads:
{'total': 13,
'firstRecord':10,
'from':1,
'itemsPerPage':10,
'wData':[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]
}
it was easy to display the data using:
dataSrc: function (json) {
json.start = json.from;
json.recordsTotal = json.total;
json.recordsFiltered = json.itemsPerPage;
return json.wData;
}
but no matter how I tried, I could not map the total, first record and the other pagination parameters because the pagination properties are evaluated from the loaded JSON before the dataSrc function kicks in.
I think my code is correct, judging from the above examples, the data shows correctly, but no pagination data is passed.
Is there another way to change the loaded JSON before any processing gets done (which is needed to have a parser) ?
Using 1.10.5 from cdn
Thank you
1.10.5 doesn't have the fix that I committed above. It is an, as yet, unreleased change and you would need to use the nightly version to be able to use it.
Allan