【问题标题】:DataTable doesn't work if columns don't match如果列不匹配,DataTable 将不起作用
【发布时间】:2017-11-30 10:03:12
【问题描述】:

我有一个表格,其中一行显示,另一行隐藏,但在点击时显示。

<table cellspacing="0" id="datatable" class="table table-responsive table-striped table-bordered" style="width: 1300px; margin-bottom: 50px">
    <thead>
        <tr>
            <th>#</th>
            <th>1</th>
            <th>2</th>
            <th>3</th>
            <th>4</th>
            <th>5</th>
            <th>6</th>
            <th>7</th>
            <th>8</th>
            <th>9</th>
        </tr>
     </thead>
     <tbody>
         <tr>
           <td>#</td>
           <td>1</td>
           <td>2</td>
           <td>3</td>
           <td>4</td>
           <td>5</td>
           <td>6</td>
           <td>8</td>
           <td>9</td>
       </tr>
       <tr>
           <td colspan="9" id="break_{{$t->BreakID}}" style="display: none;"></td>
       </tr>
            </tbody>
        </table>

如果我删除此行,DataTable 功能将起作用:

<tr>
    <td colspan="9" id="break_{{$t->BreakID}}" style="display: none;"></td>
</tr>

或者如果我添加 8 个 td 来匹配上一行的 th,但它看起来很难看。我需要一个 td,它是colspan = 9。我还尝试添加 8 个具有隐藏显示样式的 td。也没有成功。

否则不起作用并产生错误:

Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined

有什么建议吗?

【问题讨论】:

    标签: javascript jquery html laravel datatables


    【解决方案1】:

    我不知道这是否会影响,但第一行有 10 个单元格,第二行和第三行有 9 个单元格。

    【讨论】:

    • 啊,如果我正确阅读问题,这正是他想要的:p
    • 我以为他想要 10、10 和 10,但跨越了。此外, display:none 应该放在行上。
    • 就像我在上一个问题的评论中所说的那样。它可以工作,但前 20 页是空的,然后结果就出来了。
    【解决方案2】:

    每行中的列数必须相同,包括标题。 如果最后一行不应该覆盖所有列。您应该尝试使用 CSS 隐藏不需要的 &lt;td&gt;s。

    除非存在插件,否则我认为 DataTables 无法实现您想要的。在他们的手册中,他们确实有一些关于“行分组”的内容,但我猜他们的意思是“单元分组”而不是Row grouping?无论如何,在示例中,td 的数量与表的其余部分相匹配。

    【讨论】:

    • 我做到了.. 我尝试用&lt;td style="display:none;"&gt;&lt;/td&gt; 添加 8 个 tds 但它不起作用。
    • 你能用一张图片编辑你的问题吗?它在隐藏单元格中的样子?这可能是因为表格或父行仍然应用了样式。
    • 它可以工作,但前 20 页是空的......它写的是Showing 181 to 190 of 398 entries
    • 编辑了我的答案,但认为这不是简单的方法
    【解决方案3】:

    数据表需要您定义的所有字段,在加载或获取数据后,您可以使用数据表的功能隐藏特定列

    在js文件中这样

    usercheck 是我的用户角色检查变量。

    if (usercheck == 3)
    {
        data_table.column(6).visible(false);//data_table is variable of data-table and here is the code to hide column in data-table
    }
    

    您必须根据条件隐藏特定的列。

    【讨论】:

      猜你喜欢
      • 2013-04-24
      • 2022-07-11
      • 2016-04-22
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      相关资源
      最近更新 更多