symfony2设置刷新按钮

时间:2014-10-02 14:35:44

标签: symfony doctrine-orm

我可以在twig模板中实现一个刷新按钮,它会在点击时重新加载我的实体数据吗?

这是我的控制器:

public function indexAction() {       

    $repositoryForHistory = $this->getDoctrine()->getManager()->getRepository("PagesBundle:histories");

    $histories = $repositoryForHistory->findBy(Array(), Array('date' => 'DESC'));

    return $this->render('PagesBundle:Default:index.html.twig', array(                    
                'histories' => $histories));
}

}

我想重新加载div:

  <tbody>
          {% for history in histories %}
               <tr>
                  <td class="center" style="color: #ADADAD;">                 

                      <i class="fa fa-calendar-o"></i>

                   </td>
                   <td>
                      <span class="bold-text text-small">{{history.user}} {{history.action}} {{history.featureName}}</span>
                     </td>
                     <td class="center" style="color: #ADADAD;">{{history.date|date('d-m-Y H:i')}}</td>

             </tr>
                {% endfor %} </tbody>



<a class="panel-refresh" href="#">  <i class="fa fa-refresh"></i> <span>Refresh</span></a>

3 个答案:

答案 0 :(得分:0)

您可以通过单击“刷新”按钮发出ajax请求并发送它,并且成功时只需替换要更新的元素的html(实际上模板中没有div,我不明白你究竟是什么意思是div)。

因此,为此你必须创建一个特殊的控制器动作,它将返回一个没有布局的糟糕的HTML。

这是最简单的方法。

你也可以看看angularjs。但从某种意义上说,它可能会有点复杂。

答案 1 :(得分:0)

您需要使用AJAX做一些事情,以便当有人点击“刷新”按钮时它向Symfony发出XMLHttpRequest,它返回的响应仅包含<tr> s(包装表,或<head>等)。

在jQuery中(你没有说你正在使用它,但JavaScript写起来更复杂):

$('.panel-refresh').click( function(event, ui) {

    // prevents the browser following the click
    event.preventDefault(); 

    // replaces the contents of `<tbody>` with the response from the controller
    $('tbody').load('/path/to/action');

}

并将'/path/to/action'替换为操作的实际路径。

答案 2 :(得分:0)

谢谢你们@Alex和@Blowski这里是解决方案:

在我的控制器中

   function reloadHistoryAction() {
    $repositoryForHistory = $this->getDoctrine()->getManager()->getRepository("PagesBundle:histories");

    $histories = $repositoryForHistory->findBy(Array(), Array('date' => 'DESC'));
    $html="";
    foreach ($histories as $history) {
        $html .="<tr>
                   <td class='center' style='color: #ADADAD;'>                            
                     <i class='fa fa-tasks'></i>                        
                    </td>
                   <td>
                     <span class='bold-text text-small'>".$history->getUser()." ".$history->getAction()." ".
                $history->getFeatureName()."
                      </span>
                   </td>
                   <td class='center' style='color: #ADADAD;'>
                  ". $history->getDate()->format('d-m-Y  H:i')."
                   </td>

             </tr> ";

    }
    return new Response($html);
}

在我的twig模板中,ajax调用了控制器动作:

  $('.panel-refresh').click(function (e) {
        e.preventDefault();
        $.ajax({
            url:'{{path('history/reload')}}',
            type: "POST",
            cache: "false",
            dataType: "html",
            success: function (data) {
                $("tbody").html(data);
            }
        });
    });

非常感谢!