javascript 极速:隐藏/显示万行表格列只需 60毫秒

隐藏表格列,最常见的是如下方式:

[javascript] view plain copy print ?
  1. td.style.display = "none";  

这种方式的效率极低。例如,隐藏一个千行表格的某列,在我的笔记本(P4 M 1.4G,768M内存)上执行需要约 4000毫秒的时间,令人无法忍受。例如如下代码:

[javascript] view plain copy print ?
  1. <body>  
  2. <input type=button οnclick=hideCol(1) value='隐藏第 2 列'>  
  3. <input type=button οnclick=showCol(1) value='显示第 2 列'>  
  4. <div id=tableBox></div>  
  5. <mce:script type="text/javascript"><!--  
  6. //--------------------------------------------------------   
  7. // 时间转为时间戳(毫秒)   
  8. function time2stamp(){var d=new Date();return Date.parse(d)+d.getMilliseconds();}   
  9.   
  10. //--------------------------------------------------------   
  11. // 创建表格   
  12. function createTable(rowsLen)  
  13. {  
  14.     var str = "<table border=1>" +  
  15.                 "<thead>" +   
  16.                     "<tr>" +  
  17.                         "<th width=100>col1<//th>" +  
  18.                         "<th width=200>col2<//th>" +   
  19.                         "<th width=50>col3<//th>" +   
  20.                     "<//tr>" +  
  21.                 "<//thead>" +  
  22.                 "<tbody>";  
  23.   
  24.     var arr = [];  
  25.     for (var i=0; i<rowsLen; i++)  
  26.     {  
  27.         arr[i] = "<tr><td>" + i + "1<//td><td>" + i + "2</td><td>" + i + "3<//td></tr>";  
  28.     }  
  29.     str += arr.join("") + "</tbody><//table>"; // 用 join() 方式快速构建字串,速度极快  
  30.     tableBox.innerHTML = str; // 生成 table   
  31. }  
  32.   
  33. //--------------------------------------------------------   
  34. // 隐藏/显示指定列   
  35. function hideCol(colIdx){hideOrShowCol(colIdx, 0);}  
  36. function showCol(colIdx){hideOrShowCol(colIdx, 1);}  
  37. // - - - - - - - - - - - - - - - - - - - - - - - - - - - -   
  38. function hideOrShowCol(colIdx, isShow)  
  39. {  
  40.     var t1 = time2stamp(); //    
  41.     var table = tableBox.children[0];  
  42.     var rowsLen = table.rows.length;  
  43.     var lastTr = table.rows[0];  
  44.     for (var i=0; i<rowsLen; i++)  
  45.     {  
  46.         var tr = table.rows[i];  
  47.         tr.children[colIdx].style.display = isShow ? "" : "none";  
  48.     }  
  49.       
  50.     var t2 = time2stamp();  
  51.     alert("耗时:" + (t2 - t1) + " 毫秒");  
  52. }  
  53.   
  54. //--------------------------------------------------------   
  55. createTable(1000); // 创建千行表格   
  56. // --></mce:script>  

遗憾的是,我们 google 出来的用 javascript 隐藏列的方式,都是采用这样的代码。
实际上,我们可以用设置第一行的 td 或 th 的宽度为 0 的方式,来快速隐藏列。
我们把 hideOrShowCol() 函数改为如下代码:

[javascript] view plain copy print ?
  1. function hideOrShowCol(colIdx, isShow)  
  2. {  
  3.     var t1 = time2stamp(); //    
  4.     var table = tableBox.children[0];  
  5.     var tr = table.rows[0];  
  6.     tr.children[colIdx].style.width = isShow ? 200 : 0;  
  7.       
  8.     var t2 = time2stamp();  
  9.     alert("耗时:" + (t2 - t1) + " 毫秒");  
  10. }  

不过,仅这样还达不到隐藏的效果,还需要设置 table 和 td 样式为如下:

  1. <mce:style><!--  
  2. table  
  3. {  
  4.     border-collapse:collapse;  
  5.     table-layout:fixed;  
  6.     overflow:hidden;  
  7. }  
  8. td  
  9. {  
  10.     overflow:hidden;  
  11.     white-space: nowrap;  
  12. }  
  13. --></mce:style><style mce_bogus="1">table  
  14. {  
  15.     border-collapse:collapse;  
  16.     table-layout:fixed;  
  17.     overflow:hidden;  
  18. }  
  19. td  
  20. {  
  21.     overflow:hidden;  
  22.     white-space: nowrap;  
  23. }</style>  

重新测试,我们发现,隐藏千行表格的某列,只需要不到 15毫秒的时间。而即使用 createTable(10000) 创建万行表格,再来测试,也只需要 60 毫秒的时间(都是以我的笔记本上的执行时间为参照。实际上,你们大多数人的电脑配置都比我的笔记本高很多,因此时间会更短),效率十分令人满意。

补充:

根据 无常 网友的提议,加上了对 colgroup 处理的代码。奇怪的是,虽然处理原理完全一样,但对 colgroup 进行处理的时间达到了 140毫秒,即延长了一倍。尚不清楚原因。

完整代码:

