Editor - after postEdit, problems getting ID to send with ajax call
Editor - after postEdit, problems getting ID to send with ajax call
asle
Posts: 96Questions: 28Answers: 0
I have a postEdit function that works fine but I cannot get the row ID to send with an ajax call. What am I doing wrong?
// function after edit
editor.on( 'postEdit', function ( e, json, data ) {
// Below line works fine and returns cells value
alert( 'Row is edited with data: ' + data.fritekst + ' : ' + data.utedel_varenr);
var table = $('#example').DataTable();
// Below alerts "undefined"
console.log( table.row( this ).data());
var id = table.row( this ).id();
alert( 'Clicked row id '+id );
var idrow= table.row( { selected: true } ).id();
// Below alerts "NaN"
alert(+idrow);
// Below sends data to update.php
/* $.ajax({
type: "POST",
url: "update.php",
data: {
id: id,
date: data.fritekst
}
}) */
} );
This question has accepted answers - jump to:
Answers
Do alert(idrow) without the +.
The row id isn't numeric I guess, it is "row_1234" or similar. Just take a look and use a substring without the prefix.
I solved this with adding this to the controller file "staff.php" file:
I notice the json data sent from the server script -> DT_RowId: "row_37" and now with the code above I also get:
DT_RowId: "row_37", id: "37"
So now this works and I can send the id:
I am learning all the time :-)
I wonder why I am not able to get the rows id with
var idrow= table.row( { selected: true } ).id();
There is no click function on the row but "editor.on" so I thought maybe activating editor (that is what I do when I click the field to edit it?) would let me get the rows ID. Now I have defined a new $.fn.dataTable.Editor() and only on one field do I have inline edit
I am ok with using data.id but just wanted to understand when I can/should use the above table.row( { selected: true } ).id()
The
row().id()
docs state this:Are you using the
rowId
option to set the row ID torowId: "id"
?Kevin
Sorry, I thought I had answered your comment. I tried the rowID option but that was "undefined". But I got the id from data.id .
The Datatable options are case sensitive. Using
rowID
won't work. UserowId
instead. I provided a simple example showing the use ofrowId
when selecting a row:http://live.datatables.net/jofojege/1/edit
Can you post a link to your page or a test case showing that using
rowId
doesn't work?https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin