Problem getting row id for remove item
Problem getting row id for remove item
Hi everyone,
I have some problem when remove items in a datatable, especially when remove the last item, when remove others no problem. this is part of code:
columnDefs: [{
"targets": -1,
"width": "120px",
"data": null,
"render": function (data, type, row, meta) {
return '<a class="btn btn-info mr-1" href="javascript:editar_linea(' + meta.row + ')" role="button"><i class="fas fa-user-edit"></i></a>' +
'<a class="btn btn-danger" href="javascript:eliminar_linea(' + meta.row + ')" role="button" onclick="return confirm(\'Confirma Eliminar el Item?\')"><i class="fas fa-trash-alt"></i></a>'
}
}]
I am calling function eliminar_linea(' + meta.row + ')" where I am calling a function in server...this is the code:
var eliminar_linea = function (fila) {
debugger;
tablaLineas.row(fila).remove();
tablaLineas.row(fila).draw();
var datosTabla = tablaLineas.columns([0, 2, 4]).data().toArray();
$.ajax({
type: "POST",
url: "Venta?action=DeleteItem&tablaLineas="+JSON.stringify(datosTabla)+"&idsesion="+idsesion,
dataType: 'json',
success: function (data) {
actualizar_footer(data);
},
error: function (xhr, exception) {
errorCallback(xhr, exception);
},
complete: function (data) {
//
}
});
};
the problem that I could see is that the row number (parameter fila) not always corresponding with the row of datatable, especially when someone remove some items previously.
Some idea how could correct this?
Regards!
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This question has an accepted answers - jump to answer
Answers
It would be worth looking at the Editor extension, this has full support for CRUD (create/read/update/delete) operations on the table's data. There, you would use
remove()
to delete the row.Colin
My suggestion is to use jQuery delegated events like this example. Use jQuery closest() to get the row of the clicked button. Use this row to remove the row.
Kevin
The
meta.row
parameter is not an ID but a row index, see therow().index()
docs for details. Another option is to pass a unique ID from the row data instead ofmeta.row
here ofeditar_linea(' + meta.row + ')
. UserowId
to set the appropriate column as the row ID. The pass the string #ID as therow-selector
.Kevin
Thank you kthorngren !