3

I need to reload part of my html.twig:

in controller:

$entity = $em->getRepository('PublishDemandsBundle:Demands')->find($id);

In twig:

{% for n in entity %} {{ n.Id }} {% endfor %}.

i need how to reload $entity with ajax.Can someone help me and thanks.

1 Answer 1

3

You can do this with jQuery. I think the best way to do this (I think) is to have a method in your controller that do nothing but a findAll() on your Demands repo :

public function demandsAction()
{
    $entity = $em->getRepository('PublishDemandsBundle:Demands')->findAll();

    return $this->render('PublishDemandsBundle:Demands:liste.html.twig', array(
       'entity' => $entity
        ));
}

Make sure this Action can be called by a route, let's say /ajax/demands/ Then, in your twig template, just do :

 <div id="demands">
      {{ render(controller("PublishDemandsBundle:MainController:demands")) }}
 </div>
 <a href="#" id="reload">reload</a> 

With a bit of jQuery :

 $('#reload').click(function() {
     $.get("/ajax/demands", function( data ) {
     $('#demands').html( data );
 });

I haven't tested this yet, and it might be adapted to your case, but again, I would do it this way.

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

2 Comments

i have a method in controller return entity and in twig:{% block body %} {% for n in notif %} <li> <a id="reload" href="{{ path('register_demands_showdemands', { 'id': n.IdEvent , 'notif': n.id }) }}"> <span class="blue">{{ n.content}}</span> </a> </li> {% endfor %} {% endblock body %} How can i use this to reload the list of demands without refresh page.Can you explain me more and thank you for your attention
have you tried what I gave you ? Because this is exactly what you are asking for and I don't see what you don't understant/you exactly want

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.