How do I let datatable headers adjust with the column width?









up vote
1
down vote

favorite












I am using jQuery datatables for a project and I have the following issue:



On first load, the columns of the header and data rows are nicely aligned. However, when the window is resized, the body cells shrink or grow, whereas the header cells remain a constant width. The result is a mis-alignment of the header and body rows.
The following code is used to initialize the datatable:



$el.DataTable(
paging: true,
lengthChange: true,
searching: true,
ordering: true,
info: true,
autoWidth: false,
pageLength: 25,
columns: getColumns(configurationObject),
scrollX: true,
language: localizationObject,
data: getDataset(configurationObject),
dom: 'lrtip',
lengthMenu: [
[10, 25, 50, 100, -1],
[10, 25, 50, 100, localizationObject['allRows']]
]
);


And the html is as follows:



<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Code</th>
<th>Description</th>
</tr>
</thead>
</table>


In my quest to solve this issue, I have tried the following:



  • Set autoWidth to true/false

  • Enable/Disable the responsive plugin

Many answers revolve around the table being constructed before the dom is finished loading, or inside a hidden div preventing widths to be calculated. Neither of those are true for me. The code above gets called in a $(document).ready(function () } and is not hidden when called (as I said, initial load works fine, but I thought it is worth mentioning).



In my investigation, I noticed that, for the header row, an entirely new html table is created. The cells of that row are also given a fixed width. That does explain the misalignment to me, but, I am no closer to actually resolving the issue at hand.



I am trying to achieve either of two scenarios:



  • The columns of the body are of fixed width, and horizontal scroll will take over if the table overflows. I have set scrollX to true, and on small screens this works very well.

  • The columns of the header row are not of fixed width, but rather the width of the body columns.

Hopefully I have provided enough information for someone to find an answer, because I have googled and looked and I can't find it. If more information is needed, then please let me know.



Thank you in advance.










share|improve this question





















  • I ran into something similar and I have to call the DataTable.adjust() every time. May be you can try it
    – Loredra L
    Nov 9 at 16:51










  • Are you able to made a snippet example with the basic code that generate the issue?
    – D. Smania
    Nov 9 at 22:00










  • try <table class="table table-bordered table-striped" style="width: 100%;">
    – zerosand1s
    Nov 10 at 3:02














up vote
1
down vote

favorite












I am using jQuery datatables for a project and I have the following issue:



On first load, the columns of the header and data rows are nicely aligned. However, when the window is resized, the body cells shrink or grow, whereas the header cells remain a constant width. The result is a mis-alignment of the header and body rows.
The following code is used to initialize the datatable:



$el.DataTable(
paging: true,
lengthChange: true,
searching: true,
ordering: true,
info: true,
autoWidth: false,
pageLength: 25,
columns: getColumns(configurationObject),
scrollX: true,
language: localizationObject,
data: getDataset(configurationObject),
dom: 'lrtip',
lengthMenu: [
[10, 25, 50, 100, -1],
[10, 25, 50, 100, localizationObject['allRows']]
]
);


And the html is as follows:



<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Code</th>
<th>Description</th>
</tr>
</thead>
</table>


In my quest to solve this issue, I have tried the following:



  • Set autoWidth to true/false

  • Enable/Disable the responsive plugin

Many answers revolve around the table being constructed before the dom is finished loading, or inside a hidden div preventing widths to be calculated. Neither of those are true for me. The code above gets called in a $(document).ready(function () } and is not hidden when called (as I said, initial load works fine, but I thought it is worth mentioning).



In my investigation, I noticed that, for the header row, an entirely new html table is created. The cells of that row are also given a fixed width. That does explain the misalignment to me, but, I am no closer to actually resolving the issue at hand.



I am trying to achieve either of two scenarios:



  • The columns of the body are of fixed width, and horizontal scroll will take over if the table overflows. I have set scrollX to true, and on small screens this works very well.

  • The columns of the header row are not of fixed width, but rather the width of the body columns.

Hopefully I have provided enough information for someone to find an answer, because I have googled and looked and I can't find it. If more information is needed, then please let me know.



Thank you in advance.










share|improve this question





















  • I ran into something similar and I have to call the DataTable.adjust() every time. May be you can try it
    – Loredra L
    Nov 9 at 16:51










  • Are you able to made a snippet example with the basic code that generate the issue?
    – D. Smania
    Nov 9 at 22:00










  • try <table class="table table-bordered table-striped" style="width: 100%;">
    – zerosand1s
    Nov 10 at 3:02












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am using jQuery datatables for a project and I have the following issue:



On first load, the columns of the header and data rows are nicely aligned. However, when the window is resized, the body cells shrink or grow, whereas the header cells remain a constant width. The result is a mis-alignment of the header and body rows.
The following code is used to initialize the datatable:



