自动从一个 [] 字符串中选择 select2

huangapple go评论114阅读模式
英文:

Auto selecting select2 from a [] string

问题

我的HTML代码片段:

<div class="item form-group">
<label class="col-md-3 col-sm-3 col-xs-12" for="middlename">收款人代码 </label>
 <div class="col-md-6 col-sm-6 col-xs-12">
<select id="payee" name="payee" class="select2_multiple form-control" multiple="multiple">
{{range $key,$val := .payee}}
<option>{{$val.Code}}</option>
{{end}}
</select>
</div>
</div>

这个select2选项是从我从后端golang程序发送的{{.payee}}值中填充的。

现在,我想使用我从程序发送的{{.Code}}变量(它是一个[]string)自动选择这个select2。我想选择与{{.Code}}中的值匹配的所有选项。

请帮助实现select2的自动填充。
提前感谢。

英文:

My HTML code snippet:

&lt;div class=&quot;item form-group&quot;&gt;
&lt;label class=&quot;col-md-3 col-sm-3 col-xs-12&quot; for=&quot;middlename&quot;&gt;Payee Code &lt;/label&gt;
 &lt;div class=&quot;col-md-6 col-sm-6 col-xs-12&quot;&gt;
&lt;select id=&quot;payee&quot; name=&quot;payee&quot; class=&quot;select2_multiple form-control&quot;multiple=&quot;multiple&quot;&gt;
{{range $key,$val := .payee}}
&lt;option&gt;{{$val.Code}}&lt;/option&gt;
{{end}}
&lt;/select&gt;
&lt;/div&gt;
&lt;/div&gt;

This select2 options are populated from the {{.payee}} value i send from backend golang program.

Now i want to auto select this select2 using the {{.Code}} variable (which is a []string) that i send from my program.I want to select all the options that matches with the values in {{.Code}}

Please help in achieving auto fill on select2.
Thanks in advance.

答案1

得分: 1

我不太确定我是否正确理解你想要实现的内容,但是一旦你用选项值填充了你的选择框,如果这是你的目标,你可以将选中的选项添加到每个选项中。

否则,你可以使用条件语句来检查哪些值满足某个条件,只为这些值添加自动选择的选项。

<select id="payee" name="payee" class="select2_multiple form-control" multiple="multiple">
{{range $key,$val := .payee}}
<option>{{$val.Code}}{{if .Code}}selected="selected"{{end}}</option>
{{end}}
</select>
英文:

I'm not really sure if i understand correctly what you want to achieve, but as soon as you populate your select box with the option values, you can add the selected option to each of them, if this is your objective.

Otherwise you can use conditional statement to check which of the values satisfies some condition and only for those of them to add the autoselected option.

&lt;select id=&quot;payee&quot; name=&quot;payee&quot; class=&quot;select2_multiple form-control&quot;multiple=&quot;multiple&quot;&gt;
{{range $key,$val := .payee}}
&lt;option&gt;{{$val.Code}}{{if .Code}}selected=&quot;selected&quot;{{end}}&lt;/option&gt;
{{end}}
&lt;/select&gt;

答案2

得分: 0

我自己找到了答案。这将帮助那些遇到相同问题的人。

首先,错误在于选项标签必须指定值:

<option value={{$val.Code}}>{{$val.Code}}</option>

让我们以一个例子来说明,所有选项(来自$val.Code)的列表是141、152、173、184、188、200,其中141和173需要从{{.Code}}中预先选择。

脚本:

if ({{.Code}} != "")
{
$("#payee").val({{.Code}}).trigger("change");
}

这将选择与{{.Code}}匹配的选项。

感谢大家的帮助。

英文:

I got the answer myself. This will help others who are stuck with the same kind of problem.

First Mistake option tag must have value specified

&lt;option value={{$val.Code}}&gt;{{$val.Code}}&lt;/option&gt;

Let us consider an example the list of all the options(from $val.Code) are 141 , 152, 173, 184, 188 , 200
among these 141 and 173 are to be pre-selected from {{.Code}}}

script:

if( {{.Code}} != &quot;&quot;)
{
$(&quot;#payee&quot;).val({{.Code}}).trigger(&quot;change&quot;);
}

This will select the options matching {{.Code}}.

Thanks all for the help.

huangapple
  • 本文由 发表于 2016年5月27日 20:31:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/37483685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定