【问题标题】:Datatables warning - ''requested unknown parameter FirstName for row x"数据表警告-“为行 x 请求未知参数 FirstName”
【发布时间】:2015-03-16 16:02:07
【问题描述】:

我的数据表代码如下

 <table id="users" class="table table-striped table-bordered">
        <thead>
               <tr>
                     <th>FirstName</th>
                     <th>LastName</th>
                     <th>Name</th>
                      <th>Username</th>
                       <th>Password</th>
                       <th>Email</th>
                      <th>Role</th>
                 </tr>
         <thead>
         <tbody>

         </tbody>
</table>
```

对应的javascript如下

 userTable = $("#users").DataTable({
    serverSide: true,
    ajax: {
        url: "scripts/getusers.php",
        type: "POST",
        dataType: "json"
    },
    "columns": [
        {data: "FirstName", "orderable": true,"searchable":true,visible:false},
        {data: "LastName", "orderable": true,"searchable":true,visible:false},
        {data : null,
            "render":function(data, type, row, meta ){
            return row['FirstName']+','+row['LastName'];
        }},
        {data: "Username", "orderable": true,"searchable":true},
        {data: "Password", "orderable": true,"searchable":true,
        "render":function(data, type, row, meta ){
            return '********';
        }},
        {data: "Email", "orderable": true,"searchable":true},
        {data: "Role", "orderable": true,"searchable":true,
        "render":function(data, type, row, meta ){
            return data == 1?'Root':(data == 2?'Admin':'User');
        }}
    ],
    "language": {
        "emptyTable": "Seems no users were registered yet"
    },
    dom: 'T<"clear">lfrtip'
});

当我尝试如下添加一行时,虽然该行添加成功,但它显示警告

userTable.row.add([data.FirstName,data.LastName,data.Username,data.Password],data.Email,data.Role).draw();

'请求第 10 行的未知参数 FirstName'(我的默认长度是 10)

为了更新一行,我选择了一行here,然后调用以下命令

userTable.row('.selected').data([data.FirstName,data.LastName,data.Username,data.Password],data.Email,data.Role).draw();

上面出现了同样的错误。它在说

'requested unknown parameter FirstName for row x'(其中 x 是我正在更新的行号)

在这两种情况下,行都已成功添加和更新。但我收到了警告。

我猜我为第三列写的渲染函数有问题。

也用于更新(如上所示),我正在调用 draw 方法,该方法正在删除选定的类,这意味着我不能多次调用上述更新语句。有没有其他选择。实际上上面的更新语句是在模态对话框中调用的。因此,必须允许同一行多次更新,如果调用 draw() 来清除“选定”类,这是不可能的。有什么解决办法吗?

【问题讨论】:

    标签: jquery jquery-datatables


    【解决方案1】:

    我得到了答案。当控件开始渲染列时,它期待对象并且我正在提供一个数组,并且当我更新行而不是传递数组时,我必须传递整个对象。在添加过程中,由于我的代码中的错误,“数据”对象没有名为 FirstName 的属性。这就是它发出警告的原因。但是表格正在更新,因为我正在调用重新加载整个表格的 draw() 方法。

    【讨论】:

      猜你喜欢
      • 2015-10-09
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 2021-10-24
      • 2015-08-03
      • 2014-09-07
      • 2018-02-26
      相关资源
      最近更新 更多