jquery datatable get json data from ajax

限于喜欢 提交于 2019-12-13 07:28:32

问题


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.


回答1:


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/



来源:https://stackoverflow.com/questions/38920678/jquery-datatable-get-json-data-from-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!