Finally i made a file end-named with the php extension file.json.php, and manage it like a php normal file but adding an echo with the function json_encode().
This is a extract of how i made the file.
//only making to arrays an array (in this case for datatables dynamic columns on the fly)
$columns[] = array("data" => "id", "name"=>"Id");
$columns[] = array("data" => "number", "name"=>"numeroItem");
$columns[] = array("data" => "item", "name"=>"Item");
$a = array();
$a['id'] = $row[$k]['id'];
$a['number'] = $row[$k]['number'];
$a['item']= $row[$k]['Item'];
$data[] = $a;
$json_for_datatable = array("columns" => $columns, "data" => $data);
echo json_encode($json_for_datatable);
For configuration or considerations is necessary to receive de responseText with the function JSON.parse(), ¡This is important!
data = JSON.parse(jqxhr.responseText);
For considering in the ajax call
var data,
tableName = '#exampleTablaDinamica'+$(this).attr("data-id"),
columns, str,
jqxhr = $.ajax('../files/data/data.json.php?idPartida='+$(this).attr("data-idpartida")+'&meses='+ selected +'&idPresupuesto='+$('#idPresupuesto').val())
.done(function () {
data = JSON.parse(jqxhr.responseText);
// Iterate each column and print table headers for Datatables
$(tableName + " thead").html("<tr></tr>");
$.each(data.columns, function (k, colObj) {
str = '<th>' + colObj.name + '</th>';
$(str).appendTo(tableName+'>thead>tr');
});
// Add some Render transformations to Columns
// Not a good practice to add any of this in API/ Json side
data.columns[0].render = function (data, type, row) {
return data;
}
data.columns[1].render = function (data, type, row) {
return data;
}
data.columns[2].render = function (data, type, row) {
return "<a href='a.php'>" + data + "</a>";
}
// Debug?
console.log(data.columns[0]);
if ( $.fn.DataTable.isDataTable(tableName) ) {
$(tableName).DataTable().destroy();
}
table = $(tableName).dataTable({
"data": data.data,
"columns": data.columns,
"responsive":true,
"fnInitComplete": function () {
// Event handler to be fired when rendering is complete (Turn off Loading gif for example)
console.log('Datatable rendering complete');
}
});
})
.fail(function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.
Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.
' + jqXHR.responseText;
}
console.log(msg);
});
And finally this is the generated JSON content.
{
"columns": [{
"data": "id",
"name": "Id"
}, {
"data": "number",
"name": "number"
}, {
"data": "item",
"name": "Item"
}, {
"data": "10 2018",
"name": "10 2018"
}, {
"data": "11 2018",
"name": "11 2018"
}, {
"data": "total",
"name": "total"
}],
"data": [{
"id": "1",
"number": "1.1",
"item": "loremipsum loremipsum loremipsum loremipsum ",
"10 2018": "0",
"11 2018": "0",
"total": "20.741"
}]
}