天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

javascript 極速 隱藏/顯示萬行表格列只需 60毫秒

隱藏表格列,最常見的是如下方式:
復制代碼 代碼如下:
td.style.display = "none";

這種方式的效率極低。例如,隱藏一個千行表格的某列,在我的筆記本(P4 M 1.4G,768M內存)上執(zhí)行需要約 4000毫秒的時間,令人無法忍受。例如如下代碼:
復制代碼 代碼如下:
<body>
<input type=button onclick=hideCol(1) value='隱藏第 2 列'>
<input type=button onclick=showCol(1) value='顯示第 2 列'>
<div id=tableBox></div>
<script type="text/Javascript"><!--
//--------------------------------------------------------
// 時間轉為時間戳(毫秒)
function time2stamp(){var d=new Date();return Date.parse(d)+d.getMilliseconds();}

//--------------------------------------------------------
// 創(chuàng)建表格
function createTable(rowsLen)
{
var str = "<table border=1>" +
"<thead>" +
"<tr>" +
"<th width=100>col1<//th>" +
"<th width=200>col2<//th>" +
"<th width=50>col3<//th>" +
"<//tr>" +
"<//thead>" +
"<tbody>";

var arr = [];
for (var i=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
}

//--------------------------------------------------------
// 隱藏/顯示指定列
function hideCol(colIdx){hideOrShowCol(colIdx, 0);}
function showCol(colIdx){hideOrShowCol(colIdx, 1);}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function hideOrShowCol(colIdx, isShow)
{
var t1 = time2stamp(); //
var table = tableBox.children[0];
var rowsLen = table.rows.length;
var lastTr = table.rows[0];
for (var i=0; i<rowsLen; i++)
{
var tr = table.rows[i];
tr.children[colIdx].style.display = isShow ? "" : "none";
}

var t2 = time2stamp();
alert("耗時:" + (t2 - t1) + " 毫秒");
}

//--------------------------------------------------------
createTable(1000); // 創(chuàng)建千行表格
// --></script>


遺憾的是,我們 google 出來的用 Javascript 隱藏列的方式,都是采用這樣的代碼。
實際上,我們可以用設置第一行的 td 或 th 的寬度為 0 的方式,來快速隱藏列。
我們把 hideOrShowCol() 函數(shù)改為如下代碼:
復制代碼 代碼如下:
function hideOrShowCol(colIdx, isShow)
{
var t1 = time2stamp(); //
var table = tableBox.children[0];
var tr = table.rows[0];
tr.children[colIdx].style.width = isShow ? 200 : 0;

var t2 = time2stamp();
alert("耗時:" + (t2 - t1) + " 毫秒");
}

不過,僅這樣還達不到隱藏的效果,還需要設置 table 和 td 樣式為如下:
復制代碼 代碼如下:
<style><!--
table
{
border-collapse:collapse;
table-layout:fixed;
overflow:hidden;
}
td
{
overflow:hidden;
white-space: nowrap;
}
--></style><style bogus="1">table
{
border-collapse:collapse;
table-layout:fixed;
overflow:hidden;
}
td
{
overflow:hidden;
white-space: nowrap;
}</style>

重新測試,我們發(fā)現(xiàn),隱藏千行表格的某列,只需要不到 15毫秒的時間。而即使用 createTable(10000) 創(chuàng)建萬行表格,再來測試,也只需要 60 毫秒的時間(都是以我的筆記本上的執(zhí)行時間為參照。實際上,你們大多數(shù)人的電腦配置都比我的筆記本高很多,因此時間會更短),效率十分令人滿意。
補充:
根據(jù) 無常 網友的提議,加上了對 colgroup 處理的代碼。奇怪的是,雖然處理原理完全一樣,但對 colgroup 進行處理的時間達到了 140毫秒,即延長了一倍。尚不清楚原因。
完整代碼:
復制代碼 代碼如下:
<style><!--
table
{
border-collapse:collapse;
table-layout:fixed;
overflow:hidden;
}
td
{
overflow:hidden;
white-space: nowrap;
}
--></style><style bogus="1">table
{
border-collapse:collapse;
table-layout:fixed;
overflow:hidden;
}
td
{
overflow:hidden;
white-space: nowrap;
}</style>
<body>
<input type=button onclick=createTable() value='創(chuàng)建表格:使用 thead'>
<input type=button onclick=createTable(1) value='創(chuàng)建表格:使用 colgroup'>
<br>
<input type=button onclick=hideCol(1) value='隱藏第 2 列'>
<input type=button onclick=showCol(1) value='顯示第 2 列'>

<input type=button onclick=hideCol_fast(1) value='快速隱藏第 2 列'>
<input type=button onclick=showCol_fast(1) value='快速顯示第 2 列'>
<div id=tableBox></div>
<script type="text/Javascript"><!--
var tableRowsLen = 10000; // 創(chuàng)建萬行表格

//--------------------------------------------------------
// 時間轉為時間戳(毫秒)
function time2stamp(){var d=new Date();return Date.parse(d)+d.getMilliseconds();}

//--------------------------------------------------------
// 創(chuàng)建表格
function createTable(isUseColGroup)
{
if (isUseColGroup) // 使用 colgroup 標簽
{
var str = "<table border=1>" +
"<colgroup>" +
"<col width=100 />" +
"<col width=200 />" +
"<col width=50 />" +
"<//colgroup>" +
"<tbody>";
}
else
{
// 使用 thead 標簽
var str = "<table border=1>" +
"<thead>" +
"<tr>" +
"<th width=100>col1<//th>" +
"<th width=200>col2<//th>" +
"<th width=50>col3<//th>" +
"<//tr>" +
"<//thead>" +
"<tbody>";
}

var arr = [];
for (var i=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
}

//--------------------------------------------------------
// 隱藏/顯示指定列
function hideCol(colIdx){hideOrShowCol(colIdx, 0);}
function showCol(colIdx){hideOrShowCol(colIdx, 1);}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function hideOrShowCol(colIdx, isShow)
{
var t1 = time2stamp(); //
var table = tableBox.children[0];
var rowsLen = table.rows.length;
var lastTr = table.rows[0];

if (rowsLen > 1001)
{
if (!confirm("將要對 1000 行以上的表格操作,這將非常耗時(甚至導致瀏覽器死掉)。/n您確定要繼續(xù)嗎?"))
return;
}

for (var i=0; i<rowsLen; i++)
{
var tr = table.rows[i];
tr.children[colIdx].style.display = isShow ? "" : "none";
}

var t2 = time2stamp();
alert("耗時:" + (t2 - t1) + " 毫秒");
}

//--------------------------------------------------------
// 隱藏/顯示指定列 - 快速
function hideCol_fast(colIdx){hideOrShowCol_fast(colIdx, 0);}
function showCol_fast(colIdx){hideOrShowCol_fast(colIdx, 1);}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function hideOrShowCol_fast(colIdx, isShow)
{
var t1 = time2stamp(); //
var table = tableBox.children[0];
var thead = table.children[0]; // 可能是 thead 或者 tbody,也可能是 colgroup
if (thead.tagName.toLowerCase()=="colgroup") // 對 colgroup 特殊處理
{
var td = thead.children[colIdx];
}
else
{
// 注意:如果表格沒有 thead 和 tbody 標簽,則 table.children[0] 是 tbody
var tr = thead.children[0];
var td = tr.children[colIdx];
}
td.style.width = isShow ? 200 : 0;

var t2 = time2stamp();
alert("耗時:" + (t2 - t1) + " 毫秒");
}

//--------------------------------------------------------
createTable();
// --></script>

JavaScript技術javascript 極速 隱藏/顯示萬行表格列只需 60毫秒,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 在线涩涩免费观看国产精品 | 娇妻归来在线观看免费完整版电影 | AV天堂午夜精品一区 | 手机精品在线 | 亚洲熟女片嫩草影院 | 一二三四在线观看韩国 | MELODY在线播放无删减 | 美女扣逼软件 | 2020亚洲色噜噜狠狠网站 | 性色AV乱码一区二区三区视频 | 无人区乱码区1卡2卡三卡在线 | 无码AV熟妇素人内射V在线 | 久久影院毛片一区二区 | caoporn 超碰在线视频 | 永久免费的污视频网站 | 久久精品一本到99热 | 伊人情人网综合 | 亲胸揉胸膜下刺激视频在线观看 | 日本中文一区 | 亚洲精品国产精麻豆久久99 | 国产精品久久精品 | gogogo在线观看 | 强伦姧久久久久久久久久 | JIZJIZJIZ 日本老师水多 | 国产精品丰满人妻AV麻豆 | 两性午夜色视频免费网站 | 超级最爽的乱淫片免费 | 99RE久久精品国产 | 欧美疯狂做受xxxxx喷水 | 老师给美女同学开嫩苞 | 日产精品高潮呻吟AV久久 | 伊人久久99热这里只有精品 | 久久中文字幕无码A片不卡 久久中文字幕人妻熟AV女蜜柚M | jlzz中国jizz日本老师水多 | 欧美一区二区三区免费播放 | 石原莉奈rbd806中文字幕 | 亚洲 欧美 中文字幕 在线 | 国产69精品久久久久乱码免费 | 亚洲色图在线视频 | 性饥渴姓交HDSEX | HEYZO精品无码一区二区三区 |