1

所以我正在研究在一个 select2 框中加载外部数据,其中有来自 json 的预填充结果

我有两个问题 - 获取数据,然后只加载前几个,但是当用户搜索时,它将请求传递给其余的,然后返回特定的 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' }
];

一个例子是here

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

页面是选择 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