`
nanjingjiangbiao_T
  • 浏览: 2600628 次
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

js创建10万行表格 页面显示10万行数据

 
阅读更多

js创建10万行表格 页面显示10万行数据<wbr></wbr>

<html>
<head></head>
<style>
table
{
border-collapse:collapse;
table-layout:fixed;
overflow:hidden;
}
td
{
overflow:hidden;
white-space: nowrap;
}
--></mce:style><style mce_bogus="1">table
{
border-collapse:collapse;
table-layout:fixed;
overflow:hidden;
}
td
{
overflow:hidden;
white-space: nowrap;
}
</style>
<script src="jquery-1.3.2.min.js"></script>
<body>
<input type=button onclick=createTable() value='创建表格:使用 thead'>
<input type=button onclick=createTable(1) value='创建表格:使用 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; // 创建万行表格

//--------------------------------------------------------
// 时间转为时间戳(毫秒)
function time2stamp(){var d=new Date();return Date.parse(d)+d.getMilliseconds();}

//--------------------------------------------------------
// 创建表格
function createTable(isUseColGroup)
{
var t1 = time2stamp();
if (isUseColGroup) // 使用 colgroup 标签
{
var str = "<table border=1>" +
"<colgroup>" +
"<col width=100 class=col1 />" +
"<col width=200 class=col2/>" +
"<col width=50 class=col3/>" +
"<\/colgroup>" +
"<tbody>";
}
else
{
// 使用 thead 标签
var str = "<table border=1>" +
"<thead>" +
"<tr>" +
"<th width=100 class=col1>col1<\/th>" +
"<th width=200 class=col2>col2<\/th>" +
"<th width=50 class=col3>col3<\/th>" +
"<\/tr>" +
"<\/thead>" +
"<tbody>";
}

var arr = [];
for (var i=0; i<tableRowsLen; i++)
{
arr[i] = "<tr><td class=col1>" + i + "--1<\/td><td class=col2>" + i + "--2</td><td class=col3>" + i + "--3<\/td></tr>";
}
str += arr.join("") + "</tbody><\/table>"; // 用 join() 方式快速构建字串,速度极快
tableBox.innerHTML = str; // 生成 table
var t2 = time2stamp();
alert("耗时:" + (t2 - t1) + " 毫秒");

}

//--------------------------------------------------------
// 隐藏/显示指定列
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您确定要继续吗?"))
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 = 0;

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

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

肯定知道-精品推荐:

1、一个土八路凭什么让冈村宁次都忌惮?

2、二战时期仅有的国军开坦克追日本兵,都压成肉酱了!!!

3、军事奇迹:看一书生7000人马横扫大半个中国的奇迹

4、国军悍将创造军事奇迹后为什么会投降日军?

5、国军战神曹锡,一人消灭日军一个大队!

6、flash无法覆盖的问题_三种办法可以解决flash覆盖其他元素

7、南海石油30年疯狂开采,到底还剩多少?

8、送礼国家也收税?莫不是都疯啦?

9、古代第一美女的悲惨结局

10、中国空军光棍节轰炸日本航母“龙骧”号

11、日本本土史上第一次空袭竟是中国轰炸机

12、中原大战中老将的王牌战将为何被害?

13、刷流量软件-免费刷流量工具-软件

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics