2014-11-21 57 views
1

我有一個客戶的聽衆,對於每個客戶我都有一個按鈕來顯示客戶的編輯表單。 我希望當我點擊這個按鈕時,一個函數ajax顯示特定客戶點擊的編輯窗體。Symfony2獲取AJAX表單

我不知道如何才能將表單渲染到使用AJAX執行的動作(控制器)中。

在這裏,我創造我的控制器的形式的方式:

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

      $answer =$this->container->get('templating')->renderResponse('ApplicationClientBundle:Client:index.html.twig', array(
       'editForm' => $editForm->createView(), 
      )); 

      $response = new Response(json_encode(array('editForm'=> $answer))); 
      $response->headers->set('Content-type', 'application/json; charset=utf-8'); 
      return $response; 
     } 



    } 

但響應JSON是空的。

我可以在控制器和index.html.twig中做什麼來顯示錶格?

編輯:

index.html.twig,我有:

{% block body %} 
<div class="content-action"> 
    <div class="col-xs-12 col-sm-6 col-md-6"> 
     <p><span>Raison Sociale :</span><span id="raison_sociale" onclick="functionClick(this)">raison sociale</span></p> 
     <p><span>Adresse :</span><span id="adresse"> Avenue Majida Boulila imm.....</span><p> 
     <p><span>M.F. :</span><span id="matriculeFiscale"> 0000000 PES 000</span></p> 
     <p><span>C.P. :</span><span id="codePostal"> 3027</span></p> 
     <p><span>Ville :</span><span id=""> Sfax<span/></p> 
    </div> 
    <div class="col-xs-12 col-sm-6 col-md-6"> 
     <p><span>Tél. :</span> 74 400 202</p> 
     <p><span>E-mail :</span><span id="email"> [email protected]</span></p> 
     <p><span>Nom du contact :</span><span id="nomContact"> nom</span></p> 
     <p><span>Tél. Mobile :</span><span id="telephone"> 23 447454</span></p> 
     <p><input type="hidden" id="id" value="2"/> </p> 
    </div> 
    <div class="clear"></div> 
</div><!--content-action--> 
{% endblock %} 
{% block javascripts %} 
<script> 
     function functionClick(element){ 
     var id = $("#id").val(); 
      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, 



        //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) 

        { 

         element.innerHTML= {{ the_first_fiels_of_the_form }}; 
        } 

       } 

      ); 

     } 

    </script> 
{% endblock %} 

我想有the_first_fiels_of_the_form在點擊的元素。

我這樣做:the_first_fiels_of_the_form ={{form_widget(editForm.raisonSociale)}} 但沒有結果。

+0

好吧,讓我們稍微詳細一點:你可以提供模板,你在哪裏做Ajax調用(重要的位,而不是整個事情)和模板'ApplicationClientBundle:Client:index.html.twig' – Koalabaerchen 2014-11-21 10:26:31

+0

我編輯我的文章 – Aminesrine 2014-11-21 10:40:09

+0

渲染模板實際返回什麼?是否設置了$ answer? – RoToRa 2014-11-21 12:01:28

回答

1

我解決我的問題是這樣的:

在控制器方面,我做的:

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

嘗試使用:

use Symfony\Component\HttpFoundation\JsonResponse; 

及更高版本:

$response = new JsonResponse(); 
$response->setData(array('editForm'=> $answer)); 
return $response; 
+0

同樣的結果{「editForm」:{「headers」:{}}} – Aminesrine 2014-11-21 09:53:55

0

爲什麼在第一時間返回JSON,如果你只是在它呈現的模板?

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->render('ApplicationClientBundle:Client:index.html.twig', array(
       'editForm' => $editForm->createView(), 
      ));; 
     } 



    } 

這也會這樣做。它會返回一個html響應,您可以在視圖中輕鬆地添加javascript。

+0

如何獲取索引中的參數editForm。 html.twig? – Aminesrine 2014-11-21 10:20:10

+0

我不明白這個問題。第二個參數,如果$ this-> render是你提供模板的數據的數組。 – Koalabaerchen 2014-11-21 10:23:57