I'm using the "createTag" option of the Select2 plugin to allow the user to type in the search and also allow to include what was typed as a selected option.
But it only works when options already exist in select. If it is empty (returning values by Ajax) does not allow creating the new option.
$('.nova_despesa_mensal.pago_a').select2({language:"pt-BR",
width: '100%',
ajax: {
url: '/financeiro/transacoes/contatos_financeiro',
dataType: 'json'
},
tags: true,
// add "(new tag)" for new tags
createTag: function (params) {
var term = $.trim(params.term);
if (term === '') {
return null;
}
return {
id: term,
text: '+ Adicionar ' + term,
isNew : true
};
}
});