在我的迷你项目中,我有一个客户列表,对于每个客户,我有一个按钮"Update"
。
单击此按钮,我想执行一个函数AJAX来加载单击客户的表单。
这是我的行动editAction
:
public function editAction($id)
{
if ($this->container->get('request')->isXmlHttpRequest()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ApplicationClientBundle:Client')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Client entity.');
}
$editForm = $this->createEditForm($entity);
//What can I do to send the editForm to the template?
}
这是我的模板"index.html.twig"
:
{% block body -%}
...
{% for entity in entities %}
<tr>
<td>{{ entity.raisonSociale}} </td>
<td>{{ entity.login }}</td>
<td>{{ entity.password }}</td>
<td>{{ entity.soldeSMS }}</td>
<td>
<a class="modifier-client handler" data-id="{{ entity.id }}"><span class="glyphicon glyphicon1">Update</span></a>
<a href="#historique" class="modifier-client"><span class="glyphicon glyphicon2">Voir Historique</span></a>
<a href="#ajout" class="modifier-client"><span class="glyphicon glyphicon3">Ajout transaction</span></a>
<input type="hidden" name="id_client" id="id_client" value=""/>
</td>
</tr>
{% endfor %}
...
{% endblock %}
{% block javascripts %}
$('.handler').click(function() {
var id = $(this).data('id');
var route = '{{ path('client_edit', { 'id': "PLACEHOLDER" }) }}';
route = route.replace("PLACEHOLDER", id);
$.ajax({
//On lui indique le type d'envoie des informations
type: 'POST',
//On lui indique le chemin de la fonction
url: route, //<==> editAction($id_customer_selected)
//On lui donne la valeur du choix qu'on a fait, et id est la variable qui va contenir notre valeur, nous la retrouvons dans notre controller
//Enfin nous lui disons de remplir notre formulaire avec le resultat
success: function(response)
{
//How can I get the editForm from the response and put it in the template??
$(".client").hide();
$(".fich-client").show();
document.location="#cordonner";
}
}
)});
{% endblock %}
我不知道如何在控制器和模板中执行此操作。
答案 0 :(得分:0)
我这样解决了我的问题:
在控制器中,我这样做:
public function editAction($id)
{
if ($this->container->get('request')->isXmlHttpRequest()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ApplicationClientBundle:Client')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Client entity.');
}
$editForm = $this->createEditForm($entity);
return $this->container->get('templating')->renderResponse('ApplicationClientBundle:Client:cordonner.html.twig', array(
'editForm' => $editForm->createView()
));
}
在模板"index.html.twig"
中,我这样做:
{% block body -%}
...
{% for entity in entities %}
<tr>
<td>{{ entity.raisonSociale}} </td>
<td>{{ entity.login }}</td>
<td>{{ entity.password }}</td>
<td>{{ entity.soldeSMS }}</td>
<td>
<a class="modifier-client handler" data-id="{{ entity.id }}"><span class="glyphicon glyphicon1">Update</span></a>
<a href="#historique" class="modifier-client"><span class="glyphicon glyphicon2">Voir Historique</span></a>
<a href="#ajout" class="modifier-client"><span class="glyphicon glyphicon3">Ajout transaction</span></a>
<input type="hidden" name="id_client" id="id_client" value=""/>
</td>
</tr>
{% endfor %}
<div id="cordonner">
{% include 'ApplicationClientBundle:Client:cordonner.html.twig' %}
</div>
...
{% endblock %}
{% block javascripts %}
$('.handler').click(function() {
var id = $(this).data('id');
var route = '{{ path('client_edit', { 'id': "PLACEHOLDER" }) }}';
route = route.replace("PLACEHOLDER", id);
$.ajax({
//On lui indique le type d'envoie des informations
type: 'POST',
//On lui indique le chemin de la fonction
url: route, //<==> editAction($id_customer_selected)
//On lui donne la valeur du choix qu'on a fait, et id est la variable qui va contenir notre valeur, nous la retrouvons dans notre controller
//Enfin nous lui disons de remplir notre formulaire avec le resultat
success: function(response)
{
$('#cordonner').html(response);
$(".client").hide();
$(".fich-client").show();
document.location="#cordonner";
}
}
)});
{% endblock %}
在"cordonner.html.twig"
,我这样做:
{% if editForm is defined %}
{{ form(editForm)}}
{% end if%}