【问题标题】:Data Table header not aligned in bootstrap tab数据表标题未在引导选项卡中对齐
【发布时间】:2015-11-02 12:22:57
【问题描述】:

我正在尝试将 DataTable 与 Twitter 引导选项卡一起使用,为此我正在尝试以下代码,它成功调用 dataTable 并创建了 dataTable,但对于 tab2 DataTable 标头未对齐

 <div>
    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Data table 1</a>

        </li>
        <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Data table 2</a>

    </ul>
    <!-- Tab panes -->
    <div class="tab-content">
        <div role="tabpanel" class="tab-pane active" id="home">
            <table class="table" id="table1">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>First table</th>
                        <th>First table</th>
                        <th>First table</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <th scope="row">1</th>
                        <td>Mark</td>
                        <td>Otto</td>
                        <td>@mdo</td>
                    </tr>
                    <tr>
                        <th scope="row">2</th>
                        <td>Jacob</td>
                        <td>Thornton</td>
                        <td>@fat</td>
                    </tr>

                </tbody>
            </table>
        </div>
        <div role="tabpanel" class="tab-pane" id="profile">
            <table class="table" id="table2">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>second table</th>
                        <th>second table</th>
                        <th>second table</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <th scope="row">1</th>
                        <td>Mark</td>
                        <td>Otto</td>
                        <td>@mdo</td>
                    </tr>
                    <tr>
                        <th scope="row">2</th>
                        <td>Jacob</td>
                        <td>Thornton</td>
                        <td>@fat</td>
                    </tr>

                </tbody>
            </table>
        </div>
    </div>
</div>

当我添加 scrollY 时,标题会不对齐

【问题讨论】:

    标签: javascript jquery twitter-bootstrap datatable


    【解决方案1】:

    这是因为当您初始化和滚动表格时,您的第二个选项卡是隐藏的。这是工作示例。 jsFiddleDemo

    $("#table1").DataTable({
        "scrollY": 308,
        "paging": false,
        "responsive": true
    });
    $('a[href="#profile"]').one('click',function(){
        setTimeout(function(){
            $("#table2").DataTable({
                "scrollY": 308,
                "paging": false,
                "responsive": true
            });
        },0);
    });
    

    【讨论】:

    • 感谢它运行良好,虽然我有这个想法,但仍然知道为什么会这样?
    • 主要是因为table2隐藏在另一个选项卡中,可能由于隐藏而无法获取高度,无法设置scrollY的位置。虽然这是我的假设,我可能错了,但这是我最初的想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 2018-04-24
    • 2014-12-14
    相关资源
    最近更新 更多