JS-Demo2:JavaScript版TableGrid,表格头、分页表格冻结,表格头可拉动

上两天写了一个关于表格头可以拖动:JS-Demo1:JavaScript实现表格列拖动

这次在拖动的效果上,实现表格头冻结等。上图:


代码比较长,感兴趣的朋友,不妨复制看看微笑

代码:

[html]  view plain copy
  1.    
  2.    
  3.    
  4.    
  5.    
  6. <!DOCTYPE html>  
  7. <html>  
  8.   <head>  
  9.     <title>表格冻结</title>  
  10.     <style type="text/css">  
  11.         table{  
  12.             color: #000;  
  13.             font: normal 12px 宋体,tahoma,arial,verdana,sans-serif;  
  14.             table-layout:fixed;  
  15.         }  
  16.         #theadTable,#tfootTable tr{  
  17.             background-color: #CBDDF3;  
  18.             border: 1px solid #7BBDFF;  
  19.         }  
  20.         td,th{  
  21.             white-space: nowrap;  
  22.             overflow:hidden;   
  23.         }  
  24.         td{  
  25.             line-height: 16px;  
  26.         }  
  27.         .dragable{  
  28.             width: 3px;  
  29.             height:100%;   
  30.             cursor: col-resize;  
  31.             /*下面3个样式与效果有一定关系,其他无所谓*/  
  32.             float: right;    
  33.             position: relative;  
  34.             right: 0px;  
  35.         }  
  36.     </style>  
  37.     <!--  
  38.     <script type="text/javascript" src="/jxc/js/jQuery/jquery-1.7.2.js"></script> 
  39.      -->  
  40.       
  41.     <script type="text/javascript">  
  42.     /***************************************************************************/  
  43.     /**  
  44.      * 为了方便看效果,写了个jQuery的简单实现,实际应用中,可以去掉这个,而引用jquery.js,无影响。  
  45.      */  
  46.     function jQuery(expression){  
  47.         expression = expression.substring(1);  
  48.         this[0] = document.getElementById(expression);  
  49.         this.val = function(v){  
  50.             if(v!=undefined) this[0].value = v;  
  51.             else return this[0].value;  
  52.         }  
  53.         return this;  
  54.     }  
  55.     window.$ = jQuery;  
  56.     /***************************************************************************/  
  57.       
  58.     var draging = false;            //是否拖拽中  
  59.     var dragElement = null;     //当前拖拽的th  
  60.     var offsetLeft = 0;             //当前拖拽的th与绝对左坐标  
  61.     var verticalLine = undefined;   //竖线  
  62.     var draggedWidth = 0;           //拖拽时th的宽度,当鼠标mouseup时,th的宽度为该值  
  63.     function mousedown(){  
  64.         if(draging) return;  
  65.         draging = true;  
  66.         dragElement = window.event.srcElement.parentNode;  
  67.         dragElement.theadtd = theadTable.rows(0).cells(dragElement.cellIndex);  
  68.         dragElement.tfoottd = tfootTable.rows(0).cells(dragElement.cellIndex);  
  69.         dragElement.tbodytd = {style:{}};//当表格中没有数据时,赋值这么一个对象,不引起js报错。  
  70.         if(tbodyTable.rows.length>0)//当表格中有数据时  
  71.             dragElement.tbodytd = tbodyTable.rows(0).cells(dragElement.cellIndex);  
  72.           
  73.         offsetLeft = dragElement.offsetLeft;  
  74.         document.body.style.cursor = "col-resize";  
  75.         document.body.onselectstart = function(){return false;};//拖拽的时候,鼠标滑过字的时候,不让选中(默认鼠标选中字的效果,大家都知道)  
  76.         if(verticalLine==undefined){  
  77.             verticalLine = createVerticalLine();  
  78.         }  
  79.     }  
  80.     function mouseup(){  
  81.         if(!draging) return;  
  82.         draging = false;  
  83.         document.body.style.cursor = "auto";  
  84.         document.body.onselectstart = function(){return true};  
  85.         verticalLine.style.display = "none";  
  86.         dragElement.style.width = draggedWidth;  
  87.         dragElement.theadtd.style.width = draggedWidth;  
  88.         dragElement.tbodytd.style.width = draggedWidth;  
  89.         dragElement.tfoottd.style.width = draggedWidth;  
  90.         dragElement = null;  
  91.     }  
  92.     function mousemove(){  
  93.         if(draging && dragElement){  
  94.             if(event.clientX-offsetLeft>0){  
  95.                 draggedWidth = event.clientX-offsetLeft;  
  96.                 verticalLine.style.left = event.clientX+8;  
  97.                 verticalLine.style.display = "block";  
  98.             }  
  99.         }  
  100.     }  
  101.    
  102.     //创建触发拖动表格列的span:<span class="dragable" onmousedown="mousedown();"> </span>  
  103.     function createSpan(){  
  104.         var span = document.createElement("span");  
  105.         span.className = "dragable";  
  106.         span.attachEvent("onmousedown",mousedown);  
  107.         span.innerHTML = " ";  
  108.         return span;  
  109.     }  
  110.    
  111.     //创建拖动时的效果(竖线):<div style="z-index: 10; position: absolute; background-color: #c3c3c3; width: 6px; display: none; height: ?px; top: 24px; left: 608px"></div>  
  112.     function createVerticalLine(){  
  113.         var div = document.createElement("div");  
  114.         div.style.position = "absolute";  
  115.         div.style.display = "none";  
  116.         div.style.backgroundColor = "#C3C3C3";  
  117.         div.style.width = "6px";  
  118.         div.style.height = tbodyDiv.style.height;  
  119.         div.style.top = tbodyTable.offsetTop;  
  120.         div.style.zIndex = "10";  
  121.         div.innerHTML = " ";  
  122.         document.body.appendChild(div);  
  123.         return div;  
  124.     }  
  125.     var mainDiv;  
  126.     var theadDiv,tbodyDiv,tfootDiv;  
  127.     var theadTable,tbodyTable,tfootTable;  
  128.    
  129.     window.onload = function(){  
  130.         mainDiv = $("#mainDiv")[0];  
  131.         theadDiv = $("#theadDiv")[0];  
  132.         tbodyDiv = $("#tbodyDiv")[0];  
  133.         tfootDiv = $("#tfootDiv")[0];  
  134.         theadTable = $("#theadTable")[0];  
  135.         tbodyTable = $("#tbodyTable")[0];  
  136.         tfootTable = $("#tfootTable")[0];  
  137.           
  138.         /* dragable 效果 */  
  139.         //在theadTable中的th内容中添加span  
  140.         var theadThs = theadTable.getElementsByTagName("th");  
  141.         for(var i=0;i<theadThs.length;i++)  
  142.             theadThs[i].appendChild(createSpan());  
  143.           
  144.         theadTable.style.width = tbodyTable.style.width = tfootTable.style.width = Math.max(theadTable.offsetWidth,tbodyTable.offsetWidth,tfootTable.offsetWidth);  
  145.           
  146.         var ths = theadTable.rows(0).getElementsByTagName("th");  
  147.         var tfs = tfootTable.rows(0).getElementsByTagName("td");  
  148.         var tds;//当表格中没有数据时,赋值这么一个对象,不引起js报错。  
  149.         if(tbodyTable.rows.length>0){  
  150.             tds = tbodyTable.rows(0).getElementsByTagName("td");  
  151.         }else{  
  152.             tds = new Array();  
  153.             for(var i=0;i<ths.length;i++){  
  154.                 tds[tds.length] = {style:{},offsetWidth:0};  
  155.             }  
  156.         }  
  157.         if(ths.length!=tds.length && tds.length!=tfs.length){  
  158.             alert("长度不一致");  
  159.         }  
  160.         for(var i=0;i<ths.length;i++){  
  161.             var width;  
  162.             if(ths[i].style.width) width = ths[i].style.width;  
  163.             else if(ths[i].width) width = ths[i].width;  
  164.             else width = Math.max(ths[i].offsetWidth,tds[i].offsetWidth,tfs[i].offsetWidth);  
  165.             if(i!=ths.length-1)  
  166.                 ths[i].style.width = tds[i].style.width = tfs[i].style.width = width;  
  167.         }  
  168.     }  
  169.    
  170.     /**  
  171.      * 当tfootdiv横向滚动时,theadDiv与tbodyDiv同时滚动。  
  172.      * @param tfootDiv  
  173.      */  
  174.     function togetherScroll(scrollingDiv){  
  175.         theadDiv.scrollLeft = scrollingDiv.scrollLeft;  
  176.         tbodyDiv.scrollLeft = scrollingDiv.scrollLeft;  
  177.         tfootDiv.scrollLeft = scrollingDiv.scrollLeft;  
  178.     }  
  179.     </script>  
  180. </head>  
  181. <body onmousemove="mousemove();" onmouseup="mouseup();">  
  182.     <span style="font-weight: bold;font-size: 14px;">  
  183.     JavaScript版TableGrid说明:<br>  
  184.     1、暂时不支持表格跨行跨列<br>  
  185.     2、暂时不支持表格冻结列<br>  
  186.     </span>  
  187.     <div id="mainDiv" style="height: 500px;width: 100%;">  
  188.         <div id="theadDiv" style="width:100%;height:16px;overflow-y:scroll;overflow-x:hidden;">  
  189.             <table id="theadTable" border="1" cellpadding="0" cellspacing="0" style="width:100%;height:16px;border-style: solid;">  
  190.                 <thead>  
  191.                     <tr>  
  192.                         <th style="width: 120px;">网站名称</th>  
  193.                         <th style="width: 300px;">网站地址</th>  
  194.                         <th width="50px;">用户名</th>  
  195.                         <th width="50px;">密码</th>  
  196.                         <th width="180px;">邮箱</th>  
  197.                         <th>手机号码</th>  
  198.                         <th>备注</th>  
  199.                         <th>操作</th>  
  200.                     </tr>  
  201.                 </thead>  
  202.             </table>  
  203.         </div>  
  204.         <div id="tbodyDiv" style="width:100%;height:444px;overflow-y:scroll;overflow-x:hidden;background-color: #FAFAFA;" onscroll="togetherScroll(this)">  
  205.             <table id="tbodyTable" border="1" cellpadding="0" cellspacing="0" style="width:100%;height:100%;border-style: solid;">  
  206.                 <tbody id="content">  
  207.                     <!--   
  208.                         <tr>  
  209.                             <td>Siuon's CSDN Blog</td>  
  210.                             <td><a href="http://blog.csdn.net/xiaochunyong" target="_blank">http://blog.csdn.net/xiaochunyong</a></td>  
  211.                             <td>Siuon</td>  
  212.                             <td>123456</td>  
  213.                             <td>xiaochunyong@gmail.com</td>  
  214.                             <td>1862152####</td>  
  215.                             <td>...</td>  
  216.                             <td><a href="javascript:alert('修改');">修改</a> <a href="javascript:alert('删除');">删除</a></td>  
  217.                         </tr>  
  218.                      -->  
  219.                 </tbody>  
  220.             </table>  
  221.         </div>  
  222.         <div id="tfootDiv" style="width:100%;height:40px;overflow-y:scroll;overflow-x:scroll;" onscroll="togetherScroll(this)">  
  223.             <table id="tfootTable" border="1" cellpadding="0" cellspacing="0" style="width:100%;height:16px;border-style: solid;">  
  224.                 <tfoot>  
  225.                     <tr>  
  226.                         <td> </td>  
  227.                         <td> </td>  
  228.                         <td> </td>  
  229.                         <td style="text-align: right;">第一页 上一页</td>  
  230.                         <td style="text-align: center;align:justify"><input type="text" style="width: auto;"><button type="button">Go</button></td>  
  231.                         <td style="text-align: left;">下一页 最后一页</td>  
  232.                         <td> </td>  
  233.                         <td>1 - 30 共 30 条</td>  
  234.                     </tr>  
  235.                 </tfoot>  
  236.             </table>  
  237.         </div>  
  238.     </div>  
  239.     <script type="text/javascript">  
  240.         //动态生成30行数据,可以把如下代码删除,以便测试无数据时无误。  
  241.         var content = $("#content")[0];  
  242.         for(var i=0;i<30;i++){  
  243.             var row = content.insertRow();  
  244.             row.insertCell().innerHTML = 'Siuon\'s CSDN Blog';  
  245.             row.insertCell().innerHTML = '<a href="http://blog.csdn.net/xiaochunyong" target="_blank">http://blog.csdn.net/xiaochunyong</a>';  
  246.             row.insertCell().innerHTML = 'Siuon';  
  247.             row.insertCell().innerHTML = '123456';  
  248.             row.insertCell().innerHTML = 'xiaochunyong@gmail.com';  
  249.             row.insertCell().innerHTML = '1862152####';  
  250.             row.insertCell().innerHTML = '...';  
  251.             row.insertCell().innerHTML = '<a href="javascript:alert(\'修改\');">修改</a> <a href="javascript:alert(\'删除\');">删除</a>';  
  252.         }  
  253.     </script>  
  254.   </body>  
  255. </html>  

该TableGrid 暂时不支持:

1、暂时不支持表格跨行跨列
2、暂时不支持表格冻结列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值