一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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

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

這種方式的效率極低。例如,隱藏一個千行表格的某列,在我的筆記本(P4 M 1.4G,768M內(nèi)存)上執(zhí)行需要約 4000毫秒的時間,令人無法忍受。例如如下代碼:
復(fù)制代碼 代碼如下:
<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"><!--
//--------------------------------------------------------
// 時間轉(zhuǎn)為時間戳(毫秒)
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() 方式快速構(gòu)建字串,速度極快
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 隱藏列的方式,都是采用這樣的代碼。
實際上,我們可以用設(shè)置第一行的 td 或 th 的寬度為 0 的方式,來快速隱藏列。
我們把 hideOrShowCol() 函數(shù)改為如下代碼:
復(fù)制代碼 代碼如下:
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) + " 毫秒");
}

不過,僅這樣還達不到隱藏的效果,還需要設(shè)置 table 和 td 樣式為如下:
復(fù)制代碼 代碼如下:
<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ù) 無常 網(wǎng)友的提議,加上了對 colgroup 處理的代碼。奇怪的是,雖然處理原理完全一樣,但對 colgroup 進行處理的時間達到了 140毫秒,即延長了一倍。尚不清楚原因。
完整代碼:
復(fù)制代碼 代碼如下:
<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)建萬行表格

//--------------------------------------------------------
// 時間轉(zhuǎn)為時間戳(毫秒)
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() 方式快速構(gòu)建字串,速度極快
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 行以上的表格操作,這將非常耗時(甚至導(dǎo)致瀏覽器死掉)。/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技術(shù)javascript 極速 隱藏/顯示萬行表格列只需 60毫秒,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 奇米亚洲春色 | 韩国一级做a爰片性色毛片 韩国一区二区三区 | 久久久久久中文字幕 | 97视频在线观看视频在线精品 | 激情福利视频 | 国产成人综合在线视频 | 国产一区国产二区国产三区 | 国语对白免费观看网址 | 成人国产精品视频 | 超级香蕉97视频在线观看一区 | 日本乱中文字幕系列在线观看 | 99在线在线视频免费视频观看 | 欧美ab片 | 一区二区三区免费在线视频 | 久久99爰这里有精品国产 | 精品999久久久久久中文字幕 | 精品一区精品二区 | 综合激情区视频一区视频二区 | 中文字幕亚洲日韩无线码 | 国产成人一区二区三区在线视频 | 草色在线 | 优优色影 | 色呦呦网站入口 | 亚洲首页国产精品丝袜 | 激情综合在线观看 | 国产激情片| 亚洲一区视频在线播放 | 色四虎| 国产在线干 | 大尺度人体videosex | 另类天堂网 | 视频网站黄色 | 国产精品亚洲高清一区二区 | 一本一道久久综合狠狠老 | 乱子伦免费视频中文字幕 | 日本网址在线观看 | 极品美女一区二区三区 | 精品欧美一区二区三区精品久久 | 国产精品久久网 | 国产二区视频 | 亚洲喷水|