【问题标题】:Symfony2 Expaned table with checkboxes formSymfony2 带有复选框形式的扩展表
【发布时间】:2023-03-18 13:46:02
【问题描述】:

我有一个表格,其中包含“ID”、“类别”、“名称”和“操作”等信息。现在我想用复选框改进表格,使用户能够选择多行并一步删除它们。

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

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'));
    }

此表格与包含表格数据的表格一起发送到 twig-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".

【问题讨论】:

    标签: php forms symfony checkbox twig


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-08-12
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      相关资源
      最近更新 更多