Select2在HTML中设置选定的值

时间:2015-10-08 23:39:09

标签: javascript jquery-select2

有没有办法可以在HTML中的select2中设置所选的id / text,就像显示和选择一样?

我正在使用jquery select2版本4.0.0

这是代码:

$("#users").select2({
    placeholder: "select user...",
    escapeMarkup: function (markup) { return markup; },
    ajax: {
        url: "users.json",
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                q: params.term, // search term
                page: params.page
            };
        },
        processResults: function (response, page) {
            return {
                results: response.data
            };
        },
        cache: true
    },
    templateResult: function(item){
        if (item.loading) {
            return item.text;
        }
        return item.username + " <small><i>(" + item.role  + ")</i></small> "; // display data in html
    },
    templateSelection: function(item){
        if (item.username){
            return item.username + " <small><i>(" + item.role  + ")</i></small> ";  // select row in html
        }
        return item.text; // for placeholder
    }
});

要设置所选值,我会这样做:

// bring data for to set selected value, data contains fields data
var selected_user_id = data.id,

// this is the content that I need to use as it is HTML content
selected_user_text_html = data.username + " <small><i>(" + data.role  + ")</i></small> ",

// this is the one that I am using and is not html because there is no way to put html inside a option select item
selected_user_text_regular = data.username + " (" + data.role  + ") ";

// set selected value
$("#users").append("<option value='"+selected_user_id+"' selected='selected'>"+selected_user_text_regular+"</option>");

// trigger update
$("#users").trigger("change");

有没有办法可以用HTML而不是纯文本设置所选文本?

2 个答案:

答案 0 :(得分:3)

是的,你可以。 Select2有两个有用的选项“ templateSelection ”和“ templateResult ”两个都是函数,你可以创建任何你想要的html包装器。 只需将选项传递给select2()方法,就像这样..

var data = [{id: 100, text: "AAAAAA"}, {id: 200, text: "BBBB"}]

var formatState = function(result) {return $("<b>"+result.text+"</b>")}
var formatSelection = function(selection){return $("<b>"+selection.text+"</b>") }

$(".js-example-basic-single").select2({ data:data,templateResult: formatState, templateSelection: formatSelection
    }).val(100).change();

您应该会看到选项ID为100的选项。

我希望我能帮到你;

答案 1 :(得分:0)

如果我理解正确的话,

根据api https://select2.github.io/examples.html ,您可以通过将其置于html标记中然后调用javascript

来设置所选内容
the function cider-find-var is not known

<强>更新

假设在声明上调用ajax

当ajax返回时,你是否能够附加选项。

var data = [
     { id: 0, text: 'enhancement' },
     { id: 1, text: 'bug' },
     { id: 2, text: 'duplicate' },
     { id: 3, text: 'invalid' },
     { id: 4, text: 'wontfix' }
 ]

$(".example").select2({
  data: data
})

<select class="example">
    <option value="3" selected="selected">invalid</option>
</select>

同时更新

请考虑以下内容以替换您的其他代码或提供您的想法

ajax: {
 ...
    processResults: function (response, page) {

           //something like
           $.each( response.data, function( i, obj ) {
                if(obj.selected)
                {
                    var elem = $('#users')
                    elem.append($('<option>', { 
                        value: obj.value,
                        text : obj.text,
                        selected : "selected"
                    }));
                }
           })


        return {
            results: response.data
        };
    },
    cache: true
},