实现 DataGrid表头不动,表身滚动 的方法

本文介绍了一种使表格头部在滚动时保持可见的技术方案。通过JavaScript和CSS表达式实现了固定头部的效果,确保用户在浏览长表格时始终能看到列标题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用纯脚本 实现:

 

  function ScrollDataGrid() 
        

            var tb 
= document.getElementById("tbHeader"); 
            var dg 
= document.getElementById("dgSearchResult"); 
             
            
if(dg!= null
            
{     
                var dv 
= document.getElementById("dvBody"); 
                var th 
= dg.rows[0]; 
                
for(var i = 0 ; i < th.cells.length;i++
                    th.cells[i].width 
= th.cells[i].clientWidth; 
                var tr 
= th.cloneNode(true); 
                 
                
//tr.applyElement(tb); 
                tb.createTHead(); 
                var tbh 
= tb.tHead; 
                tbh.appendChild(tr); 
                tr 
= tbh.rows[0]; 
                var td 
= tr.insertCell(); 
                td.style.width 
= 19
                td.width 
= 19
                td.innerHTML 
= tr.cells[0].innerHTML; 
                th.style.display 
= 'none'
                
if(dg.scrollHeight < 300 ) 
                    dv.style.height 
= dg.scrollHeight * 1 + 2
                 
                 
            }
 
    <table width="100%" border="0" id="tbHeader" cellpadding="4" cellspacing="0"> 
                                        
</table> 
                                        
<div style="OVERFLOW-Y: scroll; HEIGHT: 300px" id="dvBody"> 
                                            
<asp:datagrid id="dgSearchResult" style="BORDER-COLLAPSE: collapse" runat="server" Width="100%" CellPadding="4" DataKeyField="编号" BorderWidth="1px" BorderStyle="Solid" BorderColor="RoyalBlue" GridLines="None"> 
                                                
<SelectedItemStyle ForeColor="SlateGray"></SelectedItemStyle> 
                                                
<ItemStyle VerticalAlign="Middle"></ItemStyle> 
                                                
<HeaderStyle HorizontalAlign="Center"></HeaderStyle> 
                                                
<Columns> 
                                                    
<asp:ButtonColumn Text="选择" HeaderText="&lt;font face=webdings&gt;6&lt;/font&gt;" CommandName="Delete"> 
                                                        
<HeaderStyle Wrap="False" Width="40px"></HeaderStyle> 
                                                        
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle> 
                                                        
<FooterStyle Wrap="False"></FooterStyle> 
                                                    
</asp:ButtonColumn> 
                                                
</Columns> 
                                            
</asp:datagrid></div> 

使用Style-Expression实现:

 

<style> 
TD.lockedhead 
{ POSITION: relative; ; TOP: expression(lockhead(this)) } 
</style> 
<script language="javascript"> 
function lockhead(cell) 

var table
=cell.parentElement.parentElement; 
while(table&&table.tagName.toLowerCase()!="table"
table
=table.parentElement; 
var _div
=table.parentElement; 
return _div.scrollTop+(cell.parentElement.rowIndex)*(parseInt(table.border)+parseInt(table.cellSpacing)-1); 
}
 
</script> 
<script language="javascript"> 
function LockHead(table,cnt) 


if(table&&table.rows.length>=cnt) 

for(k=0;k<cnt;k++

for(i=0;i<table.rows[k].cells.length;i++

table.rows[k].cells[i].className
="lockedhead"
}
 
}
 
}
 
}
 
LockHead(Form1.all.DataGrid1,
1); 


</script>

还有一种:

 

================ 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> New Document </TITLE> 
<META NAME="Generator" CONTENT="EditPlus"> 
<META NAME="Author" CONTENT=""> 
<META NAME="Keywords" CONTENT=""> 
<META NAME="Description" CONTENT=""> 
</HEAD> 

<BODY> 
<div style="overflow:auto;height:100px;"> 
<table width="100%"> 
<TR style="top:expression(this.offsetParent.scrollTop);background:navy;color:white;"> 
<TH nowrap>Header A</TH> 
<TH nowrap>Header B</TH> 
<TH nowrap>Header C</TH> 
</TR> 
<TR> 
<TD>A</TD> 
<TD>B</TD> 
<TD>C</TD> 
</TR> 
<TR> 
<TD>A</TD> 
<TD>B</TD> 
<TD>C</TD> 
</TR> 
<TR> 
<TD>A</TD> 
<TD>B</TD> 
<TD>C</TD> 
</TR> 
<TR> 
<TD>A</TD> 
<TD>B</TD> 
<TD>C</TD> 
</TR> 
<TR> 
<TD>A</TD> 
<TD>B</TD> 
<TD>C</TD> 
</TR> 
<TR> 
<TD>A</TD> 
<TD>B</TD> 
<TD>C</TD> 
</TR> 
<TR> 
<TD>A</TD> 
<TD>B</TD> 
<TD>C</TD> 
</TR> 
<TR> 
<TD>A</TD> 
<TD>B</TD> 
<TD>C</TD> 
</TR> 
<TR> 
<TD>A</TD> 
<TD>B</TD> 
<TD>C</TD> 
</TR> 
</table> 
</div> 
</BODY> 
</HTML>

这个是css:
.DataGridFixedHeader {
position: relative;
top: expression(this.offsetParent.scrollTop);
background-color: blue
}

这个是应用:
...
<HeaderStyle Font-Bold="True" CssClass="ms-formlabel DataGridFixedHeader" BackColor="#80AFEF">
</HeaderStyle>
...


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

east_liujie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值