I am using jQuery table header in my Laravel-5.8 project
<div class="card-body">
<div id="accordion">
<!-- we are adding the .class so bootstrap.js collapse plugin detects it -->
<div class="card card-secondary">
<div class="card-header">
<h4 class="card-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
Internal Respondents
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="card-body">
<div class="table-responsive">
<table id="myTable" class=" table table-bordered table-striped table-hover datatable datatable-internal">
<thead>
<tr>
<th width="10">
#
</th>
<th width="30">
Respondent
</th>
<th width="20">
Department
</th>
<th width="20">
Date
</th>
<th width="10">
Status
</th>
<th width="10">
</th>
</tr>
</thead>
<tbody>
@foreach($skills as $key => $skill)
<tr>
<td>
{{$key+1}}
</td>
<td>
{{$skill->skill_name ?? '' }}
</td>
<td>
{{$skill->description ?? '' }}
</td>
<td>
{{$skill->skill_name ?? '' }}
</td>
<td>
{{$skill->description ?? '' }}
</td>
<td>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
I am using JQuery databable as shown below.
Javascript
<script src="{{ asset('theme/adminlte3/plugins/datatables/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('theme/adminlte3/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js') }}"></script>
<script src="{{ asset('theme/adminlte3/plugins/datatables-buttons/js/dataTables.buttons.min.js') }}"></script>
<script src="{{ asset('theme/adminlte3/plugins/datatables-select/js/dataTables.select.min.js') }}"></script>
<script src="{{ asset('theme/adminlte3/plugins/datatables-buttons/js/buttons.flash.min.js') }}"></script>
<script src="{{ asset('theme/adminlte3/plugins/datatables-buttons/js/buttons.html5.min.js') }}"></script>
<script src="{{ asset('theme/adminlte3/plugins/datatables-buttons/js/buttons.print.min.js') }}"></script>
<script src="{{ asset('theme/adminlte3/plugins/datatables-buttons/js/buttons.colVis.min.js') }}"></script>
<script>
$(document).ready(function() {
$('#myTable').DataTable({
stateSave: true,
"pageLength": 10,
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
});
});
</script>
When I rendered the page, the header width did not align with the body. It appears this way:
[![datatable][1]][1]
How do I make the datatable to automatically align by itself when the page loads?
Thank you.
When I added
<script>
$(document).ready(function(){
$('#example1-tab1-dt').DataTable({
columns: [
{ width: '20%' },
{ width: '20%' },
{ width: '20%' },
{ width: '10%' },
{ width: '15%' },
{ width: '15%' }
]
});
$('#example1-tab2-dt').DataTable({
columns: [
{ width: '20%' },
{ width: '20%' },
{ width: '20%' },
{ width: '10%' },
{ width: '15%' },
{ width: '15%' }
]
});
$('a[data-toggle="tab"]').on('shown.bs.tab', function(e){
$($.fn.dataTable.tables(true)).DataTable()
.columns.adjust();
});
});
</script>
I got this popup:
DataTables warning: table id=example1-tab1-dt - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3 [1]: https://i.stack.imgur.com/RCMmB.png