Ajax - cannot render the data on the table

Ajax - cannot render the data on the table

albostamialbostami Posts: 2Questions: 0Answers: 0

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

  • allanallan Posts: 63,889Questions: 1Answers: 10,530 Site admin

    You need to use ajax.dataSrc to tell DataTables to look for the data in your rows property rather than in its default of data.

    Allan

  • kthorngrenkthorngren Posts: 21,591Questions: 26Answers: 5,004

    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 use ajax.dataSrc to change from the default of data to rows. The first example shows this.

    Kevin

This discussion has been closed.