javascript - 数据表标题在 Bootstrap 选项卡中未对齐

标签 javascript jquery twitter-bootstrap datatable

我正在尝试将 DataTable 与 Twitter bootstrap Tabs 一起使用,为此我正在尝试以下代码,它成功调用 dataTable 并创建 dataTable,但对于 tab2 DataTable header 未对齐

 <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 header 时,标题未对齐

jsFiddle

最佳答案

这是因为当您初始化并滚动 table2 时,您的第二个选项卡被隐藏。这是工作示例。 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);
});

关于javascript - 数据表标题在 Bootstrap 选项卡中未对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33477494/

相关文章:

jquery - :last vs :last-child selector

图形的 html 映射不会随着 Bootstrap 中的图像而改变

javascript - 带启动和停止功能的 Draggables Jquery UI

javascript - 使用 vanilla JS 对指向同一页面的链接做出不同的 react

javascript - Flowplayer 在 IE 下无法启动

html - col-10 和 col-2 行在 bootstrap 3 的右侧生成奇怪的间距

css - 下拉菜单可点击区域调整

javascript - gulp-rigger 的选项

javascript - 即使加载了 UJS,Rails DELETE 调用也不起作用

javascript - 使用 Rails 3 和 jQuery 进行动态选择