javascript极速隐藏/显示万行表格列只需60毫秒
JavaScript 显示 表格 极速 隐藏 60 毫秒 万行
2023-06-13 09:14:08 时间
隐藏表格列,最常见的是如下方式:
复制代码代码如下:
td.style.display="none";
这种方式的效率极低。例如,隐藏一个千行表格的某列,在我的笔记本(P4M1.4G,768M内存)上执行需要约4000毫秒的时间,令人无法忍受。例如如下代码:
<body>
<inputtype=buttononclick=hideCol(1)value="隐藏第2列">
<inputtype=buttononclick=showCol(1)value="显示第2列">
<divid=tableBox></div>
<scripttype="text/javascript"><!--
//--------------------------------------------------------
//时间转为时间戳(毫秒)
functiontime2stamp(){vard=newDate();returnDate.parse(d)+d.getMilliseconds();}
//--------------------------------------------------------
//创建表格
functioncreateTable(rowsLen)
{
varstr="<tableborder=1>"+
"<thead>"+
"<tr>"+
"<thwidth=100>col1<\/th>"+
"<thwidth=200>col2<\/th>"+
"<thwidth=50>col3<\/th>"+
"<\/tr>"+
"<\/thead>"+
"<tbody>";
vararr=[];
for(vari=0;i<rowsLen;i++)
{
arr[i]="<tr><td>"+i+"1<\/td><td>"+i+"2</td><td>"+i+"3<\/td></tr>";
}
str+=arr.join("")+"</tbody><\/table>";//用join()方式快速构建字串,速度极快
tableBox.innerHTML=str;//生成table
}
//--------------------------------------------------------
//隐藏/显示指定列
functionhideCol(colIdx){hideOrShowCol(colIdx,0);}
functionshowCol(colIdx){hideOrShowCol(colIdx,1);}
//----------------------------
functionhideOrShowCol(colIdx,isShow)
{
vart1=time2stamp();//
vartable=tableBox.children[0];
varrowsLen=table.rows.length;
varlastTr=table.rows[0];
for(vari=0;i<rowsLen;i++)
{
vartr=table.rows[i];
tr.children[colIdx].style.display=isShow?"":"none";
}
vart2=time2stamp();
alert("耗时:"+(t2-t1)+"毫秒");
}
//--------------------------------------------------------
createTable(1000);//创建千行表格
//--></script>
遗憾的是,我们google出来的用javascript隐藏列的方式,都是采用这样的代码。
实际上,我们可以用设置第一行的td或th的宽度为0的方式,来快速隐藏列。
我们把hideOrShowCol()函数改为如下代码:
functionhideOrShowCol(colIdx,isShow)
{
vart1=time2stamp();//
vartable=tableBox.children[0];
vartr=table.rows[0];
tr.children[colIdx].style.width=isShow?200:0;
vart2=time2stamp();
alert("耗时:"+(t2-t1)+"毫秒");
}
不过,仅这样还达不到隐藏的效果,还需要设置table和td样式为如下:
<style><!--
table
{
border-collapse:collapse;
table-layout:fixed;
overflow:hidden;
}
td
{
overflow:hidden;
white-space:nowrap;
}
--></style><stylebogus="1">table
{
border-collapse:collapse;
table-layout:fixed;
overflow:hidden;
}
td
{
overflow:hidden;
white-space:nowrap;
}</style>
重新测试,我们发现,隐藏千行表格的某列,只需要不到15毫秒的时间。而即使用createTable(10000)创建万行表格,再来测试,也只需要60毫秒的时间(都是以我的笔记本上的执行时间为参照。实际上,你们大多数人的电脑配置都比我的笔记本高很多,因此时间会更短),效率十分令人满意。
补充:
根据无常网友的提议,加上了对colgroup处理的代码。奇怪的是,虽然处理原理完全一样,但对colgroup进行处理的时间达到了140毫秒,即延长了一倍。尚不清楚原因。
完整代码:
<style><!--
table
{
border-collapse:collapse;
table-layout:fixed;
overflow:hidden;
}
td
{
overflow:hidden;
white-space:nowrap;
}
--></style><stylebogus="1">table
{
border-collapse:collapse;
table-layout:fixed;
overflow:hidden;
}
td
{
overflow:hidden;
white-space:nowrap;
}</style>
<body>
<inputtype=buttononclick=createTable()value="创建表格:使用thead">
<inputtype=buttononclick=createTable(1)value="创建表格:使用colgroup">
<br>
<inputtype=buttononclick=hideCol(1)value="隐藏第2列">
<inputtype=buttononclick=showCol(1)value="显示第2列">
<inputtype=buttononclick=hideCol_fast(1)value="快速隐藏第2列">
<inputtype=buttononclick=showCol_fast(1)value="快速显示第2列">
<divid=tableBox></div>
<scripttype="text/javascript"><!--
vartableRowsLen=10000;//创建万行表格
//--------------------------------------------------------
//时间转为时间戳(毫秒)
functiontime2stamp(){vard=newDate();returnDate.parse(d)+d.getMilliseconds();}
//--------------------------------------------------------
//创建表格
functioncreateTable(isUseColGroup)
{
if(isUseColGroup)//使用colgroup标签
{
varstr="<tableborder=1>"+
"<colgroup>"+
"<colwidth=100/>"+
"<colwidth=200/>"+
"<colwidth=50/>"+
"<\/colgroup>"+
"<tbody>";
}
else
{
//使用thead标签
varstr="<tableborder=1>"+
"<thead>"+
"<tr>"+
"<thwidth=100>col1<\/th>"+
"<thwidth=200>col2<\/th>"+
"<thwidth=50>col3<\/th>"+
"<\/tr>"+
"<\/thead>"+
"<tbody>";
}
vararr=[];
for(vari=0;i<tableRowsLen;i++)
{
arr[i]="<tr><td>"+i+"1<\/td><td>"+i+"2</td><td>"+i+"3<\/td></tr>";
}
str+=arr.join("")+"</tbody><\/table>";//用join()方式快速构建字串,速度极快
tableBox.innerHTML=str;//生成table
}
//--------------------------------------------------------
//隐藏/显示指定列
functionhideCol(colIdx){hideOrShowCol(colIdx,0);}
functionshowCol(colIdx){hideOrShowCol(colIdx,1);}
//----------------------------
functionhideOrShowCol(colIdx,isShow)
{
vart1=time2stamp();//
vartable=tableBox.children[0];
varrowsLen=table.rows.length;
varlastTr=table.rows[0];
if(rowsLen>1001)
{
if(!confirm("将要对1000行以上的表格操作,这将非常耗时(甚至导致浏览器死掉)。\n您确定要继续吗?"))
return;
}
for(vari=0;i<rowsLen;i++)
{
vartr=table.rows[i];
tr.children[colIdx].style.display=isShow?"":"none";
}
vart2=time2stamp();
alert("耗时:"+(t2-t1)+"毫秒");
}
//--------------------------------------------------------
//隐藏/显示指定列-快速
functionhideCol_fast(colIdx){hideOrShowCol_fast(colIdx,0);}
functionshowCol_fast(colIdx){hideOrShowCol_fast(colIdx,1);}
//----------------------------
functionhideOrShowCol_fast(colIdx,isShow)
{
vart1=time2stamp();//
vartable=tableBox.children[0];
varthead=table.children[0];//可能是thead或者tbody,也可能是colgroup
if(thead.tagName.toLowerCase()=="colgroup")//对colgroup特殊处理
{
vartd=thead.children[colIdx];
}
else
{
//注意:如果表格没有thead和tbody标签,则table.children[0]是tbody
vartr=thead.children[0];
vartd=tr.children[colIdx];
}
td.style.width=isShow?200:0;
vart2=time2stamp();
alert("耗时:"+(t2-t1)+"毫秒");
}
//--------------------------------------------------------
createTable();
//--></script>
相关文章
- javascript 基础_JavaScript高级编程
- javascript使用cookie实现DIV提示框只显示一次的方法
- 网站如何通过JavaScript代码实现FPS帧率显示?
- 如何用JavaScript实现备案不关站,非工作时间还能正常显示
- javascript表单提交的内容显示在表格中
- 用javascript分类刷leetcode13.单调栈(图文视频讲解)_2023-02-27
- JavaScript 中 input 文件框选择图片并转为Base64字符串显示
- JavaScript 中创建三种消息框:警告框、确认框、提示框详解编程语言
- 你真的已经搞懂JavaScript了吗详解编程语言
- Javascript标准DOMRange操作全集
- Javascript常用运算符(Operators)-javascript基础教程
- javascript对表格的行和列都能加亮显示
- javascript文档的编码问题解决
- 用JavaScript显示随机图像或引用
- Javascript实现的CSS代码高亮显示
- javascript获取元素位置的快速方法getBoundingClientRect()
- javascript文章截取部分无损html显示实现代码
- javaScript同意等待代码实现心得
- JavaScript在网页上单击鼠标的地方显示层及关闭层
- javascript简单实现表格行间隔显示颜色并高亮显示
- javascript实现信息的显示和隐藏如注册页面
- Javascript中各种trim的实现详细解析
- 使用javascript控制cookie显示和隐藏背景图
- javascript实现页面内关键词高亮显示代码
- javascript时间函数大全
- aspcookie中文Javascript取得中文cookie
- 浅谈javascript中自定义模版