After create: can "row" be told to expect a JSON object?
After create: can "row" be told to expect a JSON object?
https://editor.datatables.net/manual/server
I'm looking at this and it makes sense on how I can send back some additional fields after sending the new record to the server, and getting fields back such as the ID of the database record, the name of the record creator, the timestamp, and more.
The Ajax call will now return the data in a JSON structure with the name of "row", but I just realized that I'm sending it as a true object and DataTables seems to be expecting an array.
For clarity, an example of what got sent back after a successful Ajax call to the server for a create event. The structure is a valid JSON object (but not an array).
{
"row": [
{
"technology_id": "3",
"change_data_ind": "Y",
"language_cd": "en",
"updated_local_ts": "Apr 29, 2015 5:42pm",
"technology_note_id": "10",
"session_id": "75bbd3fa3068f81e8667b5b0cd24c6df",
"created_user": "Peter Smith",
"DT_RowId": "row_10",
"updated_gmt_ts": "2015-04-30 00:42",
"note_txt": "69"
}
]
}
Is there a parameter here to tell DataTables to expect a true JSON object?
This question has an accepted answers - jump to answer
Answers
DataTables can accept objects absolutely no problem. Use the
columns.data
option to tell it what property name to use from the data source object for each column. Almost all of the Editor examples do this.I guess the question then becomes, why is DataTables currently expecting arrays rather than objects like most of the examples - are you Ajax loading the table, or is it being read from the DOM?
Regards,
Allan
In this prototype, it is already loading from the database using Ajax, and I can use Ajax on the create to send the new record to the database. Here is the columns definition:
Should I adding more to this block to represent "row"?
I know I'm close on this...
An example when adding a new row.
The Ajax call on create sends this JSON object to the database server, and a new record is successfully created in the database. I just copied it out of the Inspector in Chrome so please excuse the formatting.
On that same Ajax call I currently have it responding back with a JSON object, which is just fully populated with data that exists only on the server.
Again, I'm wanting to have DataTables immediately show this data in the table. Either I'm close based on the documentation, or I need to have Ajax do a reload (not preferred).
Ah - I think I see where the problem is. In your
row
return (which is correct - you want to do that), it is returning an array with an object it in. You actually want it to just give an object:At the bottom of every example on the Editor site is an 'Ajax data' tab which shows the format that has been used for the transaction in that example. Additionally the client / server communication protocol is documented in the manual.
Allan
I confirmed that Allan's solution was correct. I changed the program to return a JSON object (instead of a JSON array of objects) after creating the data in the database, and the Editor successfully took the response and immediately showed the fields in the table.