duannao3402 2015-11-11 07:07
浏览 15
已采纳

Symfony2带有复选框形式的Expaned表

I have a table which contains information for example "ID", "Category", "Name" and "Action". Now I want to improve the table with checkboxes which enables the user to choose multiple rows and to delete them in one step.

My problem: I have created a form with the form-builder:

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

This form is send to the twig-html with the table which contains the data for the table.

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

As you can see my solution has to search in a for loop for the correct ID of the form-item and place them.

My problem now: I do not feel comfortable with this solution - is there a more easier way to display a table containing several columns and checkboxes?

Thanks in advance

EDIT:

Also have the problem that I get the following error after selecting some checkboxes:

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".
  • 写回答

1条回答 默认 最新

  • dongmi1995 2015-11-11 10:55
    关注

    Maybe you should better use the collection form type rather then the entity type because you will be able to use a custom form and twig template for each record of the database table. In this explanation you will learn how to remove or add one row. Adding checkboxes to provide batch actions will then be an additional javascript solution

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费