Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Responsive DataTables And Bootstrap Tabs

I want to use Datatables and Responsive extension inside Bootstrap Tabs. 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

like image 637
Lubco Avatar asked Sep 21 '15 08:09

Lubco


People also ask

What are the column width issues with jQuery DataTables and bootstrap?

> Articles > Web Development jQuery DataTables: Column width issues with Bootstrap tabs February 15, 2016July 31, 2017Michael Ryvkin 5959 One of the most common issues with a table using jQuery DataTables and Bootstrap framework is when the table is initially hidden. For example, your table is located in a tab, accordion menuor modal.

Is there a responsive property on the object returned by DataTables?

However, there isn't a responsive property on the object returned by a dataTables () invocation with which I could call the above methods. I have responsive tables working, so that's not the issue. Sorry, something went wrong. No, but there is from $ ().DataTable () - see FAQs. It sort of is a bug and sort of isn't...

Is it possible to get the event handler in DataTables responsive API?

API method responsive.recalc () is available in dataTables.responsive.js since 1.0.1, you're including version 1.0.0. Event handler should be attached once DOM is available.

What is the problem with fixedcolumns in Tab 2?

Problem Both tables have FixedColumns extension enabled with fixedColumnsoption. However in “Tab 2” table has incorrect height and width of the columns in the header do not match width of the columns in table body. Tab 1 Tab 2 (Problem) Name Position Office Age Start date Salary Tiger Nixon System Architect Edinburgh 61 2011/04/25 $320,800


1 Answers

CAUSE

There are multiple issues with your code:

  1. Bootstrap library is included before jQuery library
  2. API method responsive.recalc() is available in dataTables.responsive.js since 1.0.1, you're including version 1.0.0.
  3. Event handler should be attached once DOM is available.

SOLUTION

  1. Include Bootstrap library after jQuery library

  2. Include Responsive extension version 1.0.1 or later

  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

See updated jsFiddle for code and demonstration.

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 183
Gyrocode.com Avatar answered Sep 21 '22 17:09

Gyrocode.com