【问题标题】:datatables warning with valid data带有有效数据的数据表警告
【发布时间】:2018-10-05 10:58:16
【问题描述】:

我正在尝试使用以下代码初始化数据表对象:

$(document).on('click', '.queued_auto_responders', function (event) {
  // queued_ids from the clicked row
  var queued_ids = $(this).attr('data-queued-ids').split(',');

  // Set the respective controls' values
  $.get("assets/php/get_queued_responders.php", {queued_ids: queued_ids})
    .done(function(n) {
      var table_queued_responders = $('#queued_auto_responders').DataTable({
        data: n,
        columns: [
          { data: 'send_date', title: 'Send Date'},
          { data: 'title', title: 'Title' },
          { data: 'markup', title: 'Action', createdCell:
            function (td, cellData, rowData, row, col) {
              $(td).html(cellData);
            }
          }
        ]
      });
    })
    .fail(function(n) {
      console.log(n.responseText);
    });
});

并且返回的数据是由 Network > XHR > [file] > Response 验证的以下 JSON:

[
  {
    "send_date": "2019-01-17",
    "title": "...",
    "markup": "<a type=\"button\" class=\"d-block text-center text-danger\" href=\"assets/php/stop_queued_responder.php?queue_id=71\">Stop from Sending</a>"
  },
  {
    "send_date": "2018-06-01",
    "title": "...",
    "markup": "<a type=\"button\" class=\"d-block text-center text-danger\" href=\"assets/php/stop_queued_responder.php?queue_id=72\">Stop from Sending</a>"
  },
  {
    "send_date": "2018-06-11",
    "title": "...",
    "markup": "<a type=\"button\" class=\"d-block text-center text-danger\" href=\"assets/php/stop_queued_responder.php?queue_id=73\">Stop from Sending</a>"
  }
]

返回的错误如下:

DataTables 警告:表 id=queued_auto_responders - 请求第 0 行第 0 列的未知参数“send_date”。有关此错误的详细信息,请参阅http://datatables.net/tn/4

每当我关注错误消息中的 URL 时,它都会指向 Parameter is a String 类别,该类别指出错误通常是因为:

  • 指定的数据属性不存在(数据中有错字或空白)
  • 指定的属性值为空

但是从返回的数据可以看出,我的数据确实满足这两个条件中的任何一个。我还验证了返回的数据是否与 Object 类别的数据格式匹配。

那么为什么我会收到这个错误?

更新

这是我正在尝试初始化的表的标记:

<table class="table" id="queued_auto_responders">
  <thead>
  </thead>
  <tbody>
  </tbody>
</table>

我正在使用 DataTables 1.10.16。

【问题讨论】:

  • 请添加表格的 html 标记,即 ID 为 queued_auto_responders 的元素。另外,您使用的是什么版本的 DataTables 插件?
  • @D.Smania - 看看我的更新。
  • 我将首先尝试一个更基本的示例、一列和更多调试。我会用这段代码给出答案,这样我们就可以得到更多关于发生了什么的信息。

标签: jquery datatables


【解决方案1】:

尝试下一个更改(降级为更基本的示例):

HTML 标记

<table class="table" id="queued_auto_responders">
    <thead>
    <tr>
        <th>Send Date</th>
    </tr>
    </thead>
</table>

JAVASCRIPT

$(document).ready(function()
{
    $(".queued_auto_responders").on('click', function(event)
    {
        // queued_ids from the clicked row
        var queued_ids = $(this).attr('data-queued-ids').split(',');

        // Set the respective controls' values.
        $.getJSON("assets/php/get_queued_responders.php", {queued_ids: queued_ids})
        .done(function(n)
        {
            console.log("Received data: ");
            console.log("Received type of data: " + typeof(n));
            console.log(n);

            var table_queued_responders = $('#queued_auto_responders').DataTable({
                data: n,
                columns: [
                    {data: 'send_date'}
                ]
            });
        })
        .fail(function(n)
        {
            console.log(n.responseText);
        });
    });
});

【讨论】:

  • 我收到了同样的警告,我的控制台中的文本是“收到的数据”,后面是我在我的 OP 中发布的 JSON。
  • 好的,将dataType: "json" 添加到$.get() 并在php 端返回如下响应:echo json_encode($response);。我将更新我的代码以进行第一次改进。
  • 我将方法从$.get 更改为$.getJSON,这次它没有产生警告。这次控制台记录了一个数组而不是一个字符串。但结果没有显示在 HTML 中。
  • 这是第一次修复,结果必须是 JSON 对象,而不是字符串。现在我们必须看看为什么表格没有填满......
  • 表格没有填满是我的错,我有一些 CSS 隐藏了持有表格的 &lt;div&gt;。所以问题在于$.get 返回一个字符串而不是 JSON 的结果。非常感谢您的帮助。
猜你喜欢
  • 2020-09-26
  • 2015-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多