Through a function that searches according to the selection made in a select I am adding DataTable
the elements found in the search to a. the add to function DataTable
is as follows:
function ObtenerDatosContenedorPlasmaAMover(){
id_almacen = $('#id_almacenes option:selected').val();
$.ajax({
type: 'POST',
url: '{{ route('historicoMovimientos.ObtenerDatosContenedorPlasmaAMover') }}',
data: {
id_almacen: id_almacen.length ? id_almacen: null
},
success: function (data) {
if ($.isEmptyObject(data.errors)) {
/*cant_cajas = data.plasma;*/
limpiarErrores();
$('#contenedor').empty();
$('#contenedor').append(data.mostrar.mostrar);
$('#volumen_top').text(data.mostrar.total_cajas + ' Cajas');
//---------------------- Aqui adiciono a las filas de datatable el plasma ----------------------
for(var i= 0; i < data.mostrar.cant_cajas.length; i++){
var rowNode = $('#datatable').DataTable().row.add( [data.mostrar.cant_cajas[i].ubicacion, data.mostrar.cant_cajas[i].estado, data.mostrar.cant_cajas[i].codigo,
data.mostrar.cant_cajas[i].banco, data.mostrar.cant_cajas[i].plasma, data.mostrar.cant_cajas[i].cajas, data.mostrar.cant_cajas[i].recepcion])
.draw( );
rowNode.node().id = data.mostrar.cant_cajas[i].id;
}
/*$('#datatable').DataTable().refresh;*/
} else {
limpiarErrores();
$.each(data.errors, function (index, value) {
$('#_' + index).text(value);
});
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
}
});
}
After I add the elements in the search I have to select the elements of the table that I want to process, but I am not able to capture the id
one that I am assigning to it tr
and therefore I cannot capture these id in the controller. I am getting the id's while selecting the elements from the table and I do it like this:
$('#datatable tbody').on( 'click', 'tr', function () {
var tab = $('#datatable').DataTable();
if ($(this).hasClass('selected') ) {
eliminarArregloPlasma(tab.row(this).id());
$(this).removeClass('selected');
} else {
$(this).toggleClass('selected');
var variable = {
'id_plasma': tab.row(this).id()
};
arreglo_plasmas.push(variable);
console.log(tab.row.id);
}
} );
In the DataTables documentation I find that this way tab.row(this).id()
is a correct solution to get the id. Why doesn't it work for me and it gives me Undefined
. Do I need to do anything else once I add the row?
I was able to resolve the issue with the following code:
I hope it can be useful for someone who has the same doubt