【问题标题】:How can i populate select box with options in octobercms如何使用 octobercms 中的选项填充选择框
【发布时间】:2019-03-10 14:26:41
【问题描述】:

我的控制器里有这个

$this['item'] = Cat::where('parent_id',0)->pluck('cat_title');

这个在视野中

 <select id="inputCat" class="form-control">
    <option selected>Choose...</option>

    {% for item in item %}
    <option value={{ item.id }}>{{ item.cat_title }}</option>
    {% endfor %}

  </select>

它在选项中指示但不显示选项。 我做错了什么?

【问题讨论】:

  • 你确定$this['item']首先包含项目吗?
  • 是的。我一直在运行类似的查询。有了上面我可以上下移动选项,只是我看不到选项

标签: php laravel twig octobercms


【解决方案1】:

我猜你是用cat_title 采摘Cat 所以返回的数组将idcat_title 作为你所指的valueoption 的键所以,可能是导致问题的原因

这样做,

$this['items'] = Cat::where('parent_id',0)->pluck('cat_title', 'id');
//       ^ plural [ better approach ]      id as well for value  ^

返回数组;

Array[
    1 => 'title'
//  ^ id   ^ your cat_title
    ....
]

这将由pluck() 返回,因此您看不到item.id 也没有item.cat_title。它的just key and value pair

所以标记请使用


<select id="inputCat" class="form-control">
    <option selected>Choose...</option>
    {% for key, item in items %}
        <option value={{ key }}>{{ item }}</option>
    {% endfor %}
</select>

如果您发现问题或它不起作用,请尝试此操作,然后请发表评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2018-06-28
    • 2013-07-16
    • 1970-01-01
    相关资源
    最近更新 更多