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
> 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.
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...
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.
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
There are multiple issues with your code:
responsive.recalc()
is available in dataTables.responsive.js
since 1.0.1
, you're including version 1.0.0
.Include Bootstrap library after jQuery library
Include Responsive extension version 1.0.1 or later
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();
});
});
See updated jsFiddle for code and demonstration.
See jQuery DataTables – Column width issues with Bootstrap tabs for solution to the most common problems with jQuery DataTables and Bootstrap Tabs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With