DataTables 1.10.6 ajax,为第 0 行请求了未知参数 0

DataTables 1.10.6 with ajax, requested unknown parameter 0 for row 0

我刚开始使用 DataTables,在我想切换到服务器端处理之前,它一直运行良好。

感觉我开始接近它的工作了,它现在给我一个错误并显示正确数量的行但没有任何数据。

所以我得到错误:

DataTables warning: table id=dataTables-outputTest - Requested unknown parameter '0' for row 0. Fore more information about this error, please see https://www.datatables.net/manual/tech-notes/4

这是我的js:

$(document).ready( function () {
    $('#dataTables-outputTest').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "/TestData/data-source"
    });
});

这是 JSON 我要放入 table:

{
    "data": [
        {
            "cycle_p": 628320,
            "designation": "C1",
            "gear": "R660",
            "cycle_k": 204000,
            "reportnr": "NA05006"
        }
    ],
    "draw": "1",
    "recordsFiltered": 1,
    "recordsTotal": 1
}

不知道这是否重要,但我使用的是 Django,输出是来自不同模型的属性的混合。

这里是相关的 HTML:

<div class="dataTable_wrapper">
    <table class="table table-bordered table-hover" id="dataTables-outputTest">
        <thead>
        <tr>
            <th>Report</th>
            <th>Test</th>
            <th>Gear</th>
            <th>Cycle K</th>
            <th>Cycle P</th>
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>

非常感谢您的帮助,因为数据库必须能够在不减慢一切的情况下增长很多。

您正在使用 Objects in your data, therefore you need to match object properties to table columns using columns.data。请参阅以下示例:

$('#dataTables-outputTest').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "/TestData/data-source",
    "columns": [
       { "data": "cycle_p" },
       { "data": "designation" },
       { "data": "gear" },
       { "data": "cycle_k" },
       { "data": "reportnr" }
    ]
});

您需要指定哪些数据对应哪些列:

$('#dataTables-outputTest').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "/TestData/data-source",
    columns : [
       { data : "cycle_p" },
       { data : "destination" },
       { data : "gear" },
       { data : "cycle_k" },
       { data : "reportnr" }
    ]
});

这将按照列中声明的顺序生成带有 <td> 的行。如果您有其他布局,和/或另外 <td> 不应该从 JSON 接收数据,您可以使用 columnDefs 代替:

columnDefs : [
   { data : "cycle_p", targets : 0 },
   { data : "destination", targets : 8  },
   { data : "gear", targets : 3  },
   { data : "cycle_k", targets : 2  },
   { data : "reportnr", targets : 1  }
]

其中目标是列索引。