js创建10万行表格 页面显示10万行数据
2011-07-11 11:02
513 查看
js创建10万行表格 页面显示10万行数据
<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>
<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、刷流量软件-免费刷流量工具-软件
相关文章推荐
- js创建10万行表格 页面显示10万行数据
- js创建10万行表格 页面显示10万行数据
- js创建10万行表格 页面显示10万行数据
- js创建10万行表格 页面显示10万行数据
- 使用XML、XSLT和XPath创建可排序、分页、重用的数据显示页面
- 使用 UITableView 创建表格应用——从plist文件加载并显示数据(转)
- XML创建可排序、分页的数据显示页面
- html 页面调用js链接并显示数据
- XML创建可排序、分页的数据显示页面
- JS控制页面表格内数据,按照条件排序
- JS控制表格某行隐藏或显示后页面布局错乱
- XML创建可排序、分页的数据显示页面
- hibernate不创建表格 springMVC数据提交显示参数不对
- XML创建可排序、分页的数据显示页面
- ajax根据ID查询数据库并返回Json格式数据返回js,使用append显示到页面。判断json值为[]或者[[]]的问题。
- 创建响应式布局--显示表格数据,使用测量单位,使用媒体查询
- JS获取web页面表格数据
- 解决使用Vue.js显示数据的时,页面闪现原始代码
- 【Asp.Net从零开始】:创建主表格以及单击获取详细信息的数据显示图
- ThinkPHP定时ajax获取后台数据,使用javascript动态修改前端页面的表格来显示数据