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?

2 Answers 2

3

This is working so far:

Embed the controller within a div:

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

And reload the controller using javascript at the end of the ajax call

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

Does anyone have a better suggestion?

Sign up to request clarification or add additional context in comments.

Comments

2

When you call the ajax controller, this controller could render the showUsersList template and return the generated html as a json object:

$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

Then in your javascript use this generated html to replace the content of the controller part of the page (use a div or similar to identify it)

<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']);
        }
    });  
}

1 Comment

Thanks @Carlos. That looks good. There is an error in render() as render can only render a template within a controller:$answer['html'] = $this->render("AcmeUserBundle:User:showUsersList.html.twig"); This is not working yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.