在Jquery数据表中显示JSON数据 [英] Show JSON Data in Jquery Datatables

查看:66
本文介绍了在Jquery数据表中显示JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在jQuery DataTables组件中获取我的JSON数据。

I've been trying to get my JSON data in jQuery DataTables component.

首先我编写了JavaScript代码以及一个视图,如下面的代码所示:

First I wrote the JavaScript code as well as a view as shown in the code below:

$(document).ready(function () {
    $('#myData').DataTable({
        lengthChange: false,
        ajax: {
            url: "http://amp-local/api/wipbin/FetchChild/",
            // dataSrc: 'allk'
        },
        columns: [
            { data: "name" },
            { data: "numbers" }
        ],
        select: true
    });
});

我的观点:

<table id="myData" class="table table-striped table-bordered" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Numbers</th>
        </tr>
    </thead>
</table>

我的JSON:

[{
    "numbers": "38",
    "name": "Bllaca"
}, {
    "numbers": "28",
    "name": "Kaess"
}, {
    "numbers": "27",
    "name": "droessmer"
}, {
    "numbers": "24",
    "name": "friedricha"
}]

结果出现此错误:

TypeError: undefined is not an object (evaluating 'f.length')

我的桌子是空的。

推荐答案

当对AJAX请求使用服务器端处理时,它希望在 DataTables要求的特殊格式。需要以下JSON响应对象字段: draw data recordsTotal recordsFiltered 。您需要使服务器响应符合预期格式

例如,您的第一个回复应如下:

When using server-side processing with AJAX requests, it expects JSON data to be returned in the special format required by DataTables. The following fields of JSON response object are required: draw, data, recordsTotal and recordsFiltered. You need to bring your server response into accordance with expected format.
For example, your first response should be as following:

{
  "draw": 1,
  "recordsTotal": 4,
  "recordsFiltered": 4,
  "data": [
    {
      "numbers": "38",
      "name": "Bllaca"
    }, {
      "numbers": "28",
      "name": "Kaess"
    }, {
      "numbers": "27",
      "name": "droessmer"
    }, {
      "numbers": "24",
      "name": "friedricha"
    }
  ]
}

这篇关于在Jquery数据表中显示JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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