1

I have embed a controller in a template:

{% render "AcmeUserBundle:User:showUsersList"} %}

<a onClick="changeStatus({{user.getUserId()}})"

The aim is simple:

  • The user clicks on the link which updates the status via ajax (this works fine)
  • Reload the embedded controller only, not the entire page!

At the moment, I managed to do this, by reloading the entire page using document.location.reload(true); This has no point so far...

Here is the ajax part:

//...
function changeStatus(userId){
    $.ajax({
        type: "POST",
        url: ajaxControllerPath,
        data: "userId="+userId,
        success: function(){
            document.location.reload(true); 
        }
    });  
}

How would you reload the embedded controller only?

4

2 回答 2

3

到目前为止,这是有效的:

将控制器嵌入到 div 中:

<div id="block1">
    <a onClick="changeStatus({{user.getUserId()}})"
</div>

并在 ajax 调用结束时使用 javascript 重新加载控制器

$('#block1').load("{{ path('show_users_list') }}");

有人有更好的建议吗?

于 2012-08-18T07:07:35.210 回答
2

当您调用 ajax 控制器时,此控制器可以呈现 showUsersList 模板并将生成的 html 作为 json 对象返回:

$answer['html'] = $this->forward('AcmeUserBundle:User:showUsersList')->getContent(); 
$response = new Response();                                                
$response->headers->set('Content-type', 'application/json; charset=utf-8');
$response->setContent(json_encode($answer));
return $response

然后在你的 javascript 中使用这个生成的 html 来替换页面的控制器部分的内容(使用 div 或类似的来识别它)

<div id="changethis">{% render "AcmeUserBundle:User:showUsersList"} %}</div>

function changeStatus(userId){
    $.ajax({
        type: "POST",
        url: ajaxControllerPath,
        data: "userId="+userId,
        success: function(returnData){
            $('#changethis').html(returnData['html']);
        }
    });  
}
于 2012-08-18T07:59:07.170 回答