Javascript Symfony2使用AJAX获取表单
我有一个客户列表,对于每个客户,我都有一个按钮来显示客户的编辑表单。 我想当我点击这个按钮时,一个函数ajax会显示所点击的特定客户的编辑表单 我不知道如何将表单呈现到使用AJAX执行的操作(控制器)中 以下是我在控制器中创建表单的方法:Javascript Symfony2使用AJAX获取表单,javascript,ajax,symfony,render,Javascript,Ajax,Symfony,Render,我有一个客户列表,对于每个客户,我都有一个按钮来显示客户的编辑表单。 我想当我点击这个按钮时,一个函数ajax会显示所点击的特定客户的编辑表单 我不知道如何将表单呈现到使用AJAX执行的操作(控制器)中 以下是我在控制器中创建表单的方法: public function editAction($id) { if ($this->container->get('request')->isXmlHttpRequest()) { $em
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"> info@ggg.com.tn</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 %}
{%block body%}
莱森社会:莱森社会
地址:Majida Boulila imm大道……
货币:0000000 PES 000
C.p.:3027
维尔:斯法克斯
泰尔:74400202
电邮:info@ggg.com.tn
姓名:姓名
泰尔。移动电话:23447454
{%endblock%}
{%block javascripts%}
函数函数单击(元素){
var id=$(“#id”).val();
var route={{path('client_edit',{'id':“PLACEHOLDER”}}});
路由=路由。替换(“占位符”,id);
$.ajax({
//论信息环境类型
键入:“POST”,
//关于法国化学工业
url:route,
//在一个事实上,我们是一个变数,它的内容是不可改变的,我们是不可改变的
//我们不需要重新计算结果
成功:功能(响应)
{
element.innerHTML={{表单的第一个字段};
}
}
);
}
{%endblock%}
我希望在元素中单击表单的第一个字段
我做:表单的第一部分={{form\u小部件(editForm.raissonsociale)}}
但没有结果。尝试使用:
use Symfony\Component\HttpFoundation\JsonResponse;
后来:
$response = new JsonResponse();
$response->setData(array('editForm'=> $answer));
return $response;
如果只是有一个呈现的模板,为什么首先要返回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中。我这样解决了我的问题:
在控制器中,我执行以下操作:
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 %}
{% if editForm is defined %}
{{ form(editForm)}}
{% end if%}
我不明白这个问题。第二个参数if$this->render是一个为模板提供的数据数组。好的,让我们来详细介绍一下:您可以在进行ajax调用的地方提供模板(重要的部分,而不是全部)和模板ApplicationClientBundle:Client:index.html.twig
渲染模板是否确实返回了一些内容?$answer
设置了吗?json响应为空:{“editForm”:{“headers”:{}