Issue :
- The data in the table body is misaligned with the headers of each column.
Solution :
var test_insert1 = '<tr>' +
'<td class="td-trad-9">762261</td>' +
'<td class="td-trad-7">1.16176</td>' +
'<td class="td-trad-8">1.1618</td>' +
'<td class="td-trad-1">2018-08-08</td>' +
'<td class="td-trad-2">03:32</td>' +
'<td class="td-trad-3">This is a test long value</td>' +
'<td class="td-trad-4">1.00</td>' +
'<td class="td-trad-5">Up</td>' +
'<td class="td-trad-6">0.80</td>' +
'</tr>'+
'<tr>' +
'<td class="td-trad-9">456985</td>' +
'<td class="td-trad-7">1.65476</td>' +
'<td class="td-trad-8">1984218</td>' +
'<td class="td-trad-1">2018-08-08</td>' +
'<td class="td-trad-2">03:32</td>' +
'<td class="td-trad-3">This is a test value</td>' +
'<td class="td-trad-4">10000</td>' +
'<td class="td-trad-5">Up</td>' +
'<td class="td-trad-6">1.00</td>' +
'</tr>';
$('#add').click(function() {
$('#new_table_test_body').html(test_insert1);
});
#new_table_test {
width: auto;
table-layout: fixed;
border-collapse: collapse;
/* color:white; */
text-align: center;
vertical-align: middle;
}
#new_table_test tbody::-webkit-scrollbar {
width: 0px;
/* remove scrollbar space */
background: transparent;
/* optional: just make scrollbar invisible */
}
#new_table_test tbody {
display: block;
width: 100%;
overflow: auto;
height: 100px;
}
#new_table_test thead tr {
display: block;
}
#new_table_test thead {
background: black;
color: #fff;
}
#new_table_test th,
#new_table_test td {
padding: 5px;
text-align: left;
width: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="new_table_test">
<thead id="new_table_test_header">
<tr>
<th>Header 1</th>
<th>Header Two</th>
<th>Head Three</th>
<th>H4</th>
<th>Header 5</th>
<th>Heads 6</th>
<th>Header seven</th>
<th>Header 8</th>
<th>Header Nine</th>
</tr>
</thead>
<tbody id="new_table_test_body"></tbody>
</table>
<button id="add"> Add Details </button>
-- EDIT --
Additional:
Specifications:
- The table body can be scrolled
- Headers remain fixed while scrolling
- Width adjusts automatically based on content
Check out an updated fiddle for more details on this problem: http://jsfiddle.net/9sjyvb5w/50