I am using Datatable to give style to my table in HTML and I had to enable the ScrollY. Although It worked, it has a style problem that I am trying to solve. When you resize the window, for some reason, the header of the table doesn't resize with the table itself. I had some research and I found out when the web page is ready,it creates this tags related to the table:
<div class="dataTables_scrollHeadInner" style="box-sizing: content-box; width: 1632px; padding-right: 0px;">
<table class="table table-hover table-striped cell-border table-bord dataTable no-footer" style="width: 1632px; margin-left: 0px;">
My original table still exists, but It creates these tags to do some things.
- I tried give to my table a style ="width:100%", but this created tags don't change with my table.
- I also tried to change it through JS using the class as reference and directly in css using the class as reference but it didn't work.
- I tried to use tableMain.columns.adjust().draw(), didn't work.
- Only when you inspect the page and change it through the page you can solve it.
HTML:
<div class="box-body table-responsive">
<table id="tbMain" class="table table-hover table-striped cell-border table-bord" style="width:100%">
<thead>
<tr>
<th rowspan="2"></th>
<th rowspan="2">COLUMN 1</th>
<th rowspan="2"></th>
<th rowspan="2">COLUMN 2</th>
<th colspan="1">COLUMN 3</th>
<th rowspan="2">COLUMN 4</th>
</tr>
<tr>
<th>COLUMN 5</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
JS:
let tableMain = $('#tbMain').DataTable({
'scrollY': '400px',
'scrollCollapse': true,
'responsive': true,
'ordering': false,
'order': [1, 'asc'],
'columns': [
{width: '5%', className: 'text-center'},
{width: '5%', className: 'text-center'},
{width: '30%', className: 'text-center'},
{width: '40%', className: 'text-center'},
{width: '10%', className: 'text-center'},
{width: '10%', className: 'text-center'}
],
'buttons': []
}
});
$("body").css("overflow", "hidden");
I used Bootstrap 3.3.6 and Datatable 1.11.5.
I hope you can help me.