基于IE与css的表格行头与多层列头锁定方法

来源:互联网 发布:mac边充电边用好吗 编辑:程序博客网 时间:2024/05/13 18:40

(左边图锁定了行头与列头,右边图仅锁定列头)

有许多的锁定或固定html表格表头的方法和技术,本文介绍的是一个基于IE和css的简易实现方法,基本思路为:使用div行级元素确定一个包含表格的区域,在区域滚动移动时定位非固定单元格的位置(top/left)。

1、确定表格区域

定义一个有边框的div元素,并设置其overfloaw属性为scroll,保证该区域恒有一个垂直与水平滚动条。

div#DivContainer
{
overflow: scroll; 
border: solid 1px gray;
}

在div中嵌入一个表格,设置collapse属性为collapse,满足单边框(合并边框)的外观。

table

border-collapse: collapse;
}

2、单元格锁定选择器类

需要设计三种类型的锁定单元格:垂直方向锁定单元格(VLocked)、水平方向锁定单元格(HLocked)及双向锁定单元格(Locked),而一般表格内容单元格则设置其位置属性position为relative,见如下css代码:

td  

position: relative; 

padding: 5px; 

border-top: solid 0px gray; 

border-bottom: solid 1px gray; 

border-left: solid 0px gray; 

border-right: solid 1px gray; 

}

锁定表格行头时,要求行头的单元格水平方不移动,即区域移动时重定位这些行头单元格的left边界值,见如下css代码:

td.HLocked /* 水平方向锁住单元格 */

{

z-index: 10; 

position: relative; 

left: expression(parentNode.parentNode.parentNode.parentNode.scrollLeft); 

background-color: #cccccc; 

text-align: center; 

border-top: solid 0px gray; 

border-bottom: solid 1px gray; 

border-left: solid 0px gray; 

border-right: solid 1px gray;

}

需要指出,表格td元素的parentNode元素是tr,tr.parentNode元素是tbody,tbody.parentNode元素是table,所以td.parentNode.parentNode.parentNode.parentNode是定制的div元素,即当前表格区域。

锁定表格列头时,需要考虑两种情况单元格。一种是不锁定行头的单元格,此时只需要垂直方向不移动即可,见如下css代码:

td.VLocked

{

z-index: 20; 

position: relative; 

top: expression(parentNode.parentNode.parentNode.parentNode.scrollTop); 

font-size: 10pt; 

color: black; 

background-color: #cccccc; 

text-align: center; 

border-top: solid 0px gray; 

border-bottom: solid 1px gray; 

border-left: solid 0px gray; 

border-right: solid 1px gray;

}

另一种是锁定行头单元格时,这些是行头又是列头的单元格必须双向锁定,见如下css代码:

td.Locked

{

z-index: 30; 

position: relative; 

top: expression(parentNode.parentNode.parentNode.parentNode.scrollTop); 

left: expression(parentNode.parentNode.parentNode.parentNode.scrollLeft); 

font-size: 10pt; 

color: black; 

background-color: #cccccc; 

text-align: center; 

border-top: solid 0px gray; 

border-bottom: solid 1px gray; 

border-left: solid 0px gray; 

border-right: solid 1px gray;

}

还有两点需要说明:

  1. 使用了z-index坐标,并且是Locked类选择器的z-index值最大,即最靠近浏览者;
  2. 在aspx中使用时,需要删除Visual Studio自动产生的<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">行。

