js上萬條數據瞬間渲染到頁面,通過table表格演示


主頁面文件html

var data = [];
        //創建一萬條示例數據
        for (var i = 0; i < 10000; i++) {
            var row = { id: i, text: "text" + i };
            data.push(row);
        }

        //創建滾動條
        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);

            //僅創建所看到的幾十行表格,所以自然快得多
            var list = document.getElementById("tblList");
            for (var i = 0; i < data.length && i < items; i++) {
                var nr = tpl.cloneNode(true);   //從模板行復制新行
                list.appendChild(nr);
            }
            ShowData(scrbar.getPos());
        }

        //根據滾動條,展示數據
        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"];
            }
        }

 

Scrollbar.js文件

function Scrollbar() {
    this.options = {
        total: 0,   //數據總數
        pos: 0,     //當前滾動位置
        itemSize: 20,  //單項尺寸
        size: 500  //控件尺寸
    };
}
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;

        //設置控件高度
        _this.bar.style.height = _this.options.size + "px";

        //設置內容高度
        var ch = _this.options.total * _this.options.itemSize;
        _this.content.style.height = ch + "px";
    }
    //獲取滾動位置
    function getPos() {
        var top = this.bar.scrollTop;
        var pos = parseInt(top / this.options.itemSize);
        return pos;
    }
    //每頁可展示的數據數量
    function getPageItems() {
        return this.options.size / this.options.itemSize;
    }

    //滾動事件響應
    function OnScroll(_this) {
        var pos = _this.getPos();
        if (pos == _this.options.pos) return;
        _this.options.pos = pos;
        _this.onScroll(pos);
    }

    //滾動條創建
    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  //模擬滾動條事件
    };
})();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM