Can't add lines to my table despite the good formatting Json
Can't add lines to my table despite the good formatting Json
Hello
I want to fill a table (DataTables) with an AJax request
My request ajax returns Json but impossible to fill the table, I have this error:
DataTables warning: table id=dt-table - Requested unknown parameter '0' for row 1, column 0. For more information about this error, please see http://datatables.net/tn/4
My table:
<div>
<div class="table-responsive">
<table id="dt-table" class="table table-striped table-bordered">
<thead>
<tr>
<th>Invoice</th>
</tr>
</thead>
<tbody>
<tr>
<td>
</td>
</tr>
</tbody>
</table>
</div>
</div>
My JS:
var table = $('#dt-table').DataTable({});
$(document).on("click", ".button", function() {
var inputVal = $(this).text();
var selectVal = $('#select option:selected').val();
$.post("/ajax/invoice.php", {find: inputVal, column: selectVal}).done(function(data){
console.log(data)
table.rows.add(data).draw();
});
});
The ajax response:
[{"invoice":"AZERT"},{"invoice":"JFKDH"},{"invoice":"DKHFVEP"}]
My request:
$column = $_POST['column'];
$request = $bdd->prepare("SELECT invoice from table_master WHERE $column = :find");
$request->execute(array(
':find' => $_POST['find']
));
$result = $request->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result);
I do not see where the problem comes from, I look at the DataTables documentation and my json is normally properly formatted
https://datatables.net/reference/api/rows.add()
Thanks for your help
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
You need to use
columns.datato tell DataTables to get the information from theinvoiceproperty for the column. See this part of the manual for details.Allan
I added :
var table = $('#dt-table').DataTable({
columns: [
{ data: 'invoice' }
]
});
But I still have this error :
DataTables warning: table id=packing-table - Requested unknown parameter 'invoice' for row 1, column 0. For more information about this error, please see http://datatables.net/tn/4
Thank you for your help
I suspect the problem is that in your ajax
donefunctiondatais a JSON string that you need to parse into Javascript data. Something like this:Kevin
@kthorngren Thank you it's work !
So if I understand correctly, the ajax request did not understand that the return was json and not text?
The returned data is JSON which is a string.
JSON.parseconverts the string into Javascript data which in this case is an array of objects.Kevin