使用 Symfony 的 3 级 optgroup
3 level optgroup with Symfony
我正在使用 Symfony,我想做一个 3 级 optgroup,我想要的是:
类别 1(已禁用)
--儿童类别 1.1
--ChildCategory 1.2(禁用)
----ChildChildCategory 1.2.1
类别 2
但我得到的只是:
类别 1(已禁用)
--儿童类别 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();
}
}
感谢您的帮助!
你不能,一个 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>
我找到了解决办法!找到所有类别并将它们呈现到我的树枝页面,并显示它们:
<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 }}"> {{ child.title }} - {{ child.getLangLabel() }}</option>
{% else %}
<option value="{{ child.id }}" disabled> {{ child.title }} - {{ child.getLangLabel() }}</option>
{% for secondChild in child.children %}
<option value="{{ secondChild.id }}"> {{ secondChild.title }} - {{ secondChild.getLangLabel() }}</option>
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
感谢帮助! :)
我正在使用 Symfony,我想做一个 3 级 optgroup,我想要的是:
类别 1(已禁用)
--儿童类别 1.1
--ChildCategory 1.2(禁用)
----ChildChildCategory 1.2.1
类别 2
但我得到的只是:
类别 1(已禁用)
--儿童类别 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();
}
}
感谢您的帮助!
你不能,一个 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>
我找到了解决办法!找到所有类别并将它们呈现到我的树枝页面,并显示它们:
<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 }}"> {{ child.title }} - {{ child.getLangLabel() }}</option>
{% else %}
<option value="{{ child.id }}" disabled> {{ child.title }} - {{ child.getLangLabel() }}</option>
{% for secondChild in child.children %}
<option value="{{ secondChild.id }}"> {{ secondChild.title }} - {{ secondChild.getLangLabel() }}</option>
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
感谢帮助! :)