Select2 tagging with prefilled tags and remote selection

42 views
Skip to first unread message

nl...@corp.oodle.com

unread,
Aug 4, 2015, 12:21:44 PM8/4/15
to select2
Hello! I’m working on a project using select2 v4 tagging support.
<select id="tags" name="tags[]" />

I hope to have a few prefilled tags, then have the user be able to select from remote values as well as enter new ones. I got the tagging to work with pre filled values:
var app_tags=['red', 'green'];
$('select#tags').select2({
tags: app_tags,
multiple: true,
tokenSeparators: [',']
});

$("select#tags").select2('val', app_tags);

Then I added the ajax:
var app_tags=['red', 'green'];
$('select#tags').select2({
tags: app_tags,
multiple: true,
tokenSeparators: [‘,’],
createSearchChoice: function(term, data) {

if ($(data).filter(function() {

return this.text.localeCompare(term) === 0;

}).length === 0) {

return {

id: term,

text: term

};

}

},

ajax: {

url: “/some/url”,

 dataType: 'json',

delay: 250,

data: function (params) {

 return {

 q: params.term

};

},

processResults:
function(data){

return {

results: $.map(data, function (item) {

 return item;

 })

};

},

 cache: true

},

 minimumInputLength: 2

});

$("select#tags").select2('val', app_tags);


I can now select from remote list and enter new values very well, but the prefilled tags no longer exist. How do I make them show?

Thanks!

nl...@corp.oodle.com

unread,
Aug 4, 2015, 1:15:04 PM8/4/15
to select2, nl...@corp.oodle.com
Oops sorry this totally works. It just has to be
<select id="tags" name="tags[]"></select>
instead of

<select id="tags" name="tags[]" />

Thanks!

Reply all
Reply to author
Forward
0 new messages