[javascript] view plain copy print ?
  1. <mce:style><!--  
  2. table  
  3. {  
  4.     border-collapse:collapse;  
  5.     table-layout:fixed;  
  6.     overflow:hidden;  
  7. }  
  8. td  
  9. {  
  10.     overflow:hidden;  
  11.     white-space: nowrap;  
  12. }  
  13. --></mce:style><style mce_bogus="1">table  
  14. {  
  15.     border-collapse:collapse;  
  16.     table-layout:fixed;  
  17.     overflow:hidden;  
  18. }  
  19. td  
  20. {  
  21.     overflow:hidden;  
  22.     white-space: nowrap;  
  23. }</style>  
  24. <body>  
  25. <input type=button οnclick=createTable() value='创建表格:使用 thead'>  
  26. <input type=button οnclick=createTable(1) value='创建表格:使用 colgroup'>  
  27. <br>  
  28. <input type=button οnclick=hideCol(1) value='隐藏第 2 列'>  
  29. <input type=button οnclick=showCol(1) value='显示第 2 列'>  
  30.     
  31. <input type=button οnclick=hideCol_fast(1) value='快速隐藏第 2 列'>  
  32. <input type=button οnclick=showCol_fast(1) value='快速显示第 2 列'>  
  33. <div id=tableBox></div>  
  34. <mce:script type="text/javascript"><!--  
  35. var tableRowsLen = 10000; // 创建万行表格   
  36.   
  37. //--------------------------------------------------------   
  38. // 时间转为时间戳(毫秒)   
  39. function time2stamp(){var d=new Date();return Date.parse(d)+d.getMilliseconds();}   
  40.   
  41. //--------------------------------------------------------   
  42. // 创建表格   
  43. function createTable(isUseColGroup)  
  44. {  
  45.     if (isUseColGroup) // 使用 colgroup 标签   
  46.     {  
  47.         var str = "<table border=1>" +  
  48.                     "<colgroup>" +   
  49.                             "<col width=100 />" +  
  50.                             "<col width=200 />" +   
  51.                             "<col width=50 />" +   
  52.                     "<//colgroup>" +  
  53.                     "<tbody>";  
  54.     }  
  55.     else  
  56.     {  
  57.         // 使用 thead 标签   
  58.         var str = "<table border=1>" +  
  59.                     "<thead>" +   
  60.                         "<tr>" +  
  61.                             "<th width=100>col1<//th>" +  
  62.                             "<th width=200>col2<//th>" +   
  63.                             "<th width=50>col3<//th>" +   
  64.                         "<//tr>" +  
  65.                     "<//thead>" +  
  66.                     "<tbody>";  
  67.     }  
  68.   
  69.     var arr = [];  
  70.     for (var i=0; i<tableRowsLen; i++)  
  71.     {  
  72.         arr[i] = "<tr><td>" + i + "1<//td><td>" + i + "2</td><td>" + i + "3<//td></tr>";  
  73.     }  
  74.     str += arr.join("") + "</tbody><//table>"; // 用 join() 方式快速构建字串,速度极快  
  75.     tableBox.innerHTML = str; // 生成 table   
  76. }  
  77.   
  78. //--------------------------------------------------------   
  79. // 隐藏/显示指定列   
  80. function hideCol(colIdx){hideOrShowCol(colIdx, 0);}  
  81. function showCol(colIdx){hideOrShowCol(colIdx, 1);}  
  82. // - - - - - - - - - - - - - - - - - - - - - - - - - - - -   
  83. function hideOrShowCol(colIdx, isShow)  
  84. {  
  85.     var t1 = time2stamp(); //    
  86.     var table = tableBox.children[0];  
  87.     var rowsLen = table.rows.length;  
  88.     var lastTr = table.rows[0];  
  89.   
  90.     if (rowsLen > 1001)   
  91.     {  
  92.         if (!confirm("将要对 1000 行以上的表格操作,这将非常耗时(甚至导致浏览器死掉)。/n您确定要继续吗?"))  
  93.             return;  
  94.     }  
  95.   
  96.     for (var i=0; i<rowsLen; i++)  
  97.     {  
  98.         var tr = table.rows[i];  
  99.         tr.children[colIdx].style.display = isShow ? "" : "none";  
  100.     }  
  101.       
  102.     var t2 = time2stamp();  
  103.     alert("耗时:" + (t2 - t1) + " 毫秒");  
  104. }  
  105.   
  106. //--------------------------------------------------------   
  107. // 隐藏/显示指定列 - 快速   
  108. function hideCol_fast(colIdx){hideOrShowCol_fast(colIdx, 0);}  
  109. function showCol_fast(colIdx){hideOrShowCol_fast(colIdx, 1);}  
  110. // - - - - - - - - - - - - - - - - - - - - - - - - - - - -   
  111. function hideOrShowCol_fast(colIdx, isShow)  
  112. {  
  113.     var t1 = time2stamp(); //    
  114.     var table = tableBox.children[0];  
  115.     var thead = table.children[0]; // 可能是 thead 或者 tbody,也可能是 colgroup   
  116.     if (thead.tagName.toLowerCase()=="colgroup"// 对 colgroup 特殊处理   
  117.     {  
  118.         var td = thead.children[colIdx];  
  119.     }  
  120.     else  
  121.     {  
  122.         // 注意:如果表格没有 thead 和 tbody 标签,则 table.children[0] 是 tbody   
  123.         var tr = thead.children[0];  
  124.         var td = tr.children[colIdx];  
  125.     }  
  126.     td.style.width = isShow ? 200 : 0;  
  127.       
  128.     var t2 = time2stamp();  
  129.     alert("耗时:" + (t2 - t1) + " 毫秒");  
  130. }  
  131.   
  132. //--------------------------------------------------------   
  133. createTable();  
  134. // --></mce:script>  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值