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

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

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

這種方式的效率極低。例如,隱藏一個(gè)千行表格的某列,在我的筆記本(P4 M 1.4G,768M內(nèi)存)上執(zhí)行需要約 4000毫秒的時(shí)間,令人無法忍受。例如如下代碼:
復(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"><!--
//--------------------------------------------------------
// 時(shí)間轉(zhuǎn)為時(shí)間戳(毫秒)
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("耗時(shí):" + (t2 - t1) + " 毫秒");
}

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


遺憾的是,我們 google 出來的用 Javascript 隱藏列的方式,都是采用這樣的代碼。
實(shí)際上,我們可以用設(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("耗時(shí):" + (t2 - t1) + " 毫秒");
}

不過,僅這樣還達(dá)不到隱藏的效果,還需要設(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毫秒的時(shí)間。而即使用 createTable(10000) 創(chuàng)建萬行表格,再來測試,也只需要 60 毫秒的時(shí)間(都是以我的筆記本上的執(zhí)行時(shí)間為參照。實(shí)際上,你們大多數(shù)人的電腦配置都比我的筆記本高很多,因此時(shí)間會更短),效率十分令人滿意。
補(bǔ)充:
根據(jù) 無常 網(wǎng)友的提議,加上了對 colgroup 處理的代碼。奇怪的是,雖然處理原理完全一樣,但對 colgroup 進(jìn)行處理的時(shí)間達(dá)到了 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)建萬行表格

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

//--------------------------------------------------------
// 創(chuàng)建表格
function createTable(isUseColGroup)
{
if (isUseColGroup) // 使用 colgroup 標(biāo)簽
{
var str = "<table border=1>" +
"<colgroup>" +
"<col width=100 />" +
"<col width=200 />" +
"<col width=50 />" +
"<//colgroup>" +
"<tbody>";
}
else
{
// 使用 thead 標(biāo)簽
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 行以上的表格操作,這將非常耗時(shí)(甚至導(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("耗時(shí):" + (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 標(biāo)簽,則 table.children[0] 是 tbody
var tr = thead.children[0];
var td = tr.children[colIdx];
}
td.style.width = isShow ? 200 : 0;

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

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

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

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

主站蜘蛛池模板: 麻豆成人啪啪色婷婷久久 | 久久精品一本到99热 | 在线A亚洲老鸭窝天堂AV高清 | 狠狠色狠狠色综合日日92 | 久久国产免费观看精品1 | 约艺术院校96年清纯白嫩 | 年轻的朋友4在线看中文字幕 | 国产亚洲欧美日韩综合综合二区 | 欧美激情视频一区二区 | 视频一区国产在线第一页 | 飘雪韩国在线观看免费高清完整版 | 午夜影院一区二区三区 | 一抽一出BGM免费50分动漫 | 蜜臀亚洲AV永久无码精品老司机 | 久久这里的只有是精品23 | 好嗨哟在线看片免费 | 亚洲人女同志video | VIDEOSGGRATIS欧美另类 | 爽a中文字幕一区 | 成人天堂婷婷青青视频在线观看 | 动漫美女人物被黄漫在线看 | 中文字幕亚洲乱码熟女在线 | 欧美伊人久久大香线蕉综合69 | 动漫美女禁区 | 吉吉影音先锋av资源 | 亚洲AV怡红院AV男人的天堂 | 亚洲精品视频在线免费 | 国产在线一卡二卡 | 国家产午夜精品无人区 | 色婷婷AV国产精品欧美毛片 | 伊人综合在线影院 | 欧美精品色婷婷五月综合 | 亚洲综合免费视频 | 被爽到叫呻呤视频免费视频 | 做暖暖视频在线看片免费 | 亚洲无码小格式 | 国产美女影院 | 中字幕视频在线永久在线 | 国产福利视频第一导航 | 日本国产黄色片 | 亚欧成人毛片一区二区三区四区 |