DataTables 1.10 - Not Showing Results

2019-08-07 04:11发布

问题:

This is my setup.

Javascript/jQuery:

$('#list').dataTable({
    paging: false,
    serverSide: true,
    ajax: {
        url: "/search/",
        data: function (d) {
            return $.extend({}, d, {
                lid: Label.selectedId
            });
        }
    }
});

HTML

<table id="list" class="table table-striped" width="100%">
    <thead>
    <tr>
        <th>Title</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>URL</th>
    </tr>
    </thead>
    <tbody>

    </tbody>
</table>

JSON Response

{"data": [["Test", "", "", ""]], "recordsTotal": 1, "draw": 1, "recordsFiltered": 1}

It is getting called manually by doing this:

$('#list').DataTable().ajax.reload();

But the table does not change. I've done this many times with DataTables 1.9 without a hitch -- any ideas?


UPDATE

I discovered if I turn off serverSide, it works.

回答1:

I found the problem. It was with the draw in the JSON response. It was always 1.

If you use serverSide: true, that means it is going to send draw as a variable, and it must be returned.

More information here: http://datatables.net/manual/server-side