Ajax - cannot render the data on the table
Ajax - cannot render the data on the table
Hi,
I am evaluating the library. I facing this issue when retrieving the data from Ajax call:
Ajax:
{"total_rows":1,"offset":0,"rows":[
{"id":"CR27527H","key":"Ammar","value":{"ID":"CR27527H","Insurance":"Axa","Type":"Lexus","Model":2007,"Plate No":27527,"LPO Received":"No","LPO Amount":1000,"accdnt No":"N/A","Last update":"2017-04-28","Car Status":"Delivered"}}
]}
HTML:
id |
---|
JAVASCRIPT:
$('#cars_list').DataTable( {
"ajax": "http://127.0.0.1:5984/carage/_design/cars/_view/cars_summary",
"columns": [
{ "data": "id" }
],
"searching": true,
"paging": true,
"ordering": true,
"info": true
} );
});
When i run the page i get the error in Chrome Console:
Uncaught TypeError: Cannot read property 'length' of undefined
at jquery.dataTables.min.js:48
at i (jquery.dataTables.min.js:35)
at Object.success (jquery.dataTables.min.js:35)
at j (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at x (jquery.min.js:5)
at XMLHttpRequest.b (jquery.min.js:5)
What am I missing here?
Thank you
albostami
Replies
You need to use
ajax.dataSrc
to tell DataTables to look for the data in yourrows
property rather than in its default ofdata
.Allan
The JSON is not in a format that Datatables expects. Info regarding data format is here:
https://datatables.net/manual/data/
Looks like you have only one column defined but returning more data. You can do that.
Your data is in an object
rows
. I believe you will need to useajax.dataSrc
to change from the default ofdata
torows
. The first example shows this.Kevin