Reputation: 2119
Im creating a DataTable from an Ajax json.
resultTable = $('#changeTable').DataTable({
"order": [[0, "desc"]],
"pageLength": 50,
"scrollX": true,
"lengthMenu":[[50,100,250, -1], [50, 100, 250, "All"]],
"dom":'<"toolbar">ltipr', //write ltfipr to show a search bar
"ajax":{
url:"api/changes",
"dataType":"json",
timeout:15000
}
});
The DataTables creates but it shows an error:
DataTables warning: table id=changeTable - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
My JSON Looks like the following
{"data":
[
{"id":1,
"createdDate":"Apr 18, 2018 4:10:58 PM",
"source":"manual upload",
"emailId":"manual upload",
"attachmentId":"manual upload",
...,},
{next objet}]}
Such JSON object is created in my Java controller:
@RequestMapping(value = "/api/changes", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public String getChanges(){
Optional<List<PriceChange>> priceChangeList = pcService.findAllPriceChanges();
JsonObject result = new JsonObject();
if (priceChangeList.isPresent()) {
result.add("data", new Gson().toJsonTree(priceChangeList.get()));
return result.toString();
}
return null;
}
I don know how to use this information with the dataSrc
property to make it work for the DataTable. Any Ideas?
Upvotes: 1
Views: 917
Reputation: 85528
You just need to define columns
for the table. If you have
<table id="changeTable"></table>
add this to your DataTables options :
resultTable = $('#changeTable').DataTable({
...,
columns: [
{ data: 'id', title: 'id' },
{ data: 'createdDate', title: 'createdDate' },
{ data: 'source', title: 'source' },
{ data: 'emailId', title: 'emailId' },
{ data: 'attachmentId', title: 'attachmentId' }
]
})
If ypu have specifed a <thead>
section you can skip the title
's.
Upvotes: 1