javascript 極速 隱藏/顯示萬(wàn)行表格列只需 60毫秒
更新時(shí)間:2009年03月28日 00:20:41 作者:
隱藏表格列 這種方式的效率極低。例如,隱藏一個(gè)千行表格的某列,在我的筆記本(P4 M 1.4G,768M內(nèi)存)上執(zhí)行需要約 4000毫秒的時(shí)間,令人無(wú)法忍受。
隱藏表格列,最常見的是如下方式:
td.style.display = "none";
這種方式的效率極低。例如,隱藏一個(gè)千行表格的某列,在我的筆記本(P4 M 1.4G,768M內(nèi)存)上執(zhí)行需要約 4000毫秒的時(shí)間,令人無(wú)法忍受。例如如下代碼:
<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 出來(lái)的用 javascript 隱藏列的方式,都是采用這樣的代碼。
實(shí)際上,我們可以用設(shè)置第一行的 td 或 th 的寬度為 0 的方式,來(lái)快速隱藏列。
我們把 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("耗時(shí):" + (t2 - t1) + " 毫秒");
}
不過(guò),僅這樣還達(dá)不到隱藏的效果,還需要設(shè)置 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>
重新測(cè)試,我們發(fā)現(xiàn),隱藏千行表格的某列,只需要不到 15毫秒的時(shí)間。而即使用 createTable(10000) 創(chuàng)建萬(wàn)行表格,再來(lái)測(cè)試,也只需要 60 毫秒的時(shí)間(都是以我的筆記本上的執(zhí)行時(shí)間為參照。實(shí)際上,你們大多數(shù)人的電腦配置都比我的筆記本高很多,因此時(shí)間會(huì)更短),效率十分令人滿意。
補(bǔ)充:
根據(jù) 無(wú)常 網(wǎng)友的提議,加上了對(duì) colgroup 處理的代碼。奇怪的是,雖然處理原理完全一樣,但對(duì) colgroup 進(jìn)行處理的時(shí)間達(dá)到了 140毫秒,即延長(zhǎng)了一倍。尚不清楚原因。
完整代碼:
<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)建萬(wàn)行表格
//--------------------------------------------------------
// 時(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("將要對(duì) 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") // 對(duì) 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>
復(fù)制代碼 代碼如下:
td.style.display = "none";
這種方式的效率極低。例如,隱藏一個(gè)千行表格的某列,在我的筆記本(P4 M 1.4G,768M內(nèi)存)上執(zhí)行需要約 4000毫秒的時(shí)間,令人無(wú)法忍受。例如如下代碼:
復(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 出來(lái)的用 javascript 隱藏列的方式,都是采用這樣的代碼。
實(shí)際上,我們可以用設(shè)置第一行的 td 或 th 的寬度為 0 的方式,來(lái)快速隱藏列。
我們把 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) + " 毫秒");
}
不過(guò),僅這樣還達(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>
重新測(cè)試,我們發(fā)現(xiàn),隱藏千行表格的某列,只需要不到 15毫秒的時(shí)間。而即使用 createTable(10000) 創(chuàng)建萬(wàn)行表格,再來(lái)測(cè)試,也只需要 60 毫秒的時(shí)間(都是以我的筆記本上的執(zhí)行時(shí)間為參照。實(shí)際上,你們大多數(shù)人的電腦配置都比我的筆記本高很多,因此時(shí)間會(huì)更短),效率十分令人滿意。
補(bǔ)充:
根據(jù) 無(wú)常 網(wǎng)友的提議,加上了對(duì) colgroup 處理的代碼。奇怪的是,雖然處理原理完全一樣,但對(duì) colgroup 進(jìn)行處理的時(shí)間達(dá)到了 140毫秒,即延長(zhǎng)了一倍。尚不清楚原因。
完整代碼:
復(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)建萬(wàn)行表格
//--------------------------------------------------------
// 時(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("將要對(duì) 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") // 對(duì) 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>
您可能感興趣的文章:
- js中的時(shí)間轉(zhuǎn)換—毫秒轉(zhuǎn)換成日期時(shí)間的示例代碼
- Javascript毫秒數(shù)用法實(shí)例
- js時(shí)間日期和毫秒的相互轉(zhuǎn)換
- 在JS中如何把毫秒轉(zhuǎn)換成規(guī)定的日期時(shí)間格式實(shí)例
- js實(shí)現(xiàn)精確到毫秒的倒計(jì)時(shí)效果
- 基于javascript實(shí)現(xiàn)精確到毫秒的倒計(jì)時(shí)限時(shí)搶購(gòu)
- js 毫秒轉(zhuǎn)天時(shí)分秒的實(shí)例
- 用js實(shí)現(xiàn)每隔一秒刷新時(shí)間的實(shí)例(含年月日時(shí)分秒)
- JS獲取年月日時(shí)分秒的方法分析
- JS實(shí)現(xiàn)獲取毫秒值及轉(zhuǎn)換成年月日時(shí)分秒的方法
相關(guān)文章
微信小程序web-view不支持打開非業(yè)務(wù)域名https?//XXXX?請(qǐng)重新配置的解決辦法
小程序現(xiàn)在日漸成熟,功能也越來(lái)越強(qiáng)大,我們今天來(lái)一起看看小程序跳轉(zhuǎn)H5頁(yè)面的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于微信小程序web-view不支持打開非業(yè)務(wù)域名https?//XXXX?請(qǐng)重新配置的解決辦法,需要的朋友可以參考下2022-08-08微信小程序上傳帖子的實(shí)例代碼(含有文字圖片的微信驗(yàn)證)
這篇文章主要介紹了小程序上傳帖子(含有文字圖片的微信驗(yàn)證)的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07JavaScript實(shí)現(xiàn)點(diǎn)擊單元格改變背景色的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)點(diǎn)擊單元格改變背景色的方法,涉及JavaScript響應(yīng)鼠標(biāo)事件動(dòng)態(tài)操作頁(yè)面元素屬性的相關(guān)技巧,需要的朋友可以參考下2016-02-02淺談javascript:兩種注釋,聲明變量,定義函數(shù)
下面小編就為大家?guī)?lái)一篇淺談javascript:兩種注釋,聲明變量,定義函數(shù)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10JS中setInterval、setTimeout不能傳遞帶參數(shù)的函數(shù)的解決方案
在JS中無(wú)論是setTimeout還是setInterval,在使用函數(shù)名作為調(diào)用句柄時(shí)都不能帶參數(shù),而在許多場(chǎng)合必須要帶參數(shù),接下來(lái)為大家介紹具體的解決方法2013-04-04JavaScript數(shù)組排序的六種常見算法總結(jié)
這篇文章主要給大家介紹了關(guān)于JavaScript數(shù)組排序的六種常見算法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用JavaScript數(shù)組具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08