简体   繁体   English

响应式DataTables和Bootstrap选项卡的问题

[英]Issue with Responsive DataTables And Bootstrap Tabs

I want to use Datatables and Responsive extension inside Bootstrap Tabs. 我想在Bootstrap选项卡中使用Datatables和Responsive扩展。 I have this working separately. 我把它分开工作了。

$(document).ready(function() {
    $('#example').DataTable( {
        responsive: true
    } );
    $('#exampleInTab').DataTable( {
        responsive: true
    } );
} );

$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
    $($.fn.dataTable.tables(true)).DataTable()
        .columns.adjust()
        .responsive.recalc();
});

You can see the issue here 你可以在这里看到问题

CAUSE 原因

There are multiple issues with your code: 您的代码存在多个问题:

  1. Bootstrap library is included before jQuery library 引导库包含在jQuery库之前
  2. API method responsive.recalc() is available in dataTables.responsive.js since 1.0.1 , you're including version 1.0.0 . API方法responsive.recalc()是可用的dataTables.responsive.js自从1.0.1 ,你包括版本1.0.0
  3. Event handler should be attached once DOM is available. 一旦DOM可用,就应该附加事件处理程序。

SOLUTION

  1. Include Bootstrap library after jQuery library 在jQuery库之后包含Bootstrap库

  2. Include Responsive extension version 1.0.1 or later 包括响应式扩展程序版本1.0.1或更高版本

  3. Use the code below: 使用以下代码:

     $(document).ready(function () { $('#example').DataTable({ responsive: true }); $('#exampleInTab').DataTable({ responsive: true }); $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) { $($.fn.dataTable.tables(true)).DataTable() .columns.adjust() .responsive.recalc(); }); }); 

DEMO DEMO

See updated jsFiddle for code and demonstration. 有关代码和演示,请参阅更新的jsFiddle

LINKS 链接

See jQuery DataTables – Column width issues with Bootstrap tabs for solution to the most common problems with jQuery DataTables and Bootstrap Tabs. 请参阅jQuery DataTables - Bootstrap选项卡的列宽问题,以解决jQuery DataTables和Bootstrap选项卡最常见的问题。

I tried the answers above but none worked. 我尝试了上面的答案但没有奏效。 I was using JQuery Steps Wizard as tabs. 我使用JQuery Steps Wizard作为选项卡。 I had to use $('#datatable').css("width", '100%') as well for it to work. 我不得不使用$('#datatable').css("width", '100%')以及它的工作原理。

wizard.steps({
            headerTag: "h3",
            bodyTag: "section",
            transitionEffect: "slideLeft",
            enableFinishButton: false,
            enablePagination: false,
            enableAllSteps: true,
            titleTemplate: "#title#",
            cssClass: "tabcontrol",
            onStepChanged: function (event, currentIndex, priorIndex) {

                if (currentIndex == 2) {
                    $('#datatable').css("width", '100%')
                    $($.fn.dataTable.tables(true)).DataTable().columns.adjust().responsive.recalc();
                }
            }
        })

My Datatable is on the 3rd tab hence the check on the currentIndex . 我的数据表位于第3个选项卡上,因此检查currentIndex

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

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