createTag
和事件select2:select
。因为我只想在选定标记时创建标记,所以如果触发了select2:select
事件,我会对此执行AJAX调用。
问题是,我需要使用我从数据库中持久化新标记获取的ID更新已创建的select2选项。这个问题最清晰的解决方案是什么?
以下是我的代码:
$('select.tags').select2({
tags: true,
ajax: {
url: '{{ path('tag_auto_complete') }}',
processResults: function (data) {
return {
results: data.items,
pagination: {
more: false
}
};
}
},
createTag: function (tag) {
return {
id: tag.term, // <-- this one should get exchanged after persisting the new tag
text: tag.term,
tag: true
};
}
}).on('select2:select', function (evt) {
if(evt.params.data.tag == false) {
return;
}
$.post('{{ path('tag_crrate_auto_complete') }}', { name: evt.params.data.text }, function( data ) {
// ----> Here I need to update the option created in "createTag" with the ID
option_to_update.value = data.id;
}, "json");
});