symfony 3添加确认框删除
问题描述:
我有一个用户从一个数据库,其中有一个工作删除按钮的列表。 这里是我的控制器: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> </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 %}
我现在希望它要求它删除该条目之前的确认,没有人知道一个简单的解决方案? 以防万一它有助于理解,这里的渲染视图的屏幕截图:
对不起,忘了加我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();
}
答
不管你喜欢。一些常用的方法;
创建一个中间页面只需一个按钮删除时确认(你确定吗?)。一些symfony的生成器使用这种'DeleteForm'类型的东西。它使用路由链接到带有ID的新页面,并在POST上实际删除。
您可以添加一个unmappedcheckbox,它不会尝试将其保存在对象上。只要检查它是否在控制器中检查。 (虽然这并不真正适合你的列表布局)
-
并且有javascript;
- 简单confirm
- 花式dialog/modal/popup一样的东西。
- 多个状态按钮(按下一次可以改变它以通知用户,你确定,然后再次实际执行或不管)。
和IM肯定还有其他的选择了。
+0
这些都是很好的方法,谢谢,但没有人举例或类似的东西?有问题在我的项目中整合js并让它返回var ^^“ – origins523
你可以使用[modal](https://getbootstrap.com/docs/3.3/javascript/#modals)与javascript –