Javascript 使用AJAX更改细枝模板变量
我正在尝试用使用AJAX获得的新值重新加载html的一部分 有一个Javascript 使用AJAX更改细枝模板变量,javascript,ajax,symfony,twig,Javascript,Ajax,Symfony,Twig,我正在尝试用使用AJAX获得的新值重新加载html的一部分 有一个 {% for client in clients %} 循环,我有了一组使用AJAX的新客户端: $search = $request->request->get('data'); $clients=$this->getDoctrine()->getRepository( Client::class)->findBy(array('name'=&
{% for client in clients %}
循环,我有了一组使用AJAX的新客户端:
$search = $request->request->get('data');
$clients=$this->getDoctrine()->getRepository(
Client::class)->findBy(array('name'=>$search));
$response = new JsonResponse();
$response->setStatusCode(200);
return $response->setData(['search' => $clients ]);
我正在尝试使用新检索的数据更改客户端
有办法吗?还是我应该尝试另一种技术
提前谢谢 无法执行此操作,因为细枝是在服务器端渲染的。 您需要更新
{%forclientsinclients%}
使用javascript时,无法通过AJAX更改
客户端的值,因为此模板已经呈现。但是,您可以创建一个单独的细枝模板,如下所示:
{# loop.html.twig #}
{% for client in clients %}
.. your code
{% endfor %}
<div id="client-loop-container">
{% include 'loop.html.twig' %}
</div>
$.ajax({
type: "POST",
success: function(response) {
response = JSON.parse(response);
$("div#client-loop-container").html(response.template);
}
});
然后将其包含在模板中,如下所示:
{# loop.html.twig #}
{% for client in clients %}
.. your code
{% endfor %}
<div id="client-loop-container">
{% include 'loop.html.twig' %}
</div>
$.ajax({
type: "POST",
success: function(response) {
response = JSON.parse(response);
$("div#client-loop-container").html(response.template);
}
});
最后,在ajax中,您应该有如下内容:
{# loop.html.twig #}
{% for client in clients %}
.. your code
{% endfor %}
<div id="client-loop-container">
{% include 'loop.html.twig' %}
</div>
$.ajax({
type: "POST",
success: function(response) {
response = JSON.parse(response);
$("div#client-loop-container").html(response.template);
}
});
在控制器中
$clients=$this->getDoctrine()->getRepository(
Client::class)->findBy(array('name'=>$search));
return $this->render('yourTemplate.html.twig')->getContent();
在你的小枝上
$.ajax({
type: "POST",
success: function(data) {
$("div#client-loop-container").html(data);
}
});