【问题标题】:Datatables Requested unknown parameter '0' for row 0, column 0数据表为第 0 行第 0 列请求未知参数“0”
【发布时间】:2019-01-28 12:24:08
【问题描述】:

尝试使用 ajax 填充数据表时出现以下错误:DataTables 警告:表 id=employeeTable - 请求第 0 行第 0 列的未知参数“0”。有关此错误的更多信息,请参阅@987654321 @。 该表实际上具有正确的行数,但所有行都是空的。输出行如下所示:

<tr role="row" class="odd"><td class="sorting_1"></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr role="row" class="even"><td class="sorting_1"></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>

这个问题有很多答案,但似乎没有一个能解决我的问题。我使用的代码是:

function fillEmployeeTable(store_nr,week_limit) {
  $('#employeeTable').DataTable( {
    "ajax": {
      "method": "GET",
      "url": "api/employeetable/data",
      "data": {
        "store_nr": store_nr,
        "week_limit": week_limit,
      },
      "columns": [
              { "data": "function_name" },
              { "data": "register_id" },
              { "data": "age" },
              { "data": "contract_until" },
              { "data": "worked_hours" },
              { "data": "days" },
              { "data": "costs" },
              { "data": "hourly_rate" },
              { "data": "contract_from" }
          ]
    }
  });
}

使用以下 HTML

<table class="table" id="employeeTable">
<thead>
<tr>
<th>function_name</th>
<th>register_id</th>
<th>age</th>
<th>contract_until</th>
<th >worked_hours</th>
<th>days</th>
<th>costs</th>
<th>hourly_rate</th>
<th>contract_from</th>
</tr>
</thead>
</table>
</div>

(document).ready(function() {
  fillEmployeeTable({{default}})
});

而返回的JSON数据为:

{"data": [{"function_name": "Y", "register_id": "1", "age": 26, "contract_from": "01-07-18", "contract_until": "31-12-99", "worked_hours": 1, "days": 9, "costs": 7, "hourly_rate": 2}, {"function_name": "X", "register_id": "1", "age": 18, "contract_from": "24-01-18", "contract_until": "31-07-18", "worked_hours": 486.25, "days": 76, "costs": 1, "hourly_rate": 2}]}

有人知道我该如何解决这个问题吗?

【问题讨论】:

  • 我认为您缺少表格的主体
  • 在我看到的所有示例中,当您检查页面源时,您也没有在 HTML 中添加 tbody,您会看到 tbody 是由 javascript 代码添加的。

标签: jquery datatables


【解决方案1】:

所以我找到了答案。问题在于 ajax 数据的格式化方式。尽管它是完美的 JSON,但每一行都应该以方括号而不是花括号开头。所以它适用于 { "data": [ [row1], [row2] ] }。

【讨论】:

  • 我在我的项目中将它与您以前的 JSON 格式一起使用。尝试在 ajax 选项中添加"dataSrc": "data",以便 DataTables 知道数据是对象数组而不是数组。像这样:ajax:{url:...,dataSrc:'data'}
  • 您的 OP 代码中的问题是您没有在列之前正确关闭 ajax 部分。
  • 我尝试了您的两个建议,但在我的情况下都不起作用。
【解决方案2】:

在您的 html 代码中尝试此更改...

<table class="table" id="employeeTable">
<thead>
<tr>
<th>function_name</th>
<th>register_id</th>
<th>age</th>
<th>contract_until</th>
<th >worked_hours</th>
<th>days</th>
<th>costs</th>
<th>hourly_rate</th>
<th>contract_from</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>

(document).ready(function() {
  fillEmployeeTable({{default}})
});

【讨论】:

  • 不幸的是,这并没有什么不同。这是有道理的,因为运行 JS 代码已经添加了 tbody 标签。
猜你喜欢
  • 1970-01-01
  • 2014-09-16
  • 1970-01-01
  • 2015-12-15
  • 2016-09-24
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
相关资源
最近更新 更多