既存のタグから選択するか新しいタグを追加できるタグ付けシステムを実装しました。新しいタグが選択されると、AJAX呼び出しを使用して永続化されます。Select2:新しいタグを選択した後の更新オプション
これを達成するために、私はコールバックcreateTag
とイベントselect2:select
を使用します。私が選択されたときにのみタグを作成するのが好きなので、イベントselect2:select
がトリガーされたら、私はAJAXコールを行います。
問題は、すでに作成されたselect2のオプションを、新しいタグをデータベースに保存して取得したIDで更新する必要があることです。これに対して最もクリーンなソリューションは何ですか?
は、ここで私が持っているものです。
$('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");
});
タグが作成されたら、変更イベントをトリガしようとしましたか? – BuddhistBeast
@BuddhistBeastこれは私を正確に助けますか?このイベントに耳を傾ければ私のIDはすでに消えてしまっていますか?または、selectイベントの代わりにchangeイベントをlistenする必要がありますか? – TiMESPLiNTER
related http://stackoverflow.com/questions/23295168/how-do-i-fire-a-new-ajax-on-select2-new-remove-tag-event –