如何使用select2和Twig模板从选择列表中将新值设置为选定值

How to set as selected a new value from a select list with select2 and Twig templating?

本文关键字:新值 设置 列表 选择 select2 何使用 Twig      更新时间:2023-09-26

我一直在努力让它发挥作用。

我有一个下拉菜单(通过使用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>

现在,新值和已注册的值都显示为选定值。

如果其他人有更好的方法,我会把你的答案标记为已接受的一加上投票。没问题。