简体   繁体   中英

Show JSON Data in Jquery Datatables

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

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
    });
});

My view:

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

My JSON:

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

Result is with this error:

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

And my table is empty.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM