【问题标题】:3 level optgroup with SymfonySymfony 的 3 级 optgroup
【发布时间】:2017-09-27 13:58:52
【问题描述】:

我正在使用 Symfony,我想做一个 3 Levels optgroup 我想要的是:
第 1 类(已禁用)
--ChildCategory 1.1
--ChildCategory 1.2(已禁用)
----ChildChildCategory 1.2.1
第 2 类

但我得到的只是:
第 1 类(已禁用)
--ChildCategory 1.1
ChildCategory 1.2(已禁用)
--ChildChildCategory 1.2.1
第 2 类

我的 HTML 代码:

<select id="event_category" name="event[category]" class="form-control">
<optgroup label="Category 1">
    <option value="8">Category 1.1</option>
</optgroup>
<optgroup label="Category 1.2">
    <option value="7">Category 1.2.1</option>
</optgroup>
    <option value="3">Category 2</option>
</select>

我正在使用此代码创建一个数组,以将其添加到我的 formType 中:

$arrayCategories = array();
        foreach ($categories as $category)
        {
            if (count($category->getChildren()) > 0)
            {
                $arrayCategories[$category->getTitle()] = array();

                foreach ($category->getChildren() as $child)
                {
                    if (count($child->getChildren()) > 0)
                    {
                        $arrayCategories[$category->getTitle()][$child->getTitle()] = array();

                        foreach ($child->getChildren() as $child2)
                        {
                            $arrayCategories[$category->getTitle()][$child->getTitle()][$child2->getTitle().' - '. $child2->getLangLabel()] = $child2->getId();
                        }
                    }
                    else
                    {
                        $arrayCategories[$category->getTitle()][$child->getTitle().' - '. $child->getLangLabel()] = $child->getId();
                    }
                }
            }
            else
            {
                $arrayCategories[$category->getTitle().' - '. $category->getLangLabel()] = $category->getId();
            }
        }

感谢您的帮助!

【问题讨论】:

    标签: php forms symfony optgroup


    【解决方案1】:

    你不能,一个 optgroup 只能有“select”作为父级。 看看这个,这个 HTML 应该可以工作,但它没有:

    <select>
        <optgroup label="Level One">
            <option> A.1 </option>
            <optgroup label="Level Two">
                <option>A.B.1</option>
            </optgroup>
            <option> A.2 </option>
        </optgroup>
        <option> A </option>
    </select>

    【讨论】:

    • 你是对的,那么解决方案是什么?因为我将这个数组提供给我的 formType,而我不知道如何通过其他方式做到这一点:)
    • 使用 2 个单独的选择/选项?
    • 不要犹豫,为我的答案投票或选择它是正确的 ;)
    • 一样!如果您喜欢,请为我的问题和答案投票:)
    【解决方案2】:

    我找到了解决办法! findAll 类别并将它们呈现到我的树枝页面,并显示它们:

    <select id="event_category" class="form-control">
         {% for category in categories %}
              {% if category.children|length == 0 %}
    
                  <option value="{{ category.id }}">{{ category.title }} - {{ category.getLangLabel() }}</option>
              {% else %}
                  <option value="{{ category.id }}" disabled>{{ category.title }} - {{ category.getLangLabel() }}</option>
                   {% for child in category.children %}
    
                       {% if child.children|length == 0 %}
                         <option value="{{ child.id }}">&nbsp;&nbsp;&nbsp;&nbsp;{{ child.title }} - {{ child.getLangLabel() }}</option>
                                            {% else %}
                                                <option value="{{ child.id }}" disabled>&nbsp;&nbsp;&nbsp;&nbsp;{{ child.title }} - {{ child.getLangLabel() }}</option>
                                                {% for secondChild in child.children %}
                                                    <option value="{{ secondChild.id }}">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ secondChild.title }} - {{ secondChild.getLangLabel() }}</option>
                                                {% endfor %}
                                            {% endif %}
                                        {% endfor %}
                                    {% endif %}
                                {% endfor %}
    

    感谢您的帮助! :)

    【讨论】:

      猜你喜欢
      • 2017-03-22
      • 2017-07-26
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多