简体   繁体   English

数据表空表错误

[英]datatables empty table error

I have a piece of code where I use the datatables plugin. 我在使用datatables插件的地方有一段代码。

The table structure looks like this: 表结构如下所示:

<thead>
    <tr>
        <th>col1</th>
        <th>col2</th>
        <th>col3</th>
        <th>col4</th>
        <th>col5</th>
        <th>col6</th>
        <th>col7</th>
        <th>col8</th>
    </tr>
</thead>

but notice that in the initialization code I am hiding the last four columns. 但是请注意,在初始化代码中,我隐藏了最后四列。

In my code, I do the following: 在我的代码中,我执行以下操作:

if($data->responseCode < 400)
{
    echo HTML Table
}

that works 这样可行

else
{
    echo '<tr><td colspan=4><h1 class="error">'.$data->errorMsg.'</h1></td></tr>';
}

When I do this, I get the following error: 当我这样做时,出现以下错误:

"DataTables warning (table id = 'DataTables_Table_0'): Requested unknown parameter '1' from the data source for row 0" “ DataTables警告(表ID ='DataTables_Table_0'):从数据源请求的第0行的未知参数'1'”

here is the dataables code: 这是dataables代码:

var oTable = $('.table1').DataTable({
    "iDisplayLength": 100,
    "aaSortingFixed": [[0,'desc']],
    "aaSorting": [ [0,'asc'] ],
    "aoColumnDefs": [
          { "bVisible": false, "aTargets": [ 4,5,6,7 ] },
          { "bSearchable": false, "aTargets": [ 1,2,3,4,5,6,7] },
          { "bSearchable": true, "aTargets": [ 0 ] },
        ]
});

Does anybody know what would be causing that? 有人知道会导致什么吗? I thought maybe it was the colspan but when I remove that, the problem still exists. 我以为可能是colspan,但是当我删除它时,问题仍然存在。 The weird thing is that I use this on another page and it seems to handle an empty result set just fine. 奇怪的是,我在另一页上使用了它,似乎可以处理空结果集。

Not sure if this helps but after playing a little I managed to get the following error to show up in firebug: 不确定这是否有帮助,但是玩了一点之后,我设法在firebug中显示以下错误:

"TypeError: nTd is undefined" “ TypeError:nTd未定义”

If you use datatables often, then this is the common error that you will get. 如果您经常使用数据表,那么这是您会遇到的常见错误。 The reason is that the data format that you use to create the data table can't match the table columns, for example you have 4 columns while the source data just can provides 3 columns. 原因是用于创建数据表的数据格式与表列不匹配,例如,您有4列,而源数据只能提供3列。 You can post the data format and the table structure, let me check the further issue. 您可以发布数据格式和表结构,让我检查进一步的问题。

Your else statement do not include table HTML start tag, maybe this is the problem, you echo out only a row. 您的else语句不包含table HTML开始标记,也许这是问题所在,您仅回显一行。 Try 尝试

else
{
    echo '<table id="table_id" class="table1"><tr><td colspan=4><h1 class="error">'.$data->errorMsg.'</h1></td></tr></table>';
}

It's because dataTable check the matched total of "column" in header & body table 这是因为dataTable检查标题和正文表中“ column”的匹配总数

else
    {
        echo '<tr><td>'.$data->errorMsg.'</td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              </tr>';
    }

It will shows like empty row and with errorMsg in the first column of your row. 它将显示为空行,并在该行的第一列中显示errorMsg。

您可以使用小的逻辑来解决此问题,检查空记录,并为每个数据表动态设置表类。

<table class="table table-striped table-bordered <?php echo !empty($rows) ? 'data-table-display-class' : ''; ?>">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM