symfony 3添加要删除的确认框

时间:2017-10-20 06:19:04

标签: php html symfony symfony-3.3

我有一个包含来自db的用户的列表,其中有一个工作删除按钮。 这是我的控制者:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Users;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\HttpFoundation\Session\Session;


class DefaultController extends Controller
{  
    /**
     * @Route("/homepage", name="homepage")
     */
    public function listAction(Request $request)
    {
        $users = new Users();
        $userssrc = null;
        $name = null;
        $session = $request->getSession();
        $defaultData = array('message' => 'wut');
        $formsearch = $this->createFormBuilder($defaultData)
                 ->add('search', TextType::class, array('label'=>'Suche', 'attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;'))) 
                 ->add('submit', SubmitType::class, array('label'=>'suchen','attr'=>array('class'=>'btn btn-primary')))
                 ->add('reset', SubmitType::class, array('label'=>'zurücksetzten', 'attr'=>  array('class'=>'btn btn-default')))
                 ->getForm();
         $formsearch->handleRequest($request);
         if($formsearch->isSubmitted() && $formsearch->isValid()){
            $name = $formsearch['search']->getData();
            $em=$this->getDoctrine()->getManager();
            $query = $em->createQuery(
                'SELECT u 
                 FROM AppBundle:Users u
                 WHERE u.vorname = :name
                 OR u.nachname = :name')->setParameter('name', $name);
            $userssrc = $query->getResult();

            if ($formsearch->get('reset')->isClicked()){
                return $this->redirectToRoute('homepage');
            }

         }
        else{
            $vae = 'no form submission';
            dump($vae);
        }
        //gets the table entries
        $list = $this->getDoctrine()
                ->getRepository('AppBundle:Users')
                ->findAll();
        // Returns to index.html.twig with the variable liste, in which the db entries are
        return $this->render('main/index.html.twig', array('list'=>$list, 'form'=>$formsearch->createView(), 'userssrc'=>$userssrc)); 
        //return $this->render('base.html.twig', array('session'=>$session));
    }}

这里是index.html.twig文件:

{% extends 'base.html.twig' %}

{% block title %}Personeneinträge{% endblock %}
{% block body %}
<div class='container'>
<h1>Personeneinträge</h1>
<form class='form-inline' method="post">
{{ form_start(form)}}
{{ form_widget(form)}}
{{ form_end(form)}}
</form>
<table class="table table-hover">
     <thead>
     <th>Vorname</th>
     <th>Nachname</th>
     <th>Strasse</th>
     <th>Ort</th>
     <th>PLZ</th>
         <th>Beschreibung</th>
         <th class='col-md-3'>Bild</th>
         <th>&nbsp;</th>
     </thead>
     <tbody>
    {% if userssrc is null %}
        {% for row in list %}
     <tr>
             <td>{{row.vorname}}</td>
             <td>{{row.nachname}}</td>
             <td>{{row.strasse}}</td>
             <td>{{row.ort}}</td>
             <td>{{row.plz}}</td>
             <td>{{row.beschreibung}}</td>
             {% if row.bild %}
             <td><img class='usrimg' src="{{asset(row.bild, 'uploaded_files')}}" alt="Bild zum Benutzer"></td>
             {% else %}
             <td>-</td>
             {% endif %}
             <td>
                 <a href="{{ path('details',{'id':row.id})}}" class="btn btn-success">Details</a> 
                 <a href="{{ path('edit',{'id':row.id})}}" class="btn btn-info">Bearbeiten</a> 
                 <a href="{{ path('delete',{'id':row.id})}}" class="btn btn-danger">Löschen</a>
                 <a href="{{ path('mail',{'id':row.id})}}" class="btn btn-default">Als Mail senden</a>  
             </td>
        </tr> 
        {% endfor %}
    {% else %}
        {% for row in userssrc %}
        <tr>
             <td>{{row.vorname}}</td>
             <td>{{row.nachname}}</td>
             <td>{{row.strasse}}</td>
             <td>{{row.ort}}</td>
             <td>{{row.plz}}</td>
             <td>{{row.beschreibung}}</td>
             <td>
                 <a href="{{ path('details',{'id':row.id})}}" class="btn btn-success">Details</a> 
                 <a href="{{ path('edit',{'id':row.id})}}" class="btn btn-info">Bearbeiten</a> 
                 <a href="{{ path('delete',{'id':row.id})}}" class="btn btn-danger">Löschen</a>        
             </td>
        </tr> 
        {% endfor %}
    {% endif %}
     </tbody>
</table>
</div>
{% endblock %}

我现在希望它在删除条目之前要求确认,是否有人知道一个简单的解决方案? 为了防止理解,这里是渲染视图的屏幕截图: enter image description here

对不起,忘了添加我的deleteAction:

/**
     * @Route("/loeschen/{id}", name="delete")
     */
     public function deleteAction($id){
        //start Doctrine
             $em=$this->getDoctrine()->getManager();
             $list = $em->getRepository('AppBundle:Users')->find($id);
             return $this->redirectToRoute('homepage', array('remove'));   
             $em->remove($list);
             $em->flush();

     } 

1 个答案:

答案 0 :(得分:2)

无论你喜欢什么。一些常用的方法;

  • 在删除确认时创建一个只有按钮的中间页面(确定吗?)。一些symfony的生成器使用了这个&#39; DeleteForm&#39;事物的类型。它使用路由链接到带有ID的新页面,并且在POST时它实际上会删除。

  • 您可以添加unmapped checkbox,但不会尝试将其保存在对象上。只需检查控制器中是否已检查。 (虽然这并不适合你的列表布局)

  • 还有javascript;

    • 简单confirm
    • 想要dialog/modal/popup件事。
    • 多个状态按钮(按一次更改它以通知用户你确定,然后再次实际做或其他)。

我确定还有其他选择。