jQuery select2:AJAX 多选中的默认选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16142884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:32:04  来源:igfitidea点击:

select2: default selection in multiple select with AJAX

jqueryjquery-select2

提问by Gabriel Sanmartin

I am using the awesome select2to create a multiple selection combo.

我正在使用很棒的select2来创建一个多选组合。

I would like to programmatically select default values (as in selections the user has made previously), but I don't know how. I read that using initSelection does the trick but this is called when creating the combo and I don't want this default selection to be always done.

我想以编程方式选择默认值(如用户之前所做的选择),但我不知道如何选择。我读到使用 initSelection 可以解决问题,但是在创建组合时会调用它,我不希望始终完成此默认选择。

回答by Ramin Firooz

right solution in version 4.x by example:

4.x 版中的正确解决方案示例:

$('#element').val(['val1','val2']).trigger('change');

see: Creator description

参见:创作者描述

回答by sanj

I found some solutions, maybe that will help you:

我找到了一些解决方案,也许会对您有所帮助:

$('#el').select2({...}) // init select2

var defaultData = [{id:1, text:'Item1'},{id:2,text:'Item2'},{id:3,text:'Item3'}];

$('#el').data().select2.updateSelection(defaultData);

回答by sanj

From the documentation page:

从文档页面:

"val- Gets or sets the selection. If the value parameter is not specified, the id attribute of the currently selected element is returned. If the value parameter is specified it will become the current selection."

" val- 获取或设置选择。如果未指定 value 参数,则返回当前所选元素的 id 属性。如果指定了 value 参数,它将成为当前选择。"

Also, from the docs:

此外,来自文档:

$("#e8").select2("val", "CA");

回答by Thomas Clowes

As outlined in the documentation here, you can simply add 'options' to your select markup to have initial default values preselected.

正如此处的文档中所述,您可以简单地将“选项”添加到您的选择标记中,以预先选择初始默认值。

<select class="js-data-example-ajax">
  <option value="3620194" selected="selected">select2/select2</option>
</select>

回答by sebap

$('#el').select2().val(['val1','val2']).trigger("change")

$('#el').select2().val(['val1','val2']).trigger("change")

does the trick but for some reasons it removes the ability to add your own tag...

这样做的技巧,但由于某些原因,它删除了添加自己的标签的能力......

回答by longJOURNEY

I looped through an array of values, Then set each option that matched to ("selected",true)

我遍历了一组值,然后设置匹配到 ("selected",true) 的每个选项

values = [2, 5, 3]

for val in values
  $("#selects_id option[value="+val+"]").prop("selected",true).trigger("change")

You can also do with the results you get from an Ajax call.

您还可以使用从 Ajax 调用中获得的结果。

回答by Fayez Barbari

The following code works fine, but the select2need to be refreshed

以下代码工作正常,但select2需要刷新

$('#el').data().select2.updateSelection(defaultData);

回答by Louwki

To extend on @sanj answer. This seem to be working for me in my projects.

扩展@sanj 答案。这似乎在我的项目中对我有用。

Via HTML

通过 HTML

HTML

HTML

<select class="select2" style='width:100%;' multiple>
    <option val=''>Please choose</option>
    <option val="1" selected>One</option>
    <option val="2" selected>Two</option>
    <option val="3">Three</option>
</select>

JS

JS

$('.select2').select2();

Results enter image description here

结果 enter image description here

Via Javascript

通过 Javascript

HTML

HTML

<select class="select2" style='width:100%;' multiple>
    <option val=''>Please choose</option>
    <option val="1">One</option>
    <option val="2">Two</option>
    <option val="3">Three</option>
</select>

JS

JS

$('.select2').select2();
var defaultData = [{id:1, text:'One'},{id:2,text:'Two'}];
$('.select2').data().select2.updateSelection(defaultData);

Results enter image description here

结果 enter image description here

回答by absin

Figured it out finally!! Select2 iterates through the <option>tag under the <select>.

终于想通了!!选择二迭代通过<option>下的标签<select>

<select class="js-data-example-ajax" style="width:100%">
  <option value="3620194" selected="selected">Put the pre-selected options here</option>
</select>

The inner html of the <option>is put into the repo object under the text key. So repo.textwill give the innerhtml. That is, all we have to do is supply repo.textto the formatSelectionoption in the init. Also the json being passed from the servlet (controller) should have the desired label text put under the textkey!! Still figuring out how to add attributes to the li elements created, so that I can save the changed list. Any ideas?

的内部html<option>放入文本键下的repo对象中。所以repo.text会给innerhtml。也就是说,我们所要做的就是提供repo.textformatSelectioninit 中的选项。此外,从 servlet(控制器)传递的 json 应该将所需的标签文本放在text键下!!仍在弄清楚如何向创建的 li 元素添加属性,以便我可以保存更改后的列表。有任何想法吗?

回答by Arun Banik

You can find a solution here with An auto complete, multiple selection Drop-down listusing JQuery Select2.

您可以在此处找到使用 JQuery Select2的自动完成、多选下拉列表的解决方案。

Binds data with an HTML "select" element which is then taken over by "Select2" functions to convert it into a beautiful multi-select drop-down list.

将数据与 HTML“select”元素绑定,然后由“Select2”函数接管,将其转换为漂亮的多选下拉列表。

It might perform well using AJax and will sure try in the coming days.

它可能使用 AJax 表现良好,并且肯定会在未来几天内尝试。