I am using the latest version of Select2: Select2 4.0.
I would like to allow users to enter free text. In other words if a user cannot find a option in the drop down (data returned by ajax), I want them to be able to 'select' whatever text they have typed in.
This is my markup:
<select class="required form-control" id="businessName" data-placeholder="Choose An Name" > </select>
And this is the JavaScript that I am using to initialize Select2:
$("#businessName").select2({
ajax: {
url: "/register/namelookup",
dataType: 'json',
delay: 250,
type: 'post',
data: function (params) {
return {
businessName: params.term, // search term
page: params.page
};
},
processResults: function (data, page) {
return {
results: data.items
};
},
cache: false
},
escapeMarkup: function (markup) { return markup; },
minimumInputLength: 4,
createSearchChoice:function(term, data) {
if ( $(data).filter( function() {
return this.text.localeCompare(term)===0;
}).length===0) {
return {id:term, text:term};
}
},
});
I added createSearchChoice
but it doesn't work. I have looked at this answer as well but so far no luck.