javascript - jquery select2 add text to source if not found in source - Stack Overflow

I want select2 to behave as sort of bobox.See attached image for ref.So if user types in a string but

I want select2 to behave as sort of bobox.See attached image for ref. So if user types in a string but it is not found in source array, then on enter or closing select2 it should add that new string to source. So say if there were 2 values before, after above there would be now 3.

select2 just like bobox in file open dialogs

I have created sample code, but cant make it to work.I am unable to update source.

JSFIDDLE:

HTML:

<div class="row">
    <div class="col-md-2">
        <input type="hidden" id="select2_sample3" class="form-control ">
    </div>
</div>

JS:

$(function () {

    var preload_data = {
        results: [{
            id: 'user0',
            text: 'Disabled User',
        }, {
            id: 'user1',
            text: 'Jane Doe'
        }]
    };

    $("#select2_sample3").select2({
        placeholder: "Select...",
        allowClear: true,
        minimumInputLength: 1,
        data: preload_data,
        query: function (query) {
            var data = {
                results: []
            }, i, j, s;
            for (i = 1; i < 5; i++) {
                s = "";
                for (j = 0; j < i; j++) {
                    s = s + query.term;
                }
                data.results.push({
                    id: query.term + i,
                    text: s
                });
            }
            query.callback(data);
        }

    }).on("select2-close", function () {
        // add to 
        console.log("close");
    });
});

I want select2 to behave as sort of bobox.See attached image for ref. So if user types in a string but it is not found in source array, then on enter or closing select2 it should add that new string to source. So say if there were 2 values before, after above there would be now 3.

select2 just like bobox in file open dialogs

I have created sample code, but cant make it to work.I am unable to update source.

JSFIDDLE:

HTML:

<div class="row">
    <div class="col-md-2">
        <input type="hidden" id="select2_sample3" class="form-control ">
    </div>
</div>

JS:

$(function () {

    var preload_data = {
        results: [{
            id: 'user0',
            text: 'Disabled User',
        }, {
            id: 'user1',
            text: 'Jane Doe'
        }]
    };

    $("#select2_sample3").select2({
        placeholder: "Select...",
        allowClear: true,
        minimumInputLength: 1,
        data: preload_data,
        query: function (query) {
            var data = {
                results: []
            }, i, j, s;
            for (i = 1; i < 5; i++) {
                s = "";
                for (j = 0; j < i; j++) {
                    s = s + query.term;
                }
                data.results.push({
                    id: query.term + i,
                    text: s
                });
            }
            query.callback(data);
        }

    }).on("select2-close", function () {
        // add to 
        console.log("close");
    });
});
Share Improve this question edited Apr 2, 2014 at 6:53 joker 9909 silver badges23 bronze badges asked Apr 2, 2014 at 6:51 djangodjango 2,9096 gold badges51 silver badges83 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

I've recently had the same task. This is my solution:

<input type="hidden" name="trBrand" id="trBrand" class="form-control"></input>

and js code

$('#trBrand').select2({
    placeholder: 'choose something',
    allowClear: true,
    id: function(object) {
        return object.text;
    },
    createSearchChoice:function(term, data) {
        if ( $(data).filter( function() {
            return this.text.localeCompare(term)===0;
        }).length===0) {
            return {id:term, text:term};
        }
    },
    data:preload_data
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744765286a4592420.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信