add row not working for ajax sourced data
add row not working for ajax sourced data

Hello,
below code is not adding a new row into datatable. It returns warning ("requested unknown parameter "Title" ...) and adds an empty row which suggests data is null.
Here is the code
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
First Name | Last Name | Area |
---|---|---|
First Name | Last Name | Area |
$.ajax({
url: "/teams/ops/pidb/_api/web/lists/getbytitle('mytest')/items?$filter=(Modified gt '"+datestring+"')",
type: "GET",
dataType: 'json',
headers: { "Accept": "application/json; odata=verbose" },
success: mySuccessHandler1,
error: myErrorHandler1
});
function mySuccessHandler1(data) {
if(data.d.results.length > 0) {
$.each(data.d.results,function(index,item){
tablex.row.add([{"Title":item.Title,"LastName":item.LastName,"Area":item.Area}]).draw(false);
});
}
//////////////////////////////////////////////////////////////
debuggger shows data.d.result showing correct value returned in item.Title and others.
Just to add missing information.. I am initializing data table as below
function mySuccessHandler(data) {
tablex = $('#patrol').DataTable({
"aaData": data.d.results,
"aoColumns": [{
"mData": "Title"
},{
"mData": "LastName"
}, {
"mData": "Area"
}],
"aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
"select": true,
"colReorder": true,
"order": [[1, 'asc']]
}); // DataTable() closing..
This question has an accepted answers - jump to answer
Answers
Just to add missing information.. I am initializing data table as below
function mySuccessHandler(data) {
tablex = $('#patrol').DataTable({
"aaData": data.d.results,
"aoColumns": [{
"mData": "Title"
},{
"mData": "LastName"
}, {
"mData": "Area"
}],
"aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
"select": true,
"colReorder": true,
"order": [[1, 'asc']]
}); // DataTable() closing..
try:
i.e. remove the array. You are only adding one item at a time there. Use
rows.add()
if you want to add an array of rows.Allan