1

多数のレコードを含む JSON 結果があります。最初のものを表示したいのですが、2番目のものを表示するための次のボタンが必要です。ページを更新したくないので、JavaScript、jQuery、さらにはサードパーティの AJAX ライブラリの組み合わせが役立つことを願っています。

助言がありますか?

4

3 に答える 3

5

お役に立てれば:

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);
};
于 2009-02-05T17:36:48.023 に答える
2

私はこの目的のためだけにjqgridを使用します。チャームのように機能します。

http://www.trirand.com/blog/

于 2009-02-05T16:57:40.023 に答える
2

私は個人的にjsonデータをグローバル変数にロードし、そのようにページングします。調査データのコンテキストに関する私の仮定を気にしないでください。昨日のことを覚えていると思います。

var surveyData = "[{prop1: 'value', prop2:'value'},{prop1: 'value', prop2:'value'}]"
$.curPage = 0;

$.fn.loadQuestion = function(question) {
    return this.each(function() {
        $(this).empty().append(question.prop1);
        // other appends for other question elements
    });
}

$(document).ready(function() {
    $.questions = JSON.parse(surveyData);  // from the json2 library json.org
    $('.questionDiv').loadQuestion($.questions[0]);     

    $('.nextButton').click(funciton(e) {
        if ($.questions.length >= $.curPage+1)
            $('.questionDiv').loadQuestion($.questions[$.curPage++]);
        else
            $('.questionDiv').empty().append('Finished');
    });
});

~ 未テスト

アンケートを処理するためのプラグイン全体を作成する @sktrdie アプローチがいいと認めざるを得ません。IMOこの方法は、実際には抵抗が最も少ない解決策です。

于 2009-02-05T17:29:03.360 に答える