Javascript Symfony2嵌入式表单删除按钮功能

Javascript Symfony2嵌入式表单删除按钮功能,javascript,php,jquery,forms,symfony,Javascript,Php,Jquery,Forms,Symfony,我在Symfony2中创建了一个嵌入式表单,在一个“部门”中有许多“员工”。现在,在“部门”表单中添加了许多名“Firstname”和“Lastname”的员工,效果很好(为此我使用了jQuery)。我想要表单上的“删除”按钮功能,我无法理解。有人能帮我使用删除按钮功能吗 DepartmentType.php <?php namespace InstituteEvents\FormBundle\Form; use Symfony\Component\Form\AbstractType;

我在Symfony2中创建了一个嵌入式表单,在一个“部门”中有许多“员工”。现在,在“部门”表单中添加了许多名“Firstname”和“Lastname”的员工,效果很好(为此我使用了jQuery)。我想要表单上的“删除”按钮功能,我无法理解。有人能帮我使用删除按钮功能吗

DepartmentType.php

<?php

namespace InstituteEvents\FormBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class DepartmentType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
 public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name')
        ->add('employees','collection', array(
            'type' => new EmployeeType(),
            'prototype' => true,
            'allow_add' => true,
            'by_reference' =>false
         ))    
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'InstituteEvents\FormBundle\Entity\Department'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'department';
}
}

按照找到的说明进行操作:

DepartmentType
表单类中,将
EmployeeType
集合的
allow\u delete
属性设置为
true

    $builder
        ->add('name')
        ->add('employees','collection', array(
            'type' => new EmployeeType(),
            'prototype' => true,
            'allow_add' => true,
            'allow_delete' => true,//add this
            'by_reference' =>false
         )
    );
接下来,添加delete按钮(通过
EmployeeType
表单中的生成器(
->add('delete','button')
),或手动添加(在tempalte中使用JS)。然后将事件侦听器附加到表单。假设您已添加delete按钮,如下所示:

//EmployeeType:
$builder->add('delete', 'button', ['attr' => ['class' => 'delete-employee']]);
如果要在视图中使用JS添加按钮,那么下面的代码应该可以做到:

$('#department-form-selector').children('employee-form-selector').each(function(i)
{
    $(this).append('<button name="delete' + i + '" class="delete-employee">Delete</button>');
});
现在,要处理表单(假设实体设置正确):

请注意,此代码未经测试。这是最坏情况场景删除的基本流程(双向、一对多关系,反向,但未设置ondelete级联限制)。

使用此代码作为参考,您应该能够计算出它,尽管您应该使用文档:因为您所需要的全部都在文档中。我无法从食谱转换为上面index.html.twig中的jQuery代码,用于“删除员工”按钮及其代码。请帮助嘿,Udan,您可以帮助我使用“删除员工”的jQuery代码吗按钮?因为我不知道jQueryYou没有在您的
部门类型
表单上设置
允许删除
属性。如何处理删除被广泛介绍(PHP+jQ代码)谢谢您Elias Van Ootegem谢谢您的代码,但我想在“twig”(index.html.twig)中添加“删除员工”按钮,就像我在“添加”中所做的那样按钮,而不是在“EmployeeType.ie
    类似地,我希望有“Delete employees”按钮,并在“index.html.twig”中有删除功能?你能在“index.html.twig”中作为一个整体提供代码吗?作为“添加”按钮,我在“DepartmentType”或“EmployeeType”中没有提供,但我在twig中使用了“添加”按钮的代码HTML@sachintendulkar:我添加了两种方法来为每个员工添加删除按钮(包含
    $(this.append()
    )的位).deleteAll按钮与单个delete按钮基本相同,只需对所有员工表单元素调用
    remove()
    ,然后提交该表单,并以相同的方式处理它。只需添加一个button元素+事件处理程序,查询DOM中的所有员工表单,并在其上循环(
    $('emplyee-form-selector')。每个(函数(){$(this.remove()})
    ),并提交form@sachintendulkar:除此之外:不,我不会向你提供所有的代码,这不是我的工作。如果你遇到特定的问题,我愿意帮助你,但我不会免费为你做工作……对不起,如果你能为我提供“删除员工”按钮及其在index.html.twig中的功能,就像我在“添加”按钮及其在“index.html.twig”中的代码一样,请
    //EmployeeType:
    $builder->add('delete', 'button', ['attr' => ['class' => 'delete-employee']]);
    
    $('#department-form-selector').children('employee-form-selector').each(function(i)
    {
        $(this).append('<button name="delete' + i + '" class="delete-employee">Delete</button>');
    });
    
    $('#department-form-selector').on('click', '.delete-employee', function()
    {
        $(this).closest('form').delete();//remove element
        //optionally submit department form via AJAX call to persist the delete
        return false;//stop event
    });
    
    //in the controller that handles the form:
    if ($form->isValid())
    {
        //1 => we need to query for the data in the DB, so we know what to delete
        $current = $service->getCurrentDepartmentWithEmployees();
        //get the current employees, that need to be updated
        $oldEmployees = new ArrayCollection();
        foreach ($current->getEmployees() as $employee)
            $oldEmployees->add($employee);
        //2 => get the form data
        $department = $form->getData();
        //3 => check if one or more eployees were deleted
        foreach ($oldEmployees as $employee)
        {
            if (!$department->getEmployees()->contains($employee))
            {//employee was removed, update entity/entities
                $current->getEmployees()->removeElement($employee);
                //depending on the relations you've specified:
                $employee->setDepartment(null);
                $em->persist($employee);
                $em->persist($current);
            }
        }
        $em->flush();
    }