0

I want to build a datatable from the json data I am getting on the server

    $(document).ready(function() {
        $('#example').DataTable( {

        "ajax": "/analyze/List",
        "columns": [
        { responsedata: "Name" },
        { responsedata: "Total" },
        { responsedata: "Passed" },
        { responsedata: "Failed" }]  

        } );
    } );

Didn't work.Is that not how it's supposed to be done ? .

Here's the json data format on server-

{"responseCode":0,"responseData":[{"Name":"Rocky","Total":39,"Passed":35,"Failed":4}]}

Also, I'm an error Uncaught TypeError: Cannot read property 'length' of undefined. Could someone help ? I'm a noob in this.

2
  • Check out the "Ajax" tab on the example, I think your JSON needs to be formatted differently: datatables.net/examples/data_sources/ajax.html Commented Aug 12, 2016 at 14:50
  • yeah, looks like i'll have to change the format and try . Commented Aug 12, 2016 at 14:56

1 Answer 1

3

Yu are doing it a little bit backwards. Use the dataSrc attribute to instruct dataTables that the rows is hold by the responseData property, and refer to each field via the data attribute, not responseData :

$('#example').DataTable({
    ajax: {
        url: '/analyze/List',
        dataSrc: 'responseData'
    },
    columns: [
      { data: "Name" }, 
      { data: "Total" }, 
      { data: "Passed" },
      { data: "Failed" }
    ]
})

demo -> http://jsfiddle.net/2qycjwaz/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.