简体   繁体   English

如何使用initSelection附加jQuery select2值

[英]How to append jquery select2 values with initSelection

Here is my select2 multiple select with ajax. 这是我的使用ajax的select2多重选择。

Initially i am setting some values in the initselection like this 最初,我在像这样的initselection中设置一些值

initSelection : function (element, callback) {
    var data = {id: 4, zipcode: "00602"};
    callback(data);
};

And then i am doing the ajax selection. 然后我正在做ajax选择。

But i can't able to sustain the old values. 但是我无法维持旧的价值观。 ie, when i type the new selection, the old values are replaced instead of appending . 即,当我键入新选择时,将替换旧值而不是追加。

Here is my entire code. 这是我的整个代码。

var $select = $('#firstList');
$select.select2({
    placeholder : "Enter Pincode",
    allowClear : true,
    cache : true,
    initSelection : function(element, callback) {
        var data = {
            id : 4,
            zipcode : "00602"
        };
        callback(data);
    },
    ajax : {
        url : "../getZipList",
        type : "POST",
        contentType : "application/json; charset=utf-8",
        delay : 250,
        data : function(params) {
            //console.log(params)
            return {
                q : params.term, // search term
                page : params.page
            };
        },
        processResults : function(data, page) {
            // parse the results into the format expected by Select2.
            // since we are using custom formatting functions we do not need to
            // alter the remote JSON data
            return {
                results : data.items
            };
            //console.log(data.it)
        },
        cache : true
    },
    escapeMarkup : function(markup) {
        return markup;
    }, // let our custom formatter work
    minimumInputLength : 1,
    templateResult : formatRepo, // omitted for brevity, see the source of this page
    templateSelection : formatRepoSelection // omitted for brevity, see the source of this page
});

I am using select2 4 version. 我正在使用select2 4版本。

Here is the formatrepo 这是formatrepo

function formatRepo(repo) {
    if (repo.loading)
        return repo.text;
    var markup = '<div class="clearfix">' + '<div clas="col-sm-10">' + '<div class="clearfix">' + '<div class="col-sm-6">' + repo.zipcode + '</div>' + '</div>';

    markup += '</div></div>';
    return markup;
}

function formatRepoSelection(repo) {
    console.log(repo);
    return repo.zipcode;
    var cur_val = $('#zipcodeCollection').val();
    console.log(repo.zipcode);
    if (cur_val) {

        $('#zipcodeCollection').val(cur_val + "," + repo.zipcode);
    } else {

        $('#zipcodeCollection').val(repo.zipcode);
    }
    return repo.zipcode;

}

What should i do to retain the old values and append it to the new values. 我应该怎么做才能保留旧值并将其附加到新值。

In processResults method you need to add your initial data in return : processResults方法中,您需要添加初始数据作为return

data.items[data.items.length] = initialdata; results: data.items

initialdata has to be in global/object scope. initialdata必须在全局/对象范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM