Symfony2 - KnpPaginator - AJAX/嵌入式控制器


Symfony2 - KnpPaginator - AJAX/Embedded Controller

我在使用 Knp、AJAX 请求和过滤器时遇到了问题。我想我在这里做错了什么,但我不确定 KnpPaginator 在内部是如何工作的,我没有时间在这个项目中弄清楚。

无论如何,基本上,我的页面有一个嵌入式控制器,可以在页面上呈现一个表格。当从 twig 调用分页器时,它会将路由返回到容器页面,这会导致分页器无法处理我对该 uri 的 GET 请求。

我不确定你们中是否有人遇到过这个问题 - 我很乐意听听是否有更好的解决方案(我很确定有)。这是我的代码:

控制器

     /**
     * Just a shell page
     *
     * @Route("/postmanagement/index")
     * @Template()
     *
     * @return array
     */
    public function indexAction()
    {
        $form = $this->createForm(new FilterPostsType(), null, array(
                'action' => $this->generateUrl('myblog_admin_postmanagement_filterposts'),
                'method' => 'POST'
            )
        );
    return array(
        'form' => $form->createView()
    );
}
    /**
     * Returns active posts and comments
     *
     * @param Request $request
     *
     * @return array
     */
    public function defaultAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $posts = $em->getRepository('ModelBundle:Post')->findBy(array(
                'active' => true
            )
        );
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate($posts, $request->query->get('page', 1), 10);
        return $this->render("AdminBundle:PostManagement:_ajax-panel.html.twig", array(
                'isPost' => true,
                'posts' => $posts,
                'pagination' => $pagination
            )
        );
    }
    /**
     * @param Request $request
     *
     * @Route("/postmanagement/filter")
     *
     * @return array
     */
    public function filterPostsAction(Request $request)
    {
        $form = $this->createForm(new FilterPostType(), null, array(
                'action' => $this->generateUrl('myblog_admin_postmanagement_filterposts'),
                'method' => 'POST'
            )
        );
//        if ($request->isMethod('POST')) {
            $posts = null;
            $form->handleRequest($request);
            $data = $form->getData();
            $posts = $this->get('myblog.admin_manager')->filterPosts($data);
            switch ($data['type']) {
                case 'post':
                    $isPost = true;
                    $isComment = false;
                    break;
                case 'comment':
                    $isPost = false;
                    $isComment = true;
                    break;
            }
//        }
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate($posts, $request->query->get('page', 1), $data['maxresults']);
        if (is_null($posts)) {
            return new NotFoundHttpException();
        } else {
            return $this->render('AdminBundle:PostManagement:_ajax-panel.html.twig', array(
                    'posts' => $posts,
                    'isPost' => $isPost,
                    'isComment' => $isComment,
                    'pagination' => $pagination
                )
            );
        }
}

我不在这里发布视图,因为它是一个简单的渲染(controller(MyBundle:Controller:myAction))。如您所见,我在页面上提交了一份表格,用于过滤帖子。这也带来了一个问题,因为在我通过过滤器运行查询后,分页器似乎不会保留查询。

感谢您的任何帮助!如果有人以前做过这个,并且想出了一个比我相当复杂的解决方案更好的解决方案(这也涉及太多我喜欢的查询),我会很高兴。

我想通了。

如果其他人想使用 InfiScr 触发器 + KNPPaginatorBundle + filter (PHP) 进行分页,请使用以下 JS:

/**
 * Load more pagination handler
 */
var AjaxPagination = function (options) {
    AjaxProt.call(this, options);
    this.filter = options.filter;
    this.toJoinEl = options.toJoinEl;
    this.containerEl = options.containerEl;
    this.navContainer = options.navContainer;
    this.nextSelector = options.nextSelector;
    this.uri = options.uri;
};
AjaxPagination.prototype = Object.create(AjaxProt.prototype);
AjaxPagination.prototype.init = function () {
    var thisObj = this,
        uri = thisObj.uri;
    $(thisObj.navContainer).hide();
    $(document).on(thisObj.event, thisObj.targetEl, function (e) {
        e.preventDefault();
        thisObj.ajaxRequest(uri);
    });
};
AjaxPagination.prototype.ajaxRequest = function (uri) {
    var thisObj = this,
        page = $(this.nextSelector).attr('href').match(/'d+$/);
    $('#filter_bets_page').val(page);
    var data = $(this.filter).serialize(),
        method = this.method;
    console.log(data);
    $.ajax({
        url: uri,
        data: data,
        type: method,
        success: function (data) {
            thisObj.infiScrCallback(data);
        }
    });
};
AjaxPagination.prototype.infiScrCallback = function(data) {
    var thisObj = this;
    $(thisObj.navContainer).remove();
    if (thisObj.toJoinEl) {
        var filteredContent = $("<div>").append( $.parseHTML( data ) ).find( '.findable');
        var newPagination = $("<div>").append( $.parseHTML( data ) ).find( 'div.pagination-hidden' );
        $(thisObj.toJoinEl).append(filteredContent);
        $(thisObj.containerEl).append(newPagination);
    } else {
        $(thisObj.containerEl).append(data).fadeIn();
    }
    if (!$(thisObj.nextSelector).length) {
        $(thisObj.targetEl).fadeOut();
    }
};