2017-10-20 143 views
1

我有一個用戶從一個數據庫,其中有一個工作刪除按鈕的列表。 這裏是我的控制器:symfony 3添加確認框刪除

<?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(); 

    } 
+0

你可以使用[modal](https://getbootstrap.com/docs/3.3/javascript/#modals)與javascript –

回答

2

不管你喜歡。一些常用的方法;

  • 創建一箇中間頁面只需一個按鈕刪除時確認(你確定嗎?)。一些symfony的生成器使用這種'DeleteForm'類型的東西。它使用路由鏈接到帶有ID的新頁面,並在POST上實際刪除。

  • 您可以添加一個unmappedcheckbox,它不會嘗試將其保存在對象上。只要檢查它是否在控制器中檢查。 (雖然這並不真正適合你的列表佈局)

  • 並且有javascript;

    • 簡單confirm
    • 花式dialog/modal/popup一樣的東西。
    • 多個狀態按鈕(按下一次可以改變它以通知用戶,你確定,然後再次實際執行或不管)。

和IM肯定還有其他的選擇了。

+0

這些都是很好的方法,謝謝,但沒有人舉例或類似的東西?有問題在我的項目中整合js並讓它返回var ^^「 – origins523