1

だから私は、jsonから事前入力された結果があるselect2ボックスに外部データをロードすることを検討しています

私には2つの問題があります-データを取得してから最初のいくつかのみをロードしますが、ユーザーが検索するとリクエストが残りに渡され、特定のjsonが返されます

したがって、HTMLは次のように単純です

<input type="hidden" id="e21" style="width:300px;"/> 

初期jsは

$(document).ready(function () {
    $('#e21').select2({
        query: function (query){
        var data = {results: []};
        console.log(data);
        $.each(preload_data, function(){
            if(query.term.length == 0 || this.text.toUpperCase().indexOf(query.term.toUpperCase()) >= 0 ){
                data.results.push({id: this.id, text: this.text });
            }
        });

        query.callback(data);
        }
    });

    $('#e21').select2('data', preload_data );

});

それがすることは preload_data にロードされるので、

var preload_data = [
{ id: 'user0', text: 'Disabled User'}, 
{ id: 'user1', text: 'Jane Doe'},
{ id: 'user2', text: 'John Doe', locked: true },
{ id: 'user3', text: 'Robert Paulson', locked: true },
{ id: 'user5', text: 'Spongebob Squarepants'},
{ id: 'user6', text: 'Planet Bob' },
{ id: 'user7', text: 'Inigo Montoya' }
];

例はここにあります

http://jsfiddle.net/dwhitmarsh/MfJ4B/10/

ただし、preload_data の代わりに、ajax 呼び出しでロードしてロードし、結果を渡したい

だから私は使うことができました

var preload_data = $.ajax({
 url: 'data/more.json',
 method: 'get',
 dataType: 'json'
}); 

しかし、json がロードされるのを待つ必要があり、最初の 10 個だけをロードしたいと考えています。

次に、ユーザーが実際に検索するときに、次のような追加の変数を渡したい

string: term, //search term
page_limit: 10, // page size
page: page // page number

ページはselect 2の一部です

誰でも助けることができますか?

ところで、more.json は次のようになります

{
    "total": 8, 
    "result": [{
            "id": "1",
            "label": "Type",
            "safeName":"type",
            "jsonFile": "type.json"

    },{
            "id": "2",
            "label": "Primary Applicant Name",
            "safeName":"primaryApplicantName",
            "jsonFile": "primary.json"
    },{
            "id": "3",
            "label": "Address",
            "safeName":"address",
            "jsonFile": "address.json"
    },{
            "id": "4",
            "label": "Product",
            "safeName":"product",
            "jsonFile": "product.json"
    },{
            "id": "5",
            "label": "Verification",
            "safeName": "verification",
            "jsonFile": "verification.json"

    },{
            "id": "6",
            "label": "Documents",
            "safeName": "documents",
            "jsonFile": "documents.json"
    },{
            "id": "7",
            "label": "Suburb",
            "safeName": "suburb",
            "jsonFile": "suburb.json"
    },{
            "id": "8",
            "label": "State",
            "safeName": "state",
            "jsonFile": "state.json"


    }]
}
4

0 に答える 0