Symfony2带有复选框的扩展表窗体


Symfony2 Expaned table with checkboxes form

我有一个表,其中包含例如"ID"、"Category"、"Name"answers"Action"等信息。现在,我想改进带有复选框的表,使用户可以选择多行并在一步中删除它们。

我的问题:我用表单生成器创建了一个表单

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', 'entity', array(
                'required'  => false,
                'class'    => 'AppBundle:Task',
                'property' => 'id',
                'multiple' => true,
                'expanded' => true
            ))
            ->add('add', 'submit', array('label' => 'Add'));
    }

此表单与包含该表的数据的表一起发送到twitch html。

{% if table|length > 0 %}
        {{ form_start(addForm) }}
        {{ form_errors(addForm) }}
        <table class='result' border=1>
            <th></th>
            <th>ID</th>
            <th>Category</th>
            <th>Name</th>
            <th>Action</th>
            {% for item in table %}
                <tr>
                    <td>
                    {% for entity in addForm.id %}
                        {% if entity.vars.value == item.id %}
                            {{ form_widget(entity.vars.form) }}
                        {% endif %}
                    {% endfor %}
                    </td>
                    <td>{{ item.id }}</td>
                    <td>{{ item.category }}</td>
                    <td>{{ item.name }}</td>
                    <td>{{ item.action }}</td>
                </tr>
            {% endfor %}
        </table>

正如您所看到的,我的解决方案必须在for循环中搜索表单项的正确ID并放置它们。

我现在的问题是:我对这个解决方案感到不舒服——有没有一种更简单的方法来显示包含多个列和复选框的表?

提前感谢

编辑:

还有一个问题,我在选择一些复选框后得到以下错误:

Neither the property "id" nor one of the methods "addId()"/"removeId()", "setId()", "id()", "__set()" or "__call()" exist and have public access in class "AppBundle'Entity'XXXXXX".

也许您应该更好地使用集合表单类型,而不是实体类型,因为您将能够为数据库表的每个记录使用自定义表单和分支模板。在本说明中,您将学习如何删除或添加一行。添加复选框以提供批处理操作将是一个额外的javascript解决方案