[英]Table with scrollable rows and fixed header
I have this code based on this solution , but it does not seem to work for me. 我有基于此解决方案的 代码 ,但它似乎对我不起作用。
I would like to be able to : 我希望能够:
TD
row. TD
相同。 I am quite new to JavaScript, so I do not know what I am missing. 我对JavaScript非常陌生,所以我不知道自己缺少什么。 The scroll part is working, but not in jsfiddle.
滚动部分正在工作,但不在jsfiddle中。 How can I make the
TH
the same width as the TD
? 如何使
TH
与TD
宽度相同?
EDIT 1 : 编辑1:
Code 码
<html>
<head>
<style>
.outerDIV {
position: relative;
padding-top: 30px; //height of your thead
}
.innerDIV {
overflow: auto;
max-height: 200; //the actual scrolling container
}
table thead {
display: block;
position: absolute;
top: 0;
left: 0;
}
table tbody {
display: block;
}
</style>
<script>
function scrollingTableSetThWidth(tableId)
{
var table = document.getElementById(tableId);
ths = table.getElementsByTagName('th');
tds = table.getElementsByTagName('td');
if(ths.length > 0) {
for(i=0; i < ths.length; i++) {
ths[i].style.width = getCurrentComputedStyle(tds[i], 'width');
}
}
}
function getCurrentComputedStyle(element, attribute)
{
var attributeValue;
if (window.getComputedStyle)
{ // class A browsers
var styledeclaration = document.defaultView.getComputedStyle(element, null);
attributeValue = styledeclaration.getPropertyValue(attribute);
} else if (element.currentStyle) { // IE
attributeValue = element.currentStyle[vclToCamelCases(attribute)];
}
return attributeValue;
}
</script>
</head>
<body>
<div class="outerDIV">
<div class="innerDIV">
<table border="1">
<thead>
<tr>
<div><th>Column1</th><th>Column2</th><th>Column3</th></div>
</tr>
</thead>
<tbody>
<tr>
<td>Line1Cell1</td><td>Line1Cell2</td><td>Line1Cell3</td>
</tr>
<tr>
<td>Line2Cell1</td><td>Line2Cell2</td><td>Line2Cell3</td>
</tr>
<tr>
<td>Line3Cell1</td><td>Line3Cell2</td><td>Line3Cell3</td>
</tr>
<tr>
<td>Line4Cell1</td><td>Line4Cell2</td><td>Line4Cell3</td>
</tr>
<tr>
<td>Line5Cell1</td><td>Line5Cell2</td><td>Line5Cell3</td>
</tr>
<tr>
<td>Line6Cell1</td><td>Line6Cell2</td><td>Line6Cell3</td>
</tr>
<tr>
<td>Line7Cell1</td><td>Line7Cell2</td><td>Line7Cell3</td>
</tr>
<tr>
<td>Line8Cell1</td><td>Line8Cell2</td><td>Line8Cell3</td>
</tr>
<tr>
<td>Line9Cell1</td><td>Line9Cell2</td><td>Line9Cell3</td>
</tr>
<tr>
<td>Line10Cell1</td><td>Line10Cell2</td><td>Line10Cell3</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
The reason your scrolling isn't working is because you don't have any units on your .innerDiv max-height property. 滚动不起作用的原因是.innerDiv max-height属性中没有任何单位。
For matching the width of the header with the table, you don't need javascript if you can set the width of the <th>
and <td>
cells via CSS. 为了使标题的宽度与表格匹配,如果可以通过CSS设置
<th>
和<td>
单元格的宽度,则不需要javascript。
See this: http://jsfiddle.net/MVxZb/10/ 看到这个: http : //jsfiddle.net/MVxZb/10/
EDIT: updated link. 编辑:更新的链接。 forgot to delete the unnecessary javascript.
忘记删除不必要的JavaScript。
your problem is not javascript is html you need to separate your header and your table in 2 different containers not have the headers grouped under the sam container your tbody so basically you can do 您的问题不是javascript,而是html,您需要将标头和表格分别放在2个不同的容器中,而标头没有分组在sam容器下,所以基本上可以
<div style="width:200px">
<table style="width:100%">
<tr><th>header</th></tr>
</table>
<div>
<div style="width:200px; height:100px; overflow:auto">
<table style="width:100%">
<tr>
<td>value</td>
</tr>
</table>
<div>
like demostrated here 像这里展示的
You need to give a width
to the element set in absolute
position. 您需要为元素设置
absolute
位置的width
。
For a better looking the width should ~1 em smaller (average width of scrollbar). 为了更好看,宽度应减小约1 em(滚动条的平均宽度)。
If all columns spread equally in width, you may do something like this : http://jsfiddle.net/MVxZb/9/ 如果所有列的宽度均等分布,则可以执行以下操作: http : //jsfiddle.net/MVxZb/9/
.outerDIV {
position: relative;
padding-top: 30px;
display:inline-block;
}
.innerDIV {
overflow: auto;
max-height: 150px;
padding-right:1.1em;
margin-right:-1.1em;
}
table thead {
display:table;
width:100%;
border:solid 1px gray;
box-sizing:border-box;
position: absolute;
border-bottom:0;
top: 0;
left: 0;
right:0;
}
you can use table-layout:fixed;
您可以使用
table-layout:fixed;
to spread even width to your columns, if th and tds have no width set. 如果th和tds没有设置宽度,则将宽度均匀地分布到您的列。
Whenever I needed to keep the header of a table fixed I used this. 每当我需要保持表头固定时,我都会使用它。 I'd appreciate other solutions as well.
我也很感谢其他解决方案。
table th,
table td {
padding: 10px;
width: 100px;
}
.fixed-header{
position: fixed;
background: #fff;
}
.container{
height: 402px;
overflow: auto;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.