Problem with insert a new row
Problem with insert a new row
Hi.
I have a problem when I want to insert data after an ajax call with the JSON of my project.
The definition of my table is:
<table class="table table-bordered m-table" id="tableExample">
<thead class="columns titulars-sms">
<tr>
<th class="text-center">Name</th>
<th class="text-center">Created</th>
</tr>
</thead>
</table>
var tableExample = $('#tableExample').DataTable();
$.ajax({
url: "{{url('/')}}/getusers",
method: "GET",
xhrFields: {
withCredentials: true
},
data: {"CreationDate": Date},
success: function (data,status,xhr) {
$.each(data, function(index,value){
tableExample.row.add({
"Name": value.name,
"Created": value.created
}).draw();
});
},
});
And the error that I get in my screen is:
"DataTables warning: table id=tableExample- Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4"
I debug with Chrome console and data is an array and have the attr "name" and "created".
So, I don't know where could be the error
If you need some extra info, tell me.
This question has an accepted answers - jump to answer
Answers
I'm assuming this is your Datatables initialization:
You haven't defined
columns.data
which is needed when adding objects like this:Without defining
columns.data
Datatables expects the data to be an array for each row instead of the object you are adding. That is basically what the error link http://datatables.net/tn/4 is trying to say.Looks like you
data
in thesuccess
function is an array of objects. If so you can just usetableExample.rows.add( data ).draw();
. Notice the pluralrows
. However if you choose to loop through the data and userow.add()
remove the chained.draw()
and usetableExample.draw();
after the each loop. It will be more efficient to executedraw()
once rather than for each row added.Kevin
Yes Kevin, the success is an array of object, look the image.
But the error continues
Looks like the objects and the array have the keys
fecha
andtotal
. But you are using something differentvalue.name
andvalue.created
:What did you change? If you added
columns.data
you need to usefecha
andtotal
, like this:Kevin
Oh yeah, now it works Kevin.
Yeah, it was a mistake in the data names... Sorry and thanks!
Regards!