0
我有一些类别和项目给他们。我希望能够只删除尚未设置项目的类别。例如,我有书籍类别,但没有添加具体书籍,因此我可以将其删除。我有食品类,因为它有比萨饼,三明治,苹果 - 所以我不能删除它。我在想如何最好的做到这一点。会员筛选在树枝
<table class="table table table-striped spacer">
{% for c in cat %}
<tr>
<td>
{# If in edit mode, display edit form #}
{% if id == c.id %}
<form class="form-inline" action="{{ path('edit_category', {'id': c.id}) }}" method="post" {{ form_enctype(form) }}>
{% form_theme form 'EMBudgetTrackerBundle:Forms:inputs.html.twig' %}
{% for field in form %}
{{ form_row(field) }}
{% endfor %}
<button type="submit" class="btn"><i class="icon-ok"></i></button>
</form>
{# Else display the name #}
{% else %}
{{ c.name }}
<a href="{{ path('edit_category', {'id': c.id}) }}"><i class="icon-pencil"> </i></a>
<a href="{{ path('delete_category', {'id': c.id}) }}"> <i class="icon-remove"></i></a>
{% endif %}
</td>
</tr>
{% endfor %}
</table>
这是我为每个类别所做的。我想要的是如果有添加项目,不提供删除链接。我能想到的唯一的事情是有两个数组 - 一个是带有项目的类别,另一个是没有项目的类别,还有两个数组是用于循环的。但是这会导致代码重复 - 一切都会一样,只有一个循环不会有<a href="{{ path('delete_category', {'id': c.id}) }}"> <i class="icon-remove"></i></a>
这一行,它看起来不是很优雅。我在想像
if(c.name not member of array_with_categories_without_items)
then display the delete link
但我不知道是否有办法做到这一点。有人能给我一些想法吗?非常感谢您提前! :)
非常感谢您的想法! :) – Faery