Js table, tens of thousands of data are loaded instantly
In the practical application of Ajax dynamically loading data, everyone is used to a way of thinking: create a row of data.
So if the number is large and the data needs to be loaded at one time, the browser will be stuck for half a day.
Inspired by Flex's DataGrid control, in Flex's DataGrid control, the method of displaying data is not to create as many rows as there are, it only creates at most a dozen or twenty rows you see on the interface (assuming it is If there are too many data, the n rows of data you should see will be extracted from the data during the scrolling process and redisplayed in the n row control that has been created.
In other words, in the Flex DataGrid control, we actually see only the n-line controls, but the data they display is filtered according to the scroll bar state.
Therefore, if it is implemented in JS using a similar method, then there are tens of thousands of pieces of data, and it may be just a few dozen Dom creation, which will naturally be much faster.
Without further ado, please add the code. First, a scroll bar is needed
Scrollbar.js
The code copy is as follows:
function Scrollbar() {
this.options = {
total: 0, //Total number of data
pos: 0, //The current scrolling position
itemSize: 20, //Single size
size: 200 //Control size
};
}
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; //Set the control height
_this.bar.style.height = _this.options.size + "px";
//Set content height
var ch = _this.options.total * _this.options.itemSize;
_this.content.style.height = ch + "px";
}
//Get scrolling position
function getPos() {
var top = this.bar.scrollTop;
var pos = parseInt(top / this.options.itemSize);
return pos;
}
//The number of data that can be displayed per page
function getPageItems() {
return this.options.size / this.options.itemSize;
}
//Scrolling event response
function OnScroll(_this) {
var pos = _this.getPos();
if (pos == _this.options.pos)
return;
_this.options.pos = pos;
_this.onScroll(pos);
}
//Scrollbar creation
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 compatible
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
//Simulate scrollbar events
};
}) ();
Page code:
The code copy is as follows:
<!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>
Table controls
</title>
<script src="Scrollbar.js" type="text/javascript">
</script>
<script language="javascript" type="text/javascript">
var data = [];
//Create ten thousand sample data
for (var i = 0; i < 10000; i++) {
var row = {
id: i,
text: "text" + i
};
data.push(row);
}
//Create scrollbar
var scrbar = new Scrollbar();
window.onload = function() {
scrbar.CreateAt("divScroll");
scrbar.setOptions({
Total: 10000,
size: 300
});
scrbar.onScroll = function(pos) {
ShowData(pos);
}
//Get the template
var items = scrbar.getPageItems();
var tpl = document.getElementById("trTpl");
tpl.parentNode.removeChild(tpl);
// Create only a few dozen rows of tables you see, so it's naturally much faster
var list = document.getElementById("tblList");
for (var i = 0; i < data.length && i < items; i++) {
var nr = tpl.cloneNode(true);
//Copy new line from template line
list.appendChild(nr);
}
ShowData(scrbar.getPos());
}
//Display data according to the scroll bar
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>
<!--Line Title-->
<head>
<tr>
<th>
ID
</th>
<th>
Text
</th>
</tr>
</head>
<!--Data Display Area-->
<tbody id="tblList">
<tr id="trTpl">
<td id="tdID">
</td>
<td id="tdText">
</td>
</tr>
</tbody>
</table>
</body>
</html>