Retrieving the mysql ID for a newly created row.
Retrieving the mysql ID for a newly created row.
Hey All,
How would I be able to pass the mysql ID (Auto-Incrementing Primary Key value) created after DataTables Editor sends a new row to the database?
I have a 'submitSuccess' event which will call a subsequent AJAX function to insert a record into a linker table with the newly created ID from when the Editor creates the record, but I have no idea how to retrieve the ID value.
Any help would be greatly appreciated! I haven't been able to find any help online after hours of searching.
Thanks!
Replies
Hi,
The best way to do it is to have the server return the data for the row that has been created in the
row
object - that would include the row id (and everything else for the row). In this way you can handle cases such as the database using a default value for fields, etc.The client / server communication method that Editor uses is documented here, in the manual. There are some examples in the latter part of the page that might be of interest to you, particularly the
create
example.Allan
Thanks for the response. I was reading through the document you linked to and had a question. How do I parse through the data array that gets passed to a preSubmit function to find the row id of the table row to be deleted?
Consider the following:
In
preSubmit
the row id will be in theid
property of the data object passed in.This is the documentation that describes the format of the data Editor builds for submission.
Allan
Yes, but what is the code to read the property from the data object? The documentation does not have any coding examples. How would I reference the id from the data object defined in the code I'm using above?
When I do:
alert(JSON.stringify(data));
I can see the elements in the array printed; it shows the action and the ID of the row to delete, but how do I print a specific element value? For instance:
alert(data[0]);
returns "undefined"
Simply
data.id
should give you the id.You could
console.log( data )
to see the data structure in your browser's console if you want to inspect it that way.That would return
undefined
sincedata
is an object, not an array.Allan
Thank you!! ugghh, I can't believe I didn't try that.. : )