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

时间:2015-03-10 01:10:19

标签: php jquery ajax symfony pagination

我遇到了Knp,AJAX请求和过滤器的问题。我认为我在这里做了一些非常错误的事情,但我不确定KnpPaginator究竟是如何在内部工作的,而且我没有时间在这个项目中弄明白。

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

我不确定你们中是否有人遇到过这个问题 - 如果有更好的解决方案,我很高兴听到(我非常肯定)。这是我的代码:

CONTROLLER

     /**
     * 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
                )
            );
        }
}

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

感谢您的帮助!我很乐意,如果有人之前做过这件事并提出了一个比我相当复杂的解决方案更好的解决方案(这也涉及到我喜欢的太多问题)。

1 个答案:

答案 0 :(得分:1)

我明白了。

如果有人想用InfiScr触发器+ KNPPaginatorBundle +过滤器(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();
    }
};