jQuery datatable从ajax获取json数据 [英] jquery datatable get json data from ajax

查看:1468
本文介绍了jQuery datatable从ajax获取json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从服务器上获取的json数据构建数据表

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 ? .

这是服务器上的json数据格式-

Here's the json data format on server-

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

我也是一个错误Uncaught TypeError:无法读取未定义的属性'length'.有人可以帮忙吗?我是这个菜鸟.

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

推荐答案

Yu有点后退.使用dataSrc属性指示dataTables该行由responseData属性保留,并通过data属性而不是responseData来引用每个字段:

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" }
    ]
})

演示-> http://jsfiddle.net/2qycjwaz/

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

这篇关于jQuery datatable从ajax获取json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