繁体   English   中英

Symfony2使用AJAX获得所选实体的形式

[英]Symfony2 get the form of the selected entity with AJAX

在我的迷你项目中,我有一个客户列表,对于每个客户,我都有一个"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 %}

我不知道如何在控制器和模板中执行此操作。

我这样解决了我的问题:

在控制器中,我这样做:

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%}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM