使用Symfony的3级optgroup

时间:2017-09-27 13:58:53

标签: php forms symfony optgroup

我正在使用Symfony,我想做一个3级别的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();
            }
        }

谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

您不能,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>

答案 1 :(得分:0)

我找到了解决方案!找到所有类别并将它们渲染到我的树枝页面,并显示它们:

<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 %}

感谢您的帮助! :)