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 menu or modal. When user activates the tab or opens the menu and table becomes visible, it usually has columns not working and having incorrect width or unexpected behavior.
Below is a list of the most common issues when table is initially hidden.
- Incorrect column widths
- Responsive extension – Incorrect breakpoints
- Scroller extension – Incorrect column widths or missing data
- FixedColumns extension – Incorrect column widths
1. Incorrect column widths
Problem
Both tables have the same column widths defined with column.width option. However table in “Tab 2” has column widths different from the table in “Tab 1”.
Name |
Position |
Office |
Age |
Start date |
Salary |
Tiger Nixon |
System Architect |
Edinburgh |
61 |
2011/04/25 |
$320,800 |
Garrett Winters |
Accountant |
Tokyo |
63 |
2011/07/25 |
$170,750 |
Ashton Cox |
Junior Technical Author |
San Francisco |
66 |
2009/01/12 |
$86,000 |
Name |
Position |
Office |
Age |
Start date |
Salary |
Bradley Greer |
Software Engineer |
London |
41 |
2012/10/13 |
$132,000 |
Dai Rios |
Personnel Lead |
Edinburgh |
35 |
2012/09/26 |
$217,500 |
Jenette Caldwell |
Development Lead |
New York |
30 |
2011/09/03 |
$345,000 |
Solution
Use the code below to recalculate column widths of all visible tables once a tab becomes active using columns.adjust() API method.
$('a[data-toggle="tab"]').on('shown.bs.tab', function(e){
$($.fn.dataTable.tables(true)).DataTable()
.columns.adjust();
});
Name |
Position |
Office |
Age |
Start date |
Salary |
Tiger Nixon |
System Architect |
Edinburgh |
61 |
2011/04/25 |
$320,800 |
Garrett Winters |
Accountant |
Tokyo |
63 |
2011/07/25 |
$170,750 |
Ashton Cox |
Junior Technical Author |
San Francisco |
66 |
2009/01/12 |
$86,000 |
Name |
Position |
Office |
Age |
Start date |
Salary |
Bradley Greer |
Software Engineer |
London |
41 |
2012/10/13 |
$132,000 |
Dai Rios |
Personnel Lead |
Edinburgh |
35 |
2012/09/26 |
$217,500 |
Jenette Caldwell |
Development Lead |
New York |
30 |
2011/09/03 |
$345,000 |
Edit on jsFiddle
2. Responsive extension – Incorrect breakpoints
Problem
Both tables have Responsive extension enabled with responsive option. Columns that do not fit the container of the table in “Tab 1” are hidden. However table in “Tab 2” has all columns visible, even those that do not fit the container.
Name |
Position |
Office |
Age |
Start date |
Salary |
Phone |
Extension |
Department |
Supervisor |
Tiger Nixon |
System Architect |
Edinburgh |
61 |
2011/04/25 |
$320,800 |
123-456-7890 |
1230 |
IT |
Dai Rios |
Garrett Winters |
Accountant |
Tokyo |
63 |
2011/07/25 |
$170,750 |
123-456-7890 |
1231 |
Accounting |
Angelica Ramos |
Ashton Cox |
Junior Technical Author |
San Francisco |
66 |
2009/01/12 |
$86,000 |
123-456-7890 |
1232 |
Marketing |
Angelica Ramos |
Name |
Position |
Office |
Age |
Start date |
Salary |
Phone |
Extension |
Department |
Supervisor |
Tiger Nixon |
System Architect |
Edinburgh |
61 |
2011/04/25 |
$320,800 |
123-456-7890 |
1230 |
IT |
Dai Rios |
Garrett Winters |
Accountant |
Tokyo |
63 |
2011/07/25 |
$170,750 |
123-456-7890 |
1231 |
Accounting |
Angelica Ramos |
Ashton Cox |
Junior Technical Author |
San Francisco |
66 |
2009/01/12 |
$86,000 |
123-456-7890 |
1232 |
Marketing |
Angelica Ramos |
Solution
Use the code below to recalculate column widths of all visible tables once a tab becomes active by using a combination of columns.adjust() and responsive.recalc() API methods.
$('a[data-toggle="tab"]').on('shown.bs.tab', function(e){
$($.fn.dataTable.tables(true)).DataTable()
.columns.adjust()
.responsive.recalc();
});
Name |
Position |
Office |
Age |
Start date |
Salary |
Phone |
Extension |
Department |
Supervisor |
Tiger Nixon |
System Architect |
Edinburgh |
61 |
2011/04/25 |
$320,800 |
123-456-7890 |
1230 |
IT |
Dai Rios |
Garrett Winters |
Accountant |
Tokyo |
63 |
2011/07/25 |
$170,750 |
123-456-7890 |
1231 |
Accounting |
Angelica Ramos |
Ashton Cox |
Junior Technical Author |
San Francisco |
66 |
2009/01/12 |
$86,000 |
123-456-7890 |
1232 |
Marketing |
Angelica Ramos |
Name |
Position |
Office |
Age |
Start date |
Salary |
Phone |
Extension |
Department |
Supervisor |
Tiger Nixon |
System Architect |
Edinburgh |
61 |
2011/04/25 |
$320,800 |
123-456-7890 |
1230 |
IT |
Dai Rios |
Garrett Winters |
Accountant |
Tokyo |
63 |
2011/07/25 |
$170,750 |
123-456-7890 |
1231 |
Accounting |
Angelica Ramos |
Ashton Cox |
Junior Technical Author |
San Francisco |
66 |
2009/01/12 |
$86,000 |
123-456-7890 |
1232 |
Marketing |
Angelica Ramos |
Edit on jsFiddle
3. Scroller extension – Incorrect column widths or missing data
Problem
Both tables have Scroller extension enabled with scroller option and table height defined with scrollY option. However in “Tab 2” width of the columns in the header do not match width of the columns in table body. Also in some cases table may appear as having the data missing.
Name |
Position |
Office |
Age |
Start date |
Salary |
Tiger Nixon |
System Architect |
Edinburgh |
61 |
2011/04/25 |
$320,800 |
Garrett Winters |
Accountant |
Tokyo |
63 |
2011/07/25 |
$170,750 |
Ashton Cox |
Junior Technical Author |
San Francisco |
66 |
2009/01/12 |
$86,000 |
Name |
Position |
Office |
Age |
Start date |
Salary |
Bradley Greer |
Software Engineer |
London |
41 |
2012/10/13 |
$132,000 |
Dai Rios |
Personnel Lead |
Edinburgh |
35 |
2012/09/26 |
$217,500 |
Jenette Caldwell |
Development Lead |
New York |
30 |
2011/09/03 |
$345,000 |
Solution
Use the code below to recalculate column widths of all visible tables once a tab becomes active using scroller.measure() API method.
$('a[data-toggle="tab"]').on('shown.bs.tab', function(e){
$($.fn.dataTable.tables(true)).DataTable()
.scroller.measure();
});
Name |
Position |
Office |
Age |
Start date |
Salary |
Tiger Nixon |
System Architect |
Edinburgh |
61 |
2011/04/25 |
$320,800 |
Garrett Winters |
Accountant |
Tokyo |
63 |
2011/07/25 |
$170,750 |
Ashton Cox |
Junior Technical Author |
San Francisco |
66 |
2009/01/12 |
$86,000 |
Name |
Position |
Office |
Age |
Start date |
Salary |
Bradley Greer |
Software Engineer |
London |
41 |
2012/10/13 |
$132,000 |
Dai Rios |
Personnel Lead |
Edinburgh |
35 |
2012/09/26 |
$217,500 |
Jenette Caldwell |
Development Lead |
New York |
30 |
2011/09/03 |
$345,000 |
Edit on jsFiddle
4. FixedColumns extension – Incorrect column widths
Problem
Both tables have FixedColumns extension enabled with fixedColumns option. 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.
Name |
Position |
Office |
Age |
Start date |
Salary |
Tiger Nixon |
System Architect |
Edinburgh |
61 |
2011/04/25 |
$320,800 |
Garrett Winters |
Accountant |
Tokyo |
63 |
2011/07/25 |
$170,750 |
Ashton Cox |
Junior Technical Author |
San Francisco |
66 |
2009/01/12 |
$86,000 |
Name |
Position |
Office |
Age |
Start date |
Salary |
Bradley Greer |
Software Engineer |
London |
41 |
2012/10/13 |
$132,000 |
Dai Rios |
Personnel Lead |
Edinburgh |
35 |
2012/09/26 |
$217,500 |
Jenette Caldwell |
Development Lead |
New York |
30 |
2011/09/03 |
$345,000 |
Solution
Use the code below to recalculate column widths of all visible tables once a tab becomes active using columns.adjust() and fixedColumns().relayout() API methods.
$('a[data-toggle="tab"]').on('shown.bs.tab', function(e){
$($.fn.dataTable.tables(true)).DataTable()
.columns.adjust()
.fixedColumns().relayout();
});
Name |
Position |
Office |
Age |
Start date |
Salary |
Tiger Nixon |
System Architect |
Edinburgh |
61 |
2011/04/25 |
$320,800 |
Garrett Winters |
Accountant |
Tokyo |
63 |
2011/07/25 |
$170,750 |
Ashton Cox |
Junior Technical Author |
San Francisco |
66 |
2009/01/12 |
$86,000 |
Name |
Position |
Office |
Age |
Start date |
Salary |
Bradley Greer |
Software Engineer |
London |
41 |
2012/10/13 |
$132,000 |
Dai Rios |
Personnel Lead |
Edinburgh |
35 |
2012/09/26 |
$217,500 |
Jenette Caldwell |
Development Lead |
New York |
30 |
2011/09/03 |
$345,000 |
Edit on jsFiddle
Michael Ryvkin
Freelance web developer based in Perkasie, Bucks County, PA. I design and develop custom websites and web applications, perform website maintenance and provide technical support for small and medium-sized businesses.
Contact me for a free quote or consultation on your project.
How to hide the dynamically adjust the height of the table when lesser number of items are displayed?
Although this is unrelated to the article but if you’re using Scroller extension with
scrollY
, usescrollCollapse: true
to reduce table height when limited number of rows are shown.Regarding solution 2, if there’s no data in the table then the “No data in table” cell which is supposed to span the entire table only spans the first column.
If you’re fetching your data from an AJAX store which is often empty, this is a big no no…
Chaining a draw(); after calling columns.adjust fixes this issue.
https://jsfiddle.net/ppgumq4m/
I see your point, very strange behavior. Let me experiment with this a bit and I will update the article.
Hi Michael,
I have tried your suggestion in my code regarding section “1. Incorrect column widths”. But it is not working at all.
I am using Jquery v3.1.1 and Bootstrap v3.3.7. Could you please guide me what mistake i have done.
HTML Page:
Jquery:
You can refer here for my code: https://jsfiddle.net/ujjwal29/hsmzqLq4/1/
Hi Michael,
I have a similar problem as case 3 (3. Scroller extension – Incorrect column widths or missing data).
I tried all options, but the header is still a problem for me. I tried adding
$.fn.dataTable.tables(true)).DataTable().scroller.measure();
and also$($.fn.dataTable.tables(true)).DataTable().columns.adjust().responsive.recalc();
after adding scroller and responsive to datatable, but I still see the same issue.I have accordions and each accordion has a datatable in it.
Do you have any suggestion for me?
You need to call these methods when data table becomes visible, i.e. when accordion has been activated. See this answer on StackOverflow for an example.
Oh wow! I was calling these methods after the table and accordion were rendered, but I never called it inside accordion activate. Thanks for sharing the stackoveflow link. It solved my problem.
I have one more problem which I am trying to solve.
In my case, the datatable header is rendered normal in Chrome, but in all other browsers, the header expands and it’s height is 5-6 times the height of data rows.
I tried a few options to change height of the header columns by applying suggestions from datatable.net forum, but with no luck. I see the height attribute is applied to of my datatable, but the header is still rendered out of proportion.
Do you have any pointers for this?
P.S. I am sorry that I am not sharing any image here. The jsfiddle examples are also rendering normally with all static binding, which makes them futile. My page dynamically renders all accordion and datatables in those accordions.
I tried this but it is give error, trUncaught TypeError: $(…).accordion is not a function
Do you have any suggestion for me?
Hey, thanks a lot! I was so stuck in col adjust thing & now works like a charm
Thanks, glad that it worked for you!
Hi,
I am having the same issue though it’s caused because the left nav-bar collapses onload which affects the pre-defined width of the table (it is stretched out when the nav-bar collapses).
On hover, the nav-bar expands to display the links.
The header has a toggle show/hide nav-bar button as well which shrinks/expands the page content, again affecting the table width
How can I set the ‘columns.adjust();’ in this case?
I draw tabs dynamically with jQuery and inside each tab i draw a datatable. I tried those solutions but i have the same issue. The width of table is smaller like tab.
I tried a lot of things like below code, but it works bad.
i also have same problem. Michael plzzz help us .
I would like to know something from example 2.
How would you make it from the 2nd responsive column?
Thank you.
Can you please clarify your question?
Muchas gracias por tan valioso artículo. Me ayudo a resolver un problema en asp.net mvc 4. Saludos desde Colombia.
Muchas gracias!
Great, been searching on this, struggling to find correct keyword, gladly found this and works on first solution, thanks.
Thanks!
Hi, Firstly thank you this issue.
I have a problem 🙁 .
Console:
According to the official documentation scroller.measure() API method available in Scroller extension since version 1.2.0. It is possibly you’re using older version of the Scroller extension where this API method is not available.
Thank you so much. I had old version its. I updated then solved this problem.
I have similar issue like the problem is dataTable being hidden on load, and because scroll header is appending 100%, dataTable columns are not aligned correctly.
I used,
I have tried the below, but no luck.
please let me, if any suggestion.
Thanks for the code. I was stuck ‘Responsive extension – Incorrect breakpoints’ for months. This post just solved my problem.
Thank you! You have a nice website listed in your profile!
You saved my life! That’s all I needed doing in my code. I was struggling to find a solution for that and you clearly showed me. thank you. Greetings from Brazil!
Thank you, Felipe!
in my dataTable code first column width is very large and there is scroller at bottom.But i want the table fit to display screen without any scroller and avoid too much width of first column
You may want to play with columns.width option or use Responsive extension.
Problem/Solution 1 was exactly what I needed. Thank you!
Hi Micheal,
I have a problem with fixed column, it is duplicating the columns that are declared as fixed in the datatable initialization. Any idea what could be the issue.
Thank you For This Sir. it’s very helpful for me
You have no ideal how much this articles helped me. I love you so much
finally found this after 2 days debugging and it’s work. thanks