我的代码如下。尝试为DIV创建一个垂直滚动条,并使我的表头固定,这是有效的。的 FIDDLE
但是,table2的第一列包含动态数据,并且相对于正在填充的数据调整此单元格的宽度。
我需要将head1宽度(来自table1)设置为等于table2 column1的宽度。
希望有一种简单而更好的方法来实现这个目标
<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>
答案 0 :(得分:2)
对于小黑客,我假设您知道列中最长的字符串是什么,如果是这样,您可以将其放到跨度中,它将&#34;展开&#34;你的标题,但你会隐藏它。
您可以使用javascript实现它,搜索特定列中最长的文本,并将其放在该列的开头。
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);
});
&#13;
thead th div {
height: 1em;
overflow: hidden;
text-align: left;
}
thead th span {
font-weight: normal;
display: block;
}
&#13;
<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>
&#13;
答案 1 :(得分:0)
您可以为单元格(td
和th
)指定宽度设置:
table {
width: 100%;
}
th, #test td {
width: 33%;
text-align: left;
}
https://jsfiddle.net/9edzy0j0/1/
如果第一行应该总是比其他行宽,你可以使用像
这样的东西th, td {
width: 25%;
}
th:first-child, td:first-child {
width: 50%;
}