Twig Setting 在下拉列表中选择选项 [英] Twig Setting Select Option on a dropdown

查看:38
本文介绍了Twig Setting 在下拉列表中选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从头开始编写一个 php mvc,并使用 twig 作为我的模板引擎,并且需要一些帮助来设置下拉列表中的选定选项.目前在我的模型中,我有一个 sql 查询,它可以提取所有主管列表并使用 twig for 循环将它们放入我的下拉列表中,但如果匹配,我需要了解如何选择用户的主管.

I am currently writing a php mvc from scratch and using twig as my template engine and need some assistance setting the selected option on a drop down list. Currently in my model I have an sql query that pull all list of supervisors and drops them in my drop down list using the twig for loop but I need to some how select a user’s supervisor if it matches up.

我现在很抱歉,因为我是 Twig 的新手

I apologize now as I am new to twig

查看:

<select class="form-control" id="supervisor">
      {% for supervisor in supervisor %}
      <option value="{{supervisor.fname}} {{supervisor.lname}}" >{{supervisor.fname}} {{supervisor.lname}}</option>
      {% endfor %}
</select>

试过了:

<select class="form-control" id="supervisor">
      {% for supervisor in supervisor %}
{% if {{supervisor.fname}} {{supervisor.lname}} == {{ user.supervisor }} %}
            <option value="{{supervisor.fname}} {{supervisor.lname}}" selected>{{supervisor.fname}} {{supervisor.lname}}</option>
{% else %}
    <option value="{{supervisor.fname}} {{supervisor.lname}}">{{supervisor.fname}} {{supervisor.lname}}</option>
{% endif %}
      {% endfor %}
</select>

推荐答案

也许你可以试试这样的:
在循环中用 oneSupervisor 替换 supervisor 变量名,并用 user.supervisor 测试 oneSupervisor.

May be you can try something like this:
Replace supervisor variable name by oneSupervisor in the loop and test oneSupervisor with user.supervisor.

<select class="form-control" id="supervisor">
    {% for oneSupervisor in supervisor %}
        {% set selected = '' %}
        {% if (oneSupervisor.fname ~ ' ' ~ oneSupervisor.lname) == user.supervisor %}
           {% set selected = 'selected' %}
        {% endif %}
        <option value="{{oneSupervisor.fname}} {{oneSupervisor.lname}}" {{ selected }}>{{oneSupervisor.fname}} {{oneSupervisor.lname}}</option>
    {% endfor %}
</select>

这篇关于Twig Setting 在下拉列表中选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