亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Js表格萬(wàn)條數(shù)據(jù)瞬間加載實(shí)現(xiàn)代碼

 更新時(shí)間:2014年02月20日 10:10:23   作者:  
一條數(shù)據(jù)創(chuàng)建一行,如果數(shù)量大的時(shí)候,一次性要加載完數(shù)據(jù)的話(huà),瀏覽器就會(huì)卡上半天,下面有個(gè)不錯(cuò)的方法,大家可以參考下
Js表格,萬(wàn)條數(shù)據(jù)瞬間加載

在Ajax動(dòng)態(tài)加載數(shù)據(jù)的實(shí)際應(yīng)用中,大家都習(xí)慣了一種思維方式:一條數(shù)據(jù)創(chuàng)建一行。
于是如果數(shù)量大的時(shí)候,一次性要加載完數(shù)據(jù)的話(huà),瀏覽器就會(huì)卡上半天

受Flex的DataGrid控件的啟發(fā),在Flex的DataGrid控件中,展示數(shù)據(jù)的方法并不是有多少條數(shù)據(jù)就創(chuàng)建多少行,它最多只創(chuàng)建你在界面上所看到的十幾二十行(假設(shè)為n行),如果數(shù)據(jù)多的話(huà),在滾動(dòng)過(guò)程中,會(huì)從數(shù)據(jù)中抽取你應(yīng)該看到的這n行數(shù)據(jù),重新展示在已經(jīng)創(chuàng)建好的那n行控件中。
也就是說(shuō),F(xiàn)lex的DataGrid控件中,我們實(shí)際上看到的僅僅是那n行控件,只是它們展示的數(shù)據(jù)是根據(jù)滾動(dòng)條狀態(tài)來(lái)篩選出來(lái)的。

所以,如果在JS中,也用類(lèi)似的方法實(shí)現(xiàn),那么就是上萬(wàn)條數(shù)據(jù),可能也只要?jiǎng)?chuàng)建幾十個(gè)Dom而已,效率自然快得多了。
廢話(huà)不多說(shuō),上代碼。首先,需要一個(gè)滾動(dòng)條
Scrollbar.js
復(fù)制代碼 代碼如下:

function Scrollbar() {
this.options = {
total : 0, //數(shù)據(jù)總數(shù)
pos : 0, //當(dāng)前滾動(dòng)位置
itemSize : 20, //單項(xiàng)尺寸
size : 200 //控件尺寸
};
}
Scrollbar.prototype = (function () {
function setOptions(options) {
for (var attr in options) {
this.options[attr] = options[attr];
}
Refresh(this);
}
function Refresh(_this) {
if (!_this.created)
return; //設(shè)置控件高度
_this.bar.style.height = _this.options.size + "px";
//設(shè)置內(nèi)容高度
var ch = _this.options.total * _this.options.itemSize;
_this.content.style.height = ch + "px";
}
//獲取滾動(dòng)位置
function getPos() {
var top = this.bar.scrollTop;
var pos = parseInt(top / this.options.itemSize);
return pos;
}
//每頁(yè)可展示的數(shù)據(jù)數(shù)量
function getPageItems() {
return this.options.size / this.options.itemSize;
}
//滾動(dòng)事件響應(yīng)
function OnScroll(_this) {
var pos = _this.getPos();
if (pos == _this.options.pos)
return;
_this.options.pos = pos;
_this.onScroll(pos);
}
//滾動(dòng)條創(chuàng)建
function CreateAt(dom) {
var _this = this;
var bar = document.createElement("div");
var content = document.createElement("div");
bar.appendChild(content);
bar.style.width = "19px";
bar.style.overflowY = "scroll";
bar.style.overflowX = "hidden";
if (bar.attachEvent) {
bar.attachEvent("onscroll", function () {
OnScroll(_this);
});
} else {
//firefox兼容
bar.addEventListener("scroll", function () {
OnScroll(_this);
}, false);
}
content.style.backgroundColor = "white";
content.style.width = "1px";
this.bar = bar;
this.content = content;
if (typeof(dom) == "string") {
dom = document.getElementById(dom);
}
dom.innerHTML = "";
dom.appendChild(this.bar);
this.created = true;
Refresh(this);
}
return {
setOptions : setOptions,
CreateAt : CreateAt,
getPos : getPos,
getPageItems : getPageItems,
onScroll : null
//模擬滾動(dòng)條事件
};
})();

