0
В моем мини-проекте у меня есть список клиентов, для каждого клиента у меня есть кнопка "Update"
. Нажав на эту кнопку, я хочу выполнить функцию AJAX, чтобы загрузить форму клика.Symfony2 получает форму выбранного лица с 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 %}
Я не знаю, как сделать это в контроллере и в шаблоне.
Добавьте новый маршрут к новому действию, которое будет обрабатывать перенаправление на ваш editAction с правильным идентификатором клиента, переданным Ajax. – Veve
Прошу пояснить больше. – Aminesrine
Ваш аякс вызовет маршрут (используйте https://github.com/FriendsOfSymfony/FOSJsRoutingBundle), этот маршрут указывает на новое действие redirectToGoodCustomerFormAction (customer_id). В этом случае вы можете перенаправить на действие маршрута/контроллера, которое касается издания клиента. Посмотрите на документ для ajax и перенаправления внутри контроллера. – Veve