下面是完整的css代码和htm演示代码,测试时需要建立一个IIS虚拟路径,拷贝css文件和htm文件到该虚拟路径中,然后通过浏览器浏览htm文件:

 


  1. div#DivContainer   
  2. {   
  3.     overflowscrollbordersolid 1px gray;   
  4. }   
  5. table    
  6. {   
  7.     border-collapsecollapse;    
  8. }   
  9. td    
  10. {   
  11.     positionrelativepadding5px;   
  12.     border-topsolid 0px grayborder-bottomsolid 1px grayborder-leftsolid 0px grayborder-rightsolid 1px gray;   
  13. }   
  14. td.Locked /* 水平与垂直方向锁住单元格, 不随鼠标或滚动条移动 */  
  15. {   
  16.     z-index30positionrelative;    
  17.     top: expression(parentNode.parentNode.parentNode.parentNode.scrollTop);    
  18.     left: expression(parentNode.parentNode.parentNode.parentNode.scrollLeft);    
  19.     background-color#cccccctext-aligncenter;    
  20.     border-topsolid 0px grayborder-bottomsolid 1px grayborder-leftsolid 0px grayborder-rightsolid 1px gray;   
  21. }   
  22. td.HLocked /* 水平方向锁住单元格 */  
  23. {   
  24.     z-index10positionrelative; left: expression(parentNode.parentNode.parentNode.parentNode.scrollLeft);    
  25.     background-color#cccccctext-aligncenter;    
  26.     border-topsolid 0px grayborder-bottomsolid 1px grayborder-leftsolid 0px grayborder-rightsolid 1px gray;   
  27. }   
  28. td.VLocked /* 垂直方向锁住单元格 */  
  29. {   
  30.     z-index20positionrelative; top: expression(parentNode.parentNode.parentNode.parentNode.scrollTop);    
  31.     background-color#cccccctext-aligncenter;    
  32.     border-topsolid 0px grayborder-bottomsolid 1px grayborder-leftsolid 0px grayborder-rightsolid 1px gray;   
  33. }  

 

  1. <html xmlns="http://www.w3.org/1999/xhtml" >  
  2. <head>  
  3.        
  4. <title>多层表头测试</title>   
  5. <style type="text/css">    
  6.     tr { height: 26px; }   
  7.     td         { font-size: 10pt; text-align: right; }   
  8.     td.Locked  { font-size: 10pt; }   
  9.     td.HLocked { font-size: 10pt; }   
  10.     td.VLocked { font-size: 10pt; }   
  11. </style>  
  12. </head>  
  13. <body>  
  14. <div id="DivContainer" style="width: 400px; height: 260px;">  
  15. <table id = "Report">  
  16.   <tr>  
  17.     <td class="Locked" colspan="4">Column1</td>  
  18.        
  19.     <td class="VLocked" rowspan="3">Column5</td>  
  20.     <td class="VLocked" rowspan="3">Column6</td>  
  21.     <td class="VLocked" rowspan="3">Column7</td>  
  22.     <td class="VLocked" rowspan="3">Column8</td>  
  23.   </tr>  
  24.   <tr>  
  25.     <td class="Locked" colspan="2">Column11</td>  
  26.     <td class="Locked" colspan="2">Column12</td>  
  27.   </tr>  
  28.   <tr>  
  29.     <td class="Locked">Column111</td>  
  30.     <td class="Locked">Column112</td>  
  31.     <td class="Locked">Column121</td>  
  32.     <td class="Locked">Column122</td>  
  33.   </tr>  
  34. <!--上面是表列头部分 -->  
  35.   <tr>  
  36.     <td class="HLocked">Row01</td>  
  37.     <td class="HLocked">Row01A</td>  
  38.     <td class="HLocked">Row01B</td>  
  39.     <td class="HLocked">11</td>  
  40.        
  41.     <td style="text-align: center">12</td>  
  42.     <td style="text-align: left">13</td>  
  43.     <td style="text-align: right">14</td>  
  44.     <td>15</td>  
  45.   </tr>  
  46.   <tr>  
  47.     <td class="HLocked">Row02</td>  
  48.     <td class="HLocked">Row02A</td>  
  49.     <td class="HLocked">Row02B</td>  
  50.     <td class="HLocked">21</td>  
  51.     <td>22</td>  
  52.     <td>23</td>  
  53.     <td>24</td>  
  54.     <td>25</td>  
  55.   </tr>  
  56.   <tr>  
  57.     <td class="HLocked">Row03</td>  
  58.     <td class="HLocked">Row03A</td>  
  59.     <td class="HLocked">Row03B</td>  
  60.     <td class="HLocked">31</td>  
  61.     <td>32</td>  
  62.     <td>33</td>  
  63.     <td>34</td>  
  64.     <td>35</td>  
  65.   </tr>  
  66.   <tr>  
  67.     <td class="HLocked">Row04</td>  
  68.     <td class="HLocked">Row04A</td>  
  69.     <td class="HLocked">Row04B</td>  
  70.     <td class="HLocked">41</td>  
  71.     <td>42</td>  
  72.     <td>43</td>  
  73.     <td>44</td>  
  74.     <td>45</td>  
  75.   </tr>  
  76.   <tr>  
  77.     <td class="HLocked">Row05</td>  
  78.     <td class="HLocked">Row05A</td>  
  79.     <td class="HLocked">Row05B</td>  
  80.     <td class="HLocked">51</td>  
  81.     <td>52</td>  
  82.     <td>53</td>  
  83.     <td>54</td>  
  84.     <td>55</td>  
  85.   </tr>  
  86.   <tr>  
  87.     <td class="HLocked">Row06</td>  
  88.     <td class="HLocked">Row06A</td>  
  89.     <td class="HLocked">Row06B</td>  
  90.     <td class="HLocked">61</td>  
  91.     <td>62</td>  
  92.     <td>63</td>  
  93.     <td>64</td>  
  94.     <td>65</td>  
  95.   </tr>  
  96.   <tr>  
  97.     <td class="HLocked">Row07</td>  
  98.     <td class="HLocked">Row07A</td>  
  99.     <td class="HLocked">Row07B</td>  
  100.     <td class="HLocked">71</td>  
  101.     <td>72</td>  
  102.     <td>73</td>  
  103.     <td>74</td>  
  104.     <td>75</td>  
  105.   </tr>  
  106.   <tr>  
  107.     <td class="HLocked">Row08</td>  
  108.     <td class="HLocked">Row08A</td>  
  109.     <td class="HLocked">Row08B</td>  
  110.     <td class="HLocked">81</td>  
  111.     <td>82</td>  
  112.     <td>83</td>  
  113.     <td>84</td>  
  114.     <td>85</td>  
  115.   </tr>  
  116.   <tr>  
  117.     <td class="HLocked">Row09</td>  
  118.     <td class="HLocked">Row09A</td>  
  119.     <td class="HLocked">Row09B</td>  
  120.     <td class="HLocked">91</td>  
  121.     <td>92</td>  
  122.     <td>93</td>  
  123.     <td>94</td>  
  124.     <td>95</td>  
  125.   </tr>  
  126. </table>  
  127. </div>  
  128. </body>  
  129. </html>
原创粉丝点击