頁(yè)面代碼:
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
表格控件
</title>
<script src="Scrollbar.js" type="text/javascript">
</script>
<script language="javascript" type="text/javascript">
var data = [];
//創(chuàng)建一萬(wàn)條示例數(shù)據(jù)
for (var i = 0; i < 10000; i++) {
var row = {
id: i,
text: "text" + i
};
data.push(row);
}
//創(chuàng)建滾動(dòng)條
var scrbar = new Scrollbar();
window.onload = function() {
scrbar.CreateAt("divScroll");
scrbar.setOptions({
total: 10000,
size: 300
});
scrbar.onScroll = function(pos) {
ShowData(pos);
}
//獲取模板
var items = scrbar.getPageItems();
var tpl = document.getElementById("trTpl");
tpl.parentNode.removeChild(tpl);
//僅創(chuàng)建所看到的幾十行表格,所以自然快得多
var list = document.getElementById("tblList");
for (var i = 0; i < data.length && i < items; i++) {
var nr = tpl.cloneNode(true);
//從模板行復(fù)制新行
list.appendChild(nr);
}
ShowData(scrbar.getPos());
}
//根據(jù)滾動(dòng)條,展示數(shù)據(jù)
function ShowData(pos) {
var n = scrbar.getPageItems();
var rows = document.getElementById("tblList").rows;
for (var i = 0; i < n; i++) {
var row = rows[i];
var item = data[i + pos];
row.cells["tdID"].innerHTML = item["id"];
row.cells["tdText"].innerHTML = item["text"];
}
}
</script>
</head>
<body>
<div id="divScroll" style="float:right">
</div>
<table border="1">
<!--行標(biāo)題-->
<thead>
<tr>
<th>
ID
</th>
<th>
Text
</th>
</tr>
</thead>
<!--數(shù)據(jù)展示區(qū)-->
<tbody id="tblList">
<tr id="trTpl">
<td id="tdID">
</td>
<td id="tdText">
</td>
</tr>
</tbody>
</table>
</body>
</html>

相關(guān)文章

  • javascript jq 彈出層實(shí)例

    javascript jq 彈出層實(shí)例

    javascript jq 彈出提示效果實(shí)現(xiàn)的方法有很多,可能會(huì)在某些地方看到過(guò),下面為大家演示個(gè)示例介紹下如何實(shí)現(xiàn)jq 彈出層,感興趣的朋友可以參考下
    2013-08-08
  • javascript中l(wèi)ength屬性的探索

    javascript中l(wèi)ength屬性的探索

    本文中,我將會(huì)通過(guò)類(lèi)數(shù)組對(duì)象(array like object),探索javascript中的length屬性的一些秘密。
    2011-07-07
  • JavaScript/Js腳本處理html元素的自定義屬性解析(親測(cè)兼容Firefox與IE)

    JavaScript/Js腳本處理html元素的自定義屬性解析(親測(cè)兼容Firefox與IE)

    這篇文章主要是對(duì)JavaScript/Js腳本處理html元素的自定義屬性解析(親測(cè)兼容Firefox與IE)進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2013-11-11
  • JavaScript 快捷鍵設(shè)置實(shí)現(xiàn)代碼

    JavaScript 快捷鍵設(shè)置實(shí)現(xiàn)代碼

    屏蔽Alt+F4等快捷鍵 IE Javascript快捷鍵操作
    2009-03-03
  • Openlayers實(shí)現(xiàn)測(cè)量功能

    Openlayers實(shí)現(xiàn)測(cè)量功能

    這篇文章主要為大家詳細(xì)介紹了Openlayers實(shí)現(xiàn)測(cè)量功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • 關(guān)于ES6中數(shù)組新增的方法詳解

    關(guān)于ES6中數(shù)組新增的方法詳解

    數(shù)組(Array)是有序的元素序列,若將有限個(gè)類(lèi)型相同的變量的集合命名,那么這個(gè)名稱(chēng)為數(shù)組名,下面這篇文章主要給大家介紹了關(guān)于ES6中數(shù)組新增方法的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • ES6數(shù)組與對(duì)象的解構(gòu)賦值詳解

    ES6數(shù)組與對(duì)象的解構(gòu)賦值詳解

    這篇文章主要介紹了ES6數(shù)組與對(duì)象的解構(gòu)賦值,結(jié)合實(shí)例形式詳細(xì)分析了ES6中數(shù)組與對(duì)象的解構(gòu)賦值原理、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-06-06
  • JavaScript實(shí)現(xiàn)二級(jí)菜單的制作

    JavaScript實(shí)現(xiàn)二級(jí)菜單的制作

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)二級(jí)菜單的制作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • JavaScript解構(gòu)賦值詳解

    JavaScript解構(gòu)賦值詳解

    這篇文章主要為大家介紹了JavaScript解構(gòu)賦值,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • JS實(shí)現(xiàn)遮罩層效果的簡(jiǎn)單實(shí)例

    JS實(shí)現(xiàn)遮罩層效果的簡(jiǎn)單實(shí)例

    這篇文章介紹了JS實(shí)現(xiàn)遮罩層效果的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下
    2013-11-11

最新評(píng)論