如何使用select2和Twig模板从选择列表中将新值设置为选定值
How to set as selected a new value from a select list with select2 and Twig templating?
我一直在努力让它发挥作用。
我有一个下拉菜单(通过使用HTML select标记和select2.js)
[{"type":"value1"},{"type":"value2"}]
我用Twig显示数据如下:
<select multiple="" style="width: 100%" name="type" id="types" class="select2 req_place form-control input-sm">
{% for type in types %}
<option value="{{type.type}}" {% if request.post('type')==type.type %}selected{% endif %}{% if proyecto_o.type==type.type and request.post('type') is empty %}selected{% endif %}>{{type.type}}</option>
{% endfor %}
</select>
这很好用。例如,如果表单由于另一个字段的验证失败而返回错误,则先前选择的选项将显示为已选择。
但是,如果用户按如下方式键入新值(允许):
$("#types").select2({
placeholder: "Seleccione...",
allowClear: true,
tags: true, /*This allows the user to type a new value*/
language: "es",
maximumSelectionLength: 1,
});
新值不会显示为选定值。我已经尝试了很多方法来解决这个问题,但我找不到如何解决。
例如,在我的一次尝试中,如果我添加以下代码
事实上,新值仍然显示为选定值,但是,如果我从数据库中选择一个已经存在的值,它会显示为选定的值的两倍!我尝试过检查数组中是否存在新值,但仍然不起作用。
<select multiple="" style="width: 100%" name="type" id="types" class="select2 req_place form-control input-sm">
{% for type in types %}
<option value="{{type.type}}" {% if request.post('type')==type.type %}selected{% endif %}{% if proyecto_o.type==type.type and request.post('type') is empty %}selected{% endif %}>{{type.type}}</option>
{% endfor %}
{% if request.post('type') not in types %}
<option value="{{request.post('type')}}" selected>{{request.post('type')}}</option>
{% endif %}
</select>
即使我假装检查这个新键入的值是否不存在于数组中,以将其作为下拉菜单中的选定值,但它仍然不起作用,因为如果我选择了数据库中已经存在的值,它会出现两次选中。如果我选择了一个新的,它会在选择后出现,这很好。
有什么解决办法吗?如何使数据库中已存在的值不出现两次?
已解决
我已经找到了如何使用flag使其工作。
<!-- Types-->
{% set type_flag = 0 %}
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group {% if errors.has('type') %} has-error {% elseif request.post('type')%}has-success{% endif %}">
<label for="types" class="control-label{% if proyecto_o.type %} text-primary{% endif %}">type</label>
<select multiple="" style="width: 100%" name="type" id="types" class="select2 req_place form-control input-sm">
{% for type in types %}
<option value="{{type.type}}" {% if request.post('type')==type.type %}selected{% endif %}{% if proyecto_o.type==type.type and request.post('type') is empty %}selected{% endif %}>{{type.type}}</option>
{% if request.post('type')==type.type %}{% set type_flag=1 %}{% endif %}
{% if proyecto_o.type==type.type and request.post('type') is empty %}{% set type_flag=1 %}{% endif %}
{% endfor %}
{% if type_flag==0 and request.post('type') %}
<option value="{{request.post('type')}}" selected>{{request.post('type')}}</option>
% endif %}
</select>
{% if errors.has('type') %} <p class="help-block bg-danger">{{ errors.first('type') }}</p> {% endif %}
<p class="help-block">Flag: {{ type_flag }}</p>
</div>
</div>
现在,新值和已注册的值都显示为选定值。
如果其他人有更好的方法,我会把你的答案标记为已接受的一加上投票。没问题。
相关文章:
- 如何将localStorage值设置为false
- 将输入值设置为ng模型属性[Angular]时出现问题
- Knockout Force在设置新值时通知订阅者可观察到的值
- 为nouislider设置新值已达到最大数量
- 使用jQuery为属性设置新值
- 用新行设置文本区域的值
- Grunt为任务设置选项,并使用新值运行该任务
- 如何使用jquery为数据url属性设置新值
- 当我需要将页边距底部设置为新值时,它将被覆盖
- 如何使用select2和Twig模板从选择列表中将新值设置为选定值
- 在将集合模型的旧值设置为新值后,获取它们的主干模型
- & lt; textarea>设置新值时自动复位到顶部
- 在HTML5 Canvas中随机化RGB,使用Javascript为每个fillRect设置一个新值
- 基思伍德日期选择无法重新调整设置的新值
- ember.js在为对象设置新值后应用对象's属性
- 设置一个变量并立即返回新值
- 如果firebaseRef设置为新值,有必要调用.off()吗?
- 如何更新HTML与新的输入值设置与javascript
- 找到& # 39;标签# 39;for'属性和重置'for'属性设置为新值
- 除非新值是严格的数字,否则不能设置DOM元素的值