I am loading information in a table using Datatables, in this table I would like to load some information in the parent row and other information in the child row using the Child Rows API , to display the data I use AJAX like this:
/* Formatting function for row details - modify as you need */
function format(d) {
// `d` is the original data object for the row
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' +
'<tr>' +
'<td>Fecha Factura</td>' +
'<td>' + d.FechaFactura + '</td>' +
'</tr>' +
'</table>';
}
$(document).ready(function () {
var table = $('#example').dataTable( {
"ajax": {
"type": 'POST',
"url" : './utileria.php',
"dataType": 'JSON',
"cache": false,
"data": {
'param' : 1,
},
},
columns: [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data" : "OrdenCompra" },
],
"order": [[1, 'asc']]
} )
// Add event listener for opening and closing details
$('#example tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = table.row(tr);
if (row.child.isShown()) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
row.child(format(row.data())).show();
tr.addClass('shown');
}
});
});
For my server part I use PHP and this is how I loop through my table.
<?php
header('Content-Type: text/html; charset=utf-8');
$param = $_POST['param'];
switch($param) {
case '1': //Consulta
$query = array();
include './db/conectar.php';
$sql = "select PURCHID as 'OrdenCompra',
INVOICEDATE as 'FechaFactura'
FROM PP_FACTURAS F";
$stmt = sqlsrv_query($conn, $sql);
if ( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array($stmt) ) {
//print_r($row);
$record = array(
"OrdenCompra" => $row['OrdenCompra'], //Orden de compra
"FechaFactura" => $row['FechaFactura'] //FechaFactura
);
array_push($query, $record);
}
sqlsrv_free_stmt( $stmt);
sqlsrv_close($conn);
$json = array(
"success"=> count($query) > 0 ? true : false,
"data"=>$query
);
echo json_encode($json);
break;
}
?>
Here comes the interesting part, currently I can show the Purchase Order field in my parent row but I would like my child row to show the Invoice Date field , to explain a little more detail what I mean by parent row and child row, I leave the example image.
In words more word less I would like the result of the DateInvoice field to be shown in the child row when the row is expanded.
I am not really facing a problem as such, in fact in the console I do not get any error, here the only problem is that the field in the child rows is not displayed FechaFactura
as I wish.
This is an example using AJAX, it provides a completely different example and I can't seem to figure out the syntax there.
It doesn't seem to work, somehow the data is not being delivered correctly. Any help would be great.
Update:
Add format
the statement to the function console.log(d);
to know the contents of d
but I don't get results in the console.
/* Formatting function for row details - modify as you need */
function format(d) {
// `d` is the original data object for the row
console.log(d);
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' +
'<tr>' +
'<td>Invoice Date</td>' +
'<td>' + d.InvoiceDate + '</td>' +
'</tr>' +
'</table>';
}
According to the documentation there is a difference in the way of initializing the tables:
Basically, when you initialize the table with
.DataTable()
, you get an instance for API access, including all the features. When you initialize with.dataTable()
, you get a jQuery object, without access to API features and methods.You just have to initialize with capital D and it works:
Use the API. Both to instantiate, to start the sub-rows, and to control the visibility: