Home >Web Front-end >JS Tutorial >Js table tens of thousands of data instant loading implementation code_javascript skills

Js table tens of thousands of data instant loading implementation code_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:59:09943browse

Js form, tens of thousands of data are loaded instantly

In the actual application of Ajax dynamically loading data, everyone is accustomed to a way of thinking: one piece of data creates one row.
So if the amount of data is large and the data needs to be loaded at once, the browser will be stuck for a long time

Inspired by the Flex DataGrid control, in the Flex DataGrid control, the method of displaying data is not the same. It does not create as many rows as there are pieces of data. It only creates at most a dozen or twenty rows (assumed to be n rows) that you see on the interface. If there is a lot of data, you will be extracted from the data during the scrolling process. The n rows of data that should be seen are re-displayed in the n rows of controls that have been created.
In other words, in Flex's DataGrid control, what we actually see are only the n rows of controls, but the data they display is filtered out based on the scroll bar status.

So, if a similar method is used in JS, then even if there are tens of thousands of data, you may only need to create dozens of Dom, and the efficiency will naturally be much faster.
Without further ado, let’s get into the code. First, you need a scroll bar
Scrollbar.js

Copy code The code is as follows:

function Scrollbar() {
this.options = {
total : 0, //Total number of data
pos : 0, //Current scroll position
itemSize : 20, //Single item 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";
}
//Get the scroll 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 on each page
function getPageItems() {
return this.options.size / this.options.itemSize;
}
//Scroll event response
function OnScroll(_this) {
var pos = _this.getPos();
if (pos == _this.options.pos)
return;
_this.options.pos = pos;
_this.onScroll(pos);
}
//Scroll bar 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 scroll bar events
};
})();

Page code:
Copy codeThe code is as follows:




<br>Table Control<br>< ;/title> <br><script src="Scrollbar.js" type="text/javascript"> <br></script> <br><script language="javascript" type="text/ javascript"> <br>var data = []; <br>//Create 10,000 pieces of sample data<br>for (var i = 0; i < 10000; i ) { <br>var row = { <br>id: i, <br>text: "text" i <br>}; <br>data.push(row); <br>} <br>//Create scroll bar<br>var scrbar = new Scrollbar (); <br>window.onload = function() { <br>scrbar.CreateAt("divScroll"); <br>scrbar.setOptions({ <br>total: 10000, <br>size: 300 <br> }); <br>scrbar.onScroll = function(pos) { <br>ShowData(pos); <br>} <br>//Get template <br>var items = scrbar.getPageItems(); <br>var tpl = document.getElementById("trTpl"); <br>tpl.parentNode.removeChild(tpl); <br>//Only create the dozens of rows of tables you see, so it is naturally much faster<br>var list = document.getElementById("tblList"); <br>for (var i = 0; i < data.length && i < items; i ) { <br>var nr = tpl.cloneNode(true); <br> //Copy new rows from template rows<br>list.appendChild(nr); <br>} <br>ShowData(scrbar.getPos()); <br>} <br>//Display data according to the scroll bar<br>function ShowData(pos) { <br>var n = scrbar.getPageItems(); <br>var rows = document.getElementById("tblList").rows; <br>for (var i = 0; i < n; i ) { <br>var row = rows[i]; <br>var item = data[i pos]; <br>row.cells["tdID"].innerHTML = item["id"]; <br>row.cells["tdText"].innerHTML = item["text"]; <br>} <br>} <br></script> <br></head> <br><body> ; <BR><div id="divScroll" style="float:right"> <br></div> <br><table border="1"> <br><!-- Row header --> <br><thead> <br><tr> <br><th> <br>ID <br></th> <br><th> <br>Text <br></th> <br></tr> <br></thead> <br><!--Data display area--> <br><tbody id="tblList" > <br><tr id="trTpl"> <br><td id="tdID"> <br></td> <br><td id="tdText"> <br></td> <br></tr> <br></tbody> <br></table> <br></body> <br></html> <br> </div></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="After the page is loaded, the scroll bar automatically scrolls to a certain position_javascript skills" href="https://m.php.cn/faq/13975.html">After the page is loaded, the scroll bar automatically scrolls to a certain position_javascript skills</a></span><span>Next article:<a class="dBlack" title="After the page is loaded, the scroll bar automatically scrolls to a certain position_javascript skills" href="https://m.php.cn/faq/13977.html">After the page is loaded, the scroll bar automatically scrolls to a certain position_javascript skills</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="https://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://m.php.cn/faq/1609.html" title="An in-depth analysis of the Bootstrap list group component" class="aBlack">An in-depth analysis of the Bootstrap list group component</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1640.html" title="Detailed explanation of JavaScript function currying" class="aBlack">Detailed explanation of JavaScript function currying</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1949.html" title="Complete example of JS password generation and strength detection (with demo source code download)" class="aBlack">Complete example of JS password generation and strength detection (with demo source code download)</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2248.html" title="Angularjs integrates WeChat UI (weui)" class="aBlack">Angularjs integrates WeChat UI (weui)</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2351.html" title="How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills" class="aBlack">How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>Public welfare online PHP training,Help PHP learners grow quickly!</p></div><div class="footermid"><a href="https://m.php.cn/about/us.html">About us</a><a href="https://m.php.cn/about/disclaimer.html">Disclaimer</a><a href="https://m.php.cn/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>