简体   繁体   English

Select2下拉列表动态添加,删除和刷新项目

[英]Select2 Dropdown Dynamically Add, Remove and Refresh Items from

This drives me crazy! 这让我抓狂! Why cant Select2 implement clear methods or examples on their page how to do simple CRUD manipulation on Select2 :) 为什么不能在其页面上实现清晰的方法或示例如何在Select2上进行简单的CRUD操作:)

i have a select2 which gets data from a ajax call. 我有一个select2从ajax调用获取数据。

<input id="valueg" type="hidden" style="width: 300px;" data-placeholder="Select a Conference" />

$("#valueg").select2({
             data: conferences,
             allowClear: true,
             initSelection: function (element, callback) {
                            var data = { id: element.val(), text: element.val() };
                            callback(data);
                        }
}).on("change", function (e) {
 // show data in separate div when item is selected               
});

Can some one provide a clear method how to delete and add an item from the Select2. 有人可以提供一个清晰的方法来从Select2中删除和添加项目。

For example: 例如:

I add an item via ajax call to db, and just want to append an option to Select2 and set it as selected. 我通过ajax调用向db添加一个项目,只想在Select2中附加一个选项并将其设置为选中状态。

I remove an item via ajax to db, and want to remove an option from Select2 and have no option selected. 我通过ajax删除项目到db,并希望从Select2中删除一个选项,并且没有选择任何选项。

From your example I can see that you attached Select2 plugin to hidden element. 从您的示例中我可以看到您将Select2插件附加到隐藏元素。 In addition you use Ajax call to populate results, so in this case if you need to add a new option to the list you simple call: 此外,您使用Ajax调用来填充结果,因此在这种情况下,如果您需要向列表添加新选项,则可以简单地调用:

$('#valueg').select2('val', 'YOUR_VALUE');

In case of using select element as a container the only way that I found is to reinitialize plugin with new options... Something like this: 如果使用select元素作为容器,我找到的唯一方法是使用新选项重新初始化插件...这样的事情:

var el = $('select[name="type"]', '#details-form');
var temp = el.select2('val'); // save current value
temp.push(NEW_VALUE); // append new one
var newOptions = '<option value="1">1</option><option value="2">2</option>';
el.select2('destroy').html(newOptions ).select2().select2('val', temp);

I had the same issue. 我遇到过同样的问题。 This is how i solved it 这就是我解决它的方式

This has nothing to do with select2, manipulating the itself seems to work for me 这与select2无关,操纵它本身似乎对我有用

 $("#lstService").empty();
  for(var i=0;i<data.length;i++){
     $("#lstService").append('<option value="'+data[i].service_id+'">'+data[i].service_name+'</option>');
 }

I did it this way: 我是这样做的:

var $select2 = $('#valueg').select2(); // getting the select2
var item = 'xyz'; // item to be added
$select2.val(function(i, val) { // val can take function as parameter
  val = val || []; // if value is null init val as an array
  val.push(item); // add the new item
  return val;
}).trigger("change"); //refresh

Hope that helps 希望有所帮助

It is too late... but this is my solution for people that still have this problem: 现在为时已晚......但对于仍有此问题的人来说,这是我的解决方案:

html = "";
jQuery("#select_2 option").each(function()
{
    html += '<option value="'+jQuery(this).attr("value")+'">'+jQuery(this).text()+'</option>';
});
html += '<option  value="NEWVALUE">NEW OPTION</option>';
jQuery("#select_2").html(html);

In my case, the key thing to add after manipulating the underlying select is: 在我的例子中,操作底层选择后添加的关键是:

$selectlist.trigger("change"); //$selectlist is the underlying select element. // $ selectlist是底层的select元素。

Select2 works around the change event on a select, so calling "change" updates the option displayed in the select 2. Select2适用于选择的更改事件,因此调用“更改”会更新选择2中显示的选项。

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

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