How to use Restful API when the datatable expects more data than was sent in with the create?
How to use Restful API when the datatable expects more data than was sent in with the create?
The restful api example works great, however, I have two differences in with my returned data compared to what i'm POSTing to the server.
1) I'm posting a dropdown language.id value to the server but my datatable needs to display the name
2) I am not posting a GUID, but am receiving a GUID back from the server in the POST results (my datatable wants to display this value)
I am getting this error:
DataTables warning: table id=track_table_19 - Requested unknown parameter 'language.name' for row 11. For more information about this error, please see http://datatables.net/tn/4
Basically I want the datatable to be updated with the results of the restful POST rather than using the values that I fill out in the editor form. I did serverSide:true and my POST worked perfectly. However, then the search, sort, pagination all stopped working as datatables was expecting the server to handle it (which I don't want to do).
Thanks.
This question has an accepted answers - jump to answer
Answers
Hi,
When Editor receives the data returned from the server on edit, it expects the data to be returned in a particular format, specifically a JSON object with a
row
parameter that defines the data for the DataTable row (manual). Is your server responding with that JSON data, or can you modify it to do so?Let's consider the Join example to illustrate why this is required. The Editor form will submit a value for
users.site
, but DataTables needs to displaysites.name
(basically the same as what you want, but with different parameter names).So Editor might submit:
And the server will reply with the full data object for that row:
If
sites.name
wasn't returned, then DataTables wouldn't have any information to be able to display anything about the site name.Are you able to have your REST API return data in the same format that DataTables is using for your rows?
Thanks,
Allan
Hi Allan,
My API does return JSON, but it doesn't wrap the return with "row". Is that a requirement from the server or can I use some javascript to wrap the results before the datatable gets it?
For example I was playing around with doing some like this on my editor form:
The json variable has the data from the server, but doesn't have the "row" wrapper.
Thanks again.
Hi,
You are absolutely correct that you can use
postSubmit
to manipulate the data returned by the server before Editor then uses it. What we need to be careful with though is the assignment of variables.json
is an object that is passed in and it a pointer (or reference) to the object. So we can't just assign the value we want to thejson
variable, but rather manipulate the object it points to.That is all a slightly complicated way of saying, try this:
This works in Javascript as the assignment isn't made until the right hand side has been evaluated. The resulting object will end up with all the original items still present, but Editor will just ignore them (as long as they aren't called
error
orfieldErrors
!).Regards,
Allan
Thanks Allan,
That worked:
Oops - yes, thanks for spotting that error. Your code is absolutely spot on.
Great to hear that helps!
Regards,
Allan
I tried
for editor version 1.4, too, the editing window is closed (as before, so JSON format is recognized), but the table is not updated. And if I try to debug that with a break point in browser, it don't show that this is used. What can be wrong here?
What has to be used for version 1.5, if the server responses with multiple records (JSON is recognized), but without the expected "data: "? json.data=json seems to be not correct.
data
is the property that Editor 1.5 looks for (rather thanrow
) - docs, so I'm a little surprised that doesn't work. Although you need to make sure that it is an array, since 1.5 expects an array of data objects - is that the case?Allan
I found the problem, my update object and my table have a different structure and I returned the structure from the edit window, not the structure from the table row. I changed that and now it is working. Thanksl!
Good to hear you got it working!
Allan