$el.DataTable(
paging: true,
lengthChange: true,
searching: true,
ordering: true,
info: true,
autoWidth: false,
pageLength: 25,
columns: getColumns(configurationObject),
scrollX: true,
language: localizationObject,
data: getDataset(configurationObject),
dom: 'lrtip',
lengthMenu: [
[10, 25, 50, 100, -1],
[10, 25, 50, 100, localizationObject['allRows']]
]
);


And the html is as follows:



<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Code</th>
<th>Description</th>
</tr>
</thead>
</table>


In my quest to solve this issue, I have tried the following:



  • Set autoWidth to true/false

  • Enable/Disable the responsive plugin

Many answers revolve around the table being constructed before the dom is finished loading, or inside a hidden div preventing widths to be calculated. Neither of those are true for me. The code above gets called in a $(document).ready(function () } and is not hidden when called (as I said, initial load works fine, but I thought it is worth mentioning).



In my investigation, I noticed that, for the header row, an entirely new html table is created. The cells of that row are also given a fixed width. That does explain the misalignment to me, but, I am no closer to actually resolving the issue at hand.



I am trying to achieve either of two scenarios:



  • The columns of the body are of fixed width, and horizontal scroll will take over if the table overflows. I have set scrollX to true, and on small screens this works very well.

  • The columns of the header row are not of fixed width, but rather the width of the body columns.

Hopefully I have provided enough information for someone to find an answer, because I have googled and looked and I can't find it. If more information is needed, then please let me know.



Thank you in advance.










share|improve this question













I am using jQuery datatables for a project and I have the following issue:



On first load, the columns of the header and data rows are nicely aligned. However, when the window is resized, the body cells shrink or grow, whereas the header cells remain a constant width. The result is a mis-alignment of the header and body rows.
The following code is used to initialize the datatable:



$el.DataTable(
paging: true,
lengthChange: true,
searching: true,
ordering: true,
info: true,
autoWidth: false,
pageLength: 25,
columns: getColumns(configurationObject),
scrollX: true,
language: localizationObject,
data: getDataset(configurationObject),
dom: 'lrtip',
lengthMenu: [
[10, 25, 50, 100, -1],
[10, 25, 50, 100, localizationObject['allRows']]
]
);


And the html is as follows:



<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Code</th>
<th>Description</th>
</tr>
</thead>
</table>


In my quest to solve this issue, I have tried the following:



  • Set autoWidth to true/false

  • Enable/Disable the responsive plugin

Many answers revolve around the table being constructed before the dom is finished loading, or inside a hidden div preventing widths to be calculated. Neither of those are true for me. The code above gets called in a $(document).ready(function () } and is not hidden when called (as I said, initial load works fine, but I thought it is worth mentioning).



In my investigation, I noticed that, for the header row, an entirely new html table is created. The cells of that row are also given a fixed width. That does explain the misalignment to me, but, I am no closer to actually resolving the issue at hand.



I am trying to achieve either of two scenarios:



  • The columns of the body are of fixed width, and horizontal scroll will take over if the table overflows. I have set scrollX to true, and on small screens this works very well.

  • The columns of the header row are not of fixed width, but rather the width of the body columns.

Hopefully I have provided enough information for someone to find an answer, because I have googled and looked and I can't find it. If more information is needed, then please let me know.



Thank you in advance.







javascript jquery datatables






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 15:06









Maarten

415




415











  • I ran into something similar and I have to call the DataTable.adjust() every time. May be you can try it
    – Loredra L
    Nov 9 at 16:51










  • Are you able to made a snippet example with the basic code that generate the issue?
    – D. Smania
    Nov 9 at 22:00










  • try <table class="table table-bordered table-striped" style="width: 100%;">
    – zerosand1s
    Nov 10 at 3:02
















  • I ran into something similar and I have to call the DataTable.adjust() every time. May be you can try it
    – Loredra L
    Nov 9 at 16:51










  • Are you able to made a snippet example with the basic code that generate the issue?
    – D. Smania
    Nov 9 at 22:00










  • try <table class="table table-bordered table-striped" style="width: 100%;">
    – zerosand1s
    Nov 10 at 3:02















I ran into something similar and I have to call the DataTable.adjust() every time. May be you can try it
– Loredra L
Nov 9 at 16:51




I ran into something similar and I have to call the DataTable.adjust() every time. May be you can try it
– Loredra L
Nov 9 at 16:51












Are you able to made a snippet example with the basic code that generate the issue?
– D. Smania
Nov 9 at 22:00




Are you able to made a snippet example with the basic code that generate the issue?
– D. Smania
Nov 9 at 22:00












try <table class="table table-bordered table-striped" style="width: 100%;">
– zerosand1s
Nov 10 at 3:02




try <table class="table table-bordered table-striped" style="width: 100%;">
– zerosand1s
Nov 10 at 3:02

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228272%2fhow-do-i-let-datatable-headers-adjust-with-the-column-width%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228272%2fhow-do-i-let-datatable-headers-adjust-with-the-column-width%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus