简体   繁体   English

设置多个Select2选项

[英]Set multiple Select2 options

I have a form to create new Objects, which gives the possibility to add multiple Keywords via a Select2. 我有一个创建新对象的表单,这使通过Select2添加多个关键字成为可能。

I use Symfony ; 我用Symfony; the Keywords are taken from an entity. 关键字取自实体。 Adding existing keyword in the Select2 list work perfectly fine. 将现有关键字添加到Select2列表中可以很好地工作。

Now I want to give the user the possibility to add brand new keywords, and that those new keywords directly appears in the Select2 list. 现在,我希望为用户提供添加全新关键字的可能性,并使这些新关键字直接出现在Select2列表中。

So I created a "Add Keyword" button on my "new object form", which leads to a "New Keyword" form (a Bootstrap modal window). 因此,我在“新对象表单”上创建了一个“添加关键字”按钮,这导致了“新关键字”表单(Bootstrap模态窗口)。

The Javascript used to add the newly created keyword to the Select2 list is the following: 用于将新创建的关键字添加到Select2列表的Javascript如下:

function OnSuccess(result){
    var keywordsSelected = new Array($("#object_keywords").select2("val")); 
    $('#object_keywords').append('<option value="'+result.id+'" selected="selected">'+result.name+'</option>');
    keywordsSelected.push(result.id);
    $("#object_keywords").select2("val", keywordsSelected); // 
    $('#addKeyword').modal('hide');
}

Now the weird thing: 现在奇怪的是:

  • I click on my "Add keyword" button, I fill the name of the new keyword in the "new Keyword" form, I validate, the new keyword is added in the Select2 list --> everything is fine 我单击我的“添加关键字”按钮,在“新关键字”表单中填写新关键字的名称,我确认,新关键字已添加到Select2列表中->一切都很好
  • I do the same thing another time --> everything is fine, the second new keyword is added to the Select2 list near the previous one 下次我也做同样的事情->一切都很好,第二个新关键字被添加到上一个关键字附近的Select2列表中
  • I do the same thing a third time and... the third keyword is added but the two first disappear !! 我第三次做同样的事情,...添加了第三个关键字, 但是第一个关键字消失了

This appends even if the 2 fisrt are "old" keywords already existing in the DB, but this doesn't append if I add only "old" keywords (I can add as many as I want). 即使2个fisrt是数据库中已经存在的“旧”关键字,此操作也会追加,但是如果我仅添加“旧”关键字(我可以添加任意多个),则不会追加。

Do you have any idea where the problem could come from? 您是否知道问题可能来自何处?

Well... 好...

I continued to search and by chance I found something which seems to work (ie I didn't find a case where it doesn't, yet). 我继续搜索,偶然发现了一些似乎有用的东西(即,我还没有找到一个行不通的情况)。

I changed the order of the lines and added a "clear Select2" instruction: 我更改了行的顺序,并添加了“ clear Select2”指令:

function OnSuccess(result){
    $('#addKeyword').modal('hide');  
    $('#webobs_eventbundle_event_keywords').append('<option value="'+result.id+'" selected="selected">'+result.name+'</option>'); // Add the new Keyword in the list
    var keywordsSelected = $("#webobs_eventbundle_event_keywords").select2("val");
    keywordsSelected.push(result.id);
    $("#webobs_eventbundle_event_keywords").select2("val", null);
    $("#webobs_eventbundle_event_keywords").select2("val", keywordsSelected);     
}

Anyway, I'm still interested in you opinion about this problem, because I don't really see the reason why this code works and not the first one... 无论如何,我仍然对您对此问题的看法感兴趣,因为我没有真正看到此代码起作用的原因,而不是第一个代码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM