编程语言
首页 > 编程语言> > javascript – 使用jQuery对记录进行分页

javascript – 使用jQuery对记录进行分页

作者:互联网

我有一个包含大量记录的JSON结果.我想显示第一个,但有一个下一个按钮来查看第二个,依此类推.我不希望页面刷新,这就是为什么我希望JavaScript,jQuery甚至第三方AJAX库的组合可以提供帮助.

有什么建议么?

解决方法:

希望这可以帮助:

var noName = {
    data: null
    ,currentIndex : 0
    ,init: function(data) {
        this.data = data;
        this.show(this.data.length - 1); // show last
    }
    ,show: function(index) {
        var jsonObj = this.data[index];
        if(!jsonObj) {
            alert("No more data");
            return;
        }
        this.currentIndex = index;
        var title = jsonObj.title;
        var text = jsonObj.text;
        var next = $("<a>").attr("href","#").click(this.nextHandler).text("next");
        var previous = $("<a>").attr("href","#").click(this.previousHandler).text("previous");

        $("body").html("<h2>"+title+"</h2><p>"+text+"</p>");
        $("body").append(previous);
        $("body").append(document.createTextNode(" "));
        $("body").append(next);
    }
    ,nextHandler: function() {
        noName.show(noName.currentIndex + 1);
    }
    ,previousHandler: function() {
        noName.show(noName.currentIndex - 1);
    }
};

window.onload = function() {
    var data = [
        {"title": "Hello there", "text": "Some text"},
        {"title": "Another title", "text": "Other"}
    ];
    noName.init(data);
};

标签:javascript,jquery,json,ajax,paging
来源: https://codeday.me/bug/20191006/1858859.html