I currently have a json return that I want to put in my datatable but it doesn't do anything and as far as I'm concerned it's a valid value:
Return of the json:
Object { NombreProducto: "Refresco", PrecioUnitario: "15", Cantidad: "5", Importe: 75, Acciones: "<div class='col-lg-12'><div class='row'><div class='col-lg-6'>EL</div><div class='col-lg-6'>Up</div></div></div>" }
I build it as follows:
echo json_encode(array( 'NombreProducto' => $row["NombreProducto"], 'PrecioUnitario' => $row["PrecioUnitario"], 'Cantidad' => $_POST["Cantidad"],'Importe' => $importe ,'Acciones' => $Acciones ));
My table is like this:
<table id="tb_AddProducto" class="table table-striped table-bordered" style="width:100%;" >
<thead>
<tr>
<th>Cantidad</th>
<th>Precio Unitario</th>
<th>Producto</th>
<th>Importe</th>
<th>Acciones</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th>Cantidad</th>
<th>Precio Unitario</th>
<th>Producto</th>
<th>Importe</th>
<th>Acciones</th>
</tr>
</tfoot>
</table>
The filling of my ajax is like this:
$.ajax({
url: "back_end/GetAddProduct.php",
type: "post",
dataType: 'json',
data: {selectProduct:""+$Product+"",Cantidad:""+$Cantidad+""},
error:function(xhr, status, error){
alert("error");
},
success: function(data) {
table.rows.add(data).draw();
console.log(data);
}
});
But it just doesn't fill up.
update
So I start the datatable in the document.ready:
table= $('#tb_AddProducto').DataTable( {
"language": {
"lengthMenu": "Mostrar _MENU_ Registros por Pagina",
"zeroRecords": "No se Encontro Nada - Lo Sentimos",
"info": "Mostrando pagina _PAGE_ de _PAGES_",
"infoEmpty": "No hay registros disponibles",
"infoFiltered": "(filtrado de _MAX_ registros totales)",
"sSearch": "Buscar",
"oPaginate": {
"sFirst": "Primero",
"sLast": "Último",
"sNext": "Siguiente",
"sPrevious": "Anterior"
}
}
});
Your response file
php
must meet a minimum format; must includedraw
: The counterdraw
to which this object responds - from the parameterdraw
sent as part of the data request. (must be an integer).recordsTotal
: Total records, before filtering (that is, the total number of records in the database)recordsFiltered
: Total records, after filtering (that is, the total number of records after filtering was applied, not just the number of records returned for this page of data).data
: The data to display in the table. This is an array of data source objects, one for each row, that will be used by DataTables.They must be in order as shown in the table (Quantity, Unit Price, Product, Amount, Actions)
more information
getting something like this