Create a row of data. If a large number of data is loaded at a time, the browser will be stuck for half a day. Below is a good method. You can refer to the Js table below, load tens of thousands of data records instantly
In the practical application of Ajax dynamic loading data, everyone is used to a way of thinking: creating a row of data.
If the data is loaded at a time when the number is large, the browser will be stuck for half a day.
Inspired by the Flex DataGrid Control, in the Flex DataGrid Control, the method for displaying data is not to create as many rows as there are many data records, it can only create a dozen or twenty rows you can see on the interface (assuming n rows). If there is too much data, during the scrolling process, the n rows that you should see will be extracted from the data and re-displayed in the created n rows control.
That is to say, in the DataGrid Control of Flex, we actually see only the n-row controls, but the data they display is filtered out based on the scroll bar status.
Therefore, if a similar method is used in JS, tens of thousands of data records may be used to create dozens of Dom records, which is much more efficient.
Let's talk about the code. First, a scroll bar is required.
Scrollbar. js
The Code is as follows:
Function Scrollbar (){
This. options = {
Total: 0, // total data
Pos: 0, // current rolling position
ItemSize: 20, // individual 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 the content height
Var ch = _ this. options. total * _ this. options. itemSize;
_ This. content. style. height = ch + "px ";
}
// Obtain the scroll position
Function getPos (){
Var top = this. bar. scrollTop;
Var pos = parseInt (top/this. options. itemSize );
Return pos;
}
// Number of data displayed on each page
Function getPageItems (){
Return this. options. size/this. options. itemSize;
}
// Rolling Event Response
Function OnScroll (_ this ){
Var pos = _ this. getPos ();
If (pos = _ this. options. pos)
Return;
_ This. options. pos = pos;
_ This. onScroll (pos );
}
// Create a scroll bar
Function CreateAt (dom ){
Var _ this = this;
Var bar = document. createElement ("p ");
Var content = document. createElement ("p ");
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 compatibility
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 a scroll bar event
};
})();
Page code:
The Code is as follows:
<BR> Table Control <BR>