Reputation: 33
My code below. Tried creating a vertical scroll bar for DIV and also to make my table header fixed which is working. FIDDLE
However, the first column of table2 has dynamic data and the width of this cell gets adjusted with respect to the data that is being populated.
I need to set my Head1 width (from table1) equivalent to the width of table2 column1.
Hoping for a simple and better approach to achieve this
<table id="table1">
<thead>
<tr>
<th>Head1</th>
<th>Head2</th>
<th>Head3</th>
</tr>
</thead>
</table>
<div style="max-height: 200px; overflow: auto;">
<table id="table2">
<tbody>
<tr> <td>Contains Dynamic data</td> <td>content</td> <td>content</td> </tr>
<tr> <td>content</td> <td>content</td> <td>content</td> </tr>
<tr> <td>content</td> <td>content</td> <td>content</td> </tr>
<tr> <td>content</td> <td>content</td> <td>content</td> </tr>
</tbody>
</table>
</div>
Upvotes: 0
Views: 813
Reputation: 35493
With small hack, I assume that you know what is the longest string in the column, if so, you can put it to the span, it will "expand" your header, but you will hide it.
You can achieve it using javascript, search for the longest text in a specific column, and put it to the head of that column.
const headerContainers = document.querySelectorAll('#head-row div');
const maxLengthPerColumn = Array.from(headerContainers).map(_ => '');
Array.from(document.querySelectorAll('#test tr'))
.forEach((row) => Array.from(row.querySelectorAll('td')).forEach((td, index) => {
maxLengthPerColumn[index] = maxLengthPerColumn[index].length < td.textContent.length ? td.textContent : maxLengthPerColumn[index];
}));
maxLengthPerColumn.forEach((longText, index) => {
const span = document.createElement('span');
span.textContent = longText;
headerContainers[index].appendChild(span);
});
thead th div {
height: 1em;
overflow: hidden;
text-align: left;
}
thead th span {
font-weight: normal;
display: block;
}
<div>Page content goes here. </div>
<table>
<thead>
<tr id="head-row">
<th>
<div>Head1</div>
</th>
<th>
<div>Head2</div>
</th>
<th>
<div>Head3</div>
</th>
</tr>
</thead>
</table>
<div style="max-height: 200px; overflow: auto;">
<table id="test">
<tbody>
<tr>
<td>Contains Dynamic data</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content Longer text</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
</tbody>
</table>
</div>
<div>More Page Content.
<br/>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque
rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam
nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris
sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc</div>
Upvotes: 2
Reputation: 67748
You can assign a width setting to the cells (both td
and th
):
table {
width: 100%;
}
th, #test td {
width: 33%;
text-align: left;
}
https://jsfiddle.net/9edzy0j0/1/
And in case the first row should always be wider than the others, you can use something like
th, td {
width: 25%;
}
th:first-child, td:first-child {
width: 50%;
}
Upvotes: 0