问题描述
限时送ChatGPT账号..我目前正在从头开始编写一个 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.
我现在很抱歉,因为我是新来的树枝
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>
这篇关于树枝设置在下拉菜单中选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论