Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatables improperly sized in bootstrap tab

I have a page that has three bootstrap tabs and a datatable in each tab. The datatable in the first tab works fine. However, the datatable in the second tab is at half-width and I have no idea why. There's no css applied to it either.

Here's a screenshot of the first tab (works fine) Tab 1 - works

And the 2nd tab (datatable at half width) tab 2 - datatable at half width

My code:

$('#companies').DataTable({ //table in tab 1 (works fine)
        responsive: true
});


$('#companies2').DataTable({ //table in tab 2
        responsive: true
});

My html:

<!-- tab start -->
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h1>Tabs</h1>
            <ul class="nav nav-tabs">
                <li class="active"><a href="#tab1" data-toggle="tab">Section 1</a></li>
                <li><a href="#tab2" data-toggle="tab">Section 2</a></li>
                <li><a href="#tab3" data-toggle="tab">Section 3</a></li>
            </ul>

            <!-- tab sections -->
            <div class="tab-content">
                <div class="tab-pane active" id="tab1">
                    <table id="companies" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0">
                        <thead>
                            <tr>
                                <th>Company ID</th>
                                <th>Company Name</th>
                                <th>Status</th>
                                <th></th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            {{#each context}}
                            {{#if setup}}
                            <tr>
                                <td>{{company_id}}</td>
                                <td>{{company_name}}</td>
                                {{#if is_approved}}
                                <td>Approved</td>
                                {{else}}
                                <td>Not Approved</td>
                                {{/if}}
                                <td><a href="/admin/viewclientorders/{{company_id}}">View Orders</a></td>
                                <td><a href="/admin/company/{{company_id}}">View Details</a></td>
                            </tr>
                            {{/if}}
                            {{/each}}
                        </tbody>
                    </table> 
                </div>
                <div class="tab-pane" id="tab2">
                    <table id="companies2" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0">
                        <thead>
                            <tr>
                                <th>Company ID</th>
                                <th>Company Name</th>
                                <th>Create Date</th>
                                <th></th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            {{#each context}}
                            {{#unless setup}}
                            <tr>
                                <td>{{company_id}}</td>
                                <td>{{company_name}}</td>
                                <td>{{created_at}}</td>
                                <td><button class="btn btn-primary setup"><a href="/admin/companies/setup_company/{{company_id}}">Setup</a></button></td>
                                <td><button class="btn btn-danger delete">Ignore</button></td>
                            </tr>
                            {{/unless}}
                            {{/each}}
                        </tbody>
                    </table>
                </div>
                <div class="tab-pane" id="tab3">
                    3rd
                </div>

            </div>

Can someone help?

like image 822
Trung Tran Avatar asked Mar 29 '16 00:03

Trung Tran


3 Answers

SOLUTION

Use columns.adjust() and responsive.recalc() recalculate the column widths when table becomes visible.

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

DEMO

https://jsfiddle.net/gyrocode/Ltga3n4u/

LINKS

See jQuery DataTables – Column width issues with Bootstrap tabs for solution to the most common problems with jQuery DataTables and Bootstrap Tabs.

like image 153
Javier Navarro Avatar answered Nov 18 '22 18:11

Javier Navarro


Keep the dataTable instances, i.e.

var companies2 = $('#companies2').DataTable({
  responsive: true
})

Then use columns.adjust() to recalculate the widths when the tab holding the table becomes visible :

$('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
  if (e.target.hash == '#tab2') {
    companies2.columns.adjust().draw()
  }
})

Completely untested with your specific scenario, but this is certainly the way to go.

like image 7
davidkonrad Avatar answered Nov 18 '22 17:11

davidkonrad


I found a very simple solution : adding "style:100%"

<table style="width: 100% !important" id="my-table" datatable="ng" dt-options="dtOptions" class="table table-striped table-bordered table-hover dataTables-example">
like image 6
Fox5150 Avatar answered Nov 18 '22 18:11

Fox5150