How to get the id of the selected row?
How to get the id of the selected row?
Hi,
I'm building a web app over Laravel framework.
In this app, I have a simple Datatable which ID is table_employees.
I'm using my own server-side scripts.
Editor's "create" method is OK, it works.
However I can't delete a record because I need to pass the id of the current record in the URL sent to my controller.
I'm using RESTful routes and controllers: so deleting a record (e.g. an "employee") is as simple as sending an AJAX "delete" request to URL /employees/id.
I tried '/employees/'+id but obviously I get a "undefined variable 'id'" error message...
So my question is simple: in my "remove" part of the code, how can I retrieve the id of the current selected row in order to pass it to URL like url : '/employees/'+id ?
Thanks a lot for your help.
Here is my JS code below:
Regards,
editor_employees = new $.fn.dataTable.Editor( {
ajax: {
create: {
type : 'POST',
url : '/employees' //OK, this works
},
remove: {
type: 'DELETE',
url : '/employees/'+id, // THIS DOES NOT WORK AS "id" IS UNDEFINED...
}
},
table: "#table_employees",
idSrc: "id", // necessary???
fields: [
{
label: "lastname",
name: "lastname",
}, {
label: "firstname",
name: "firstname",
}, {
label: "birthdate",
name: "birthdate",
}, {
label: "job",
name: "job",
}
]
} );
// Datatable
$('#table_employees').DataTable( {
dom: 'Bfrtip',
ajax: {
url: "/employees",
dataSrc: "",
},
columns: [
{ data: "lastname" },
{ data: "firstname" },
{ data: "birthdate" },
{ data: "job" }
],
select: true,
buttons: [
{ extend: "create", editor: editor_employees, text: 'Add' },
{ extend: "remove", editor: editor_employees, text: 'Delete' }
]
});
This question has an accepted answers - jump to answer
Answers
Use
url : '/employees/_id_',
. See the URL parameters section of theajax
documentation.Allan
It works! Thank you so much Allan.
Regards,