【问题标题】:Datatables colspan header数据表 colspan 标头
【发布时间】:2016-11-15 07:27:38
【问题描述】:

我有这样的桌子

<table id="tbl_orderstatus" class="dataTable table table-bordered table-hover table-full-width nowrap" width="150%" data-table="tblorderstatus">
    <thead>
        <tr>
            <th>Buyer</th>
            <th>Season</th>
            <th>Style#</th>
            <th>KP#</th>
            <th>Item Description</th>
            <th>Buyer PO</th>
            <th colspan="2">Qty</th>
            <th>Material</th>
            <th>Destination</th>
            <th>Delivery Date</th>
        </tr>
    </thead>
</table>

但我的问题是,我只是把&lt;th colspan="2"&gt;Qty&lt;/th&gt; 那是错误的,它说未捕获的风格未定义。我怎么做这样的clospan?因为我需要在数量上放 2 个数据列。

tq 回答,我打赌我必须在标题上使用两行。 tQ伙计们:)

【问题讨论】:

  • 尝试关注this example
  • @GuruprasadRao :对不起,我的语言不好,我使用你给出的例子中的代码。但我的问题是我不需要 mor 标题上的 2 行,我只需要标题上的 1 行。那是我的问题
  • jQuery DataTables 要求正文中的每一列在标题中至少有一列,这就是为什么如果要显示 Qty 的两个数据列,所有答案都包含两行的原因.
  • @Gyrocode.com Oww .. 好吧,所以我不能在一行上做。好的tq然后:D

标签: jquery datatables


【解决方案1】:

这是一个旧线程,但仍然 - 我想分享我在自己的项目中提出的想法。我需要基本相同的东西 - 我想将标题跨越 3 列而不加强它,将它保持在一行中。

您看到了错误,因为 jQuery DataTables 想要为每个字段分配一个单独的列来分配处理程序或其他任何内容。我们可以通过添加另一个标题行来消除错误,该行包含您想要跨越的标题列。就我而言,我添加了 3 个标题列。

现在,错误消失了,但标题看起来很可怕。看起来没有必要,但将所有其他顶行标题列设置为跨越 2 行(行跨度)。 而现在,“魔法”——因为我们的第二行标题列不包含任何信息,只需隐藏它们!

$(document).ready(function() {
    $('#example').DataTable();
});
<script
  src="https://code.jquery.com/jquery-3.2.1.js"
  integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
  crossorigin="anonymous"></script>
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css" rel="stylesheet"/>

<table id="example" class="display" cellspacing="0" width="100%" style="text-align: center;">
    <thead>
        <tr>
            <th rowspan="2">One</th>
            <th colspan="2">Two and Three</th>
            <th rowspan="2">Four</th>
            <th rowspan="2">Five</th>
        </tr>
        <tr style="display:none">
            <th></th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td> First data </td>
            <td> Second data </td>
            <td> Third data </td>
            <td> Fourth data </td>
            <td> Fifth data </td>
        </tr>
    </tbody>
</table>

瞧。这是我能产生的最干净的解决方法,但它会做。

【讨论】:

  • 这可行,但唯一的问题是您将无法获得任何隐藏列的列排序功能。
【解决方案2】:

我测试过,它正在运行。也许你忘了加一个&lt;td&gt;&lt;/td&gt;在这里输入代码

<table id="tbl_orderstatus" class="dataTable table table-bordered table-hover table-full-width nowrap" width="150%" data-table="tblorderstatus">
        <thead>
            <tr>
                <th>Buyer</th>
                <th>Season</th>
                <th>Style#</th>
                <th>KP#</th>
                <th>Item Description</th>
                <th>Buyer PO</th>
                <th colspan="2">Qty</th>
                <th>Material</th>
                <th>Destination</th>
                <th>Delivery Date</th>
            </tr>
        </thead>
        <tr>
            <td>Test</td>
            <td>Test</td>
            <td>Test</td>
            <td>Test</td>
            <td>Test</td>
            <td>Test</td>
            <td>Test 1</td>
            <td>Test 2</td>
            <td>Test</td>
            <td>Test</td>
            <td>Test</td>
        </tr>
   </table>

【讨论】:

    【解决方案3】:

    你需要为每一行加上一个标题,所以你的标题应该是这样的:

                   <thead>
                        <tr>
                            <th rowspan="2">Buyer</th>
                            <th rowspan="2">Season</th>
                            <th rowspan="2">Style#</th>
                            <th rowspan="2">KP#</th>
                            <th rowspan="2">Item Description</th>
                            <th rowspan="2">Buyer PO</th>
                            <th colspan="2">Qty</th>
                            <th rowspan="2">Material</th>
                            <th rowspan="2">Destination</th>
                            <th rowspan="2">Delivery Date</th>
                        </tr>
                        <tr>
                            <th>Qty 1</th>
                            <th>Qty 2</th>
                        </tr>
                    </thead>
    
                </table>
    

    【讨论】:

    • 我的问题是我不需要更多的标题行。我是怎么做到的?? ://
    猜你喜欢
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多