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

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

报告相同问题?