首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony2 - KnpPaginator -AJAX/嵌入式控制器

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

Stack Overflow用户
提问于 2015-03-10 01:10:19
回答 1查看 1.7K关注 0票数 0

我对Knp、AJAX请求和过滤器有问题。我想我在这里做了一些非常错误的事情,但我不知道KnpPaginator到底是如何在内部工作的,我没有时间在这个项目上弄清楚它。

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

我不知道你们中是否有人遇到过这个问题--我很高兴听到这个问题是否有更好的解决办法(我很肯定有)。这是我的代码:

控制器

代码语言:javascript
复制
     /**
     * 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
                )
            );
        }
}

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

谢谢你的帮助!如果有人以前做过这件事,并且想出了一个比我复杂的解决方案更好的解决方案,我会很高兴的(这也涉及太多的问题,我不喜欢)。

EN

回答 1

Stack Overflow用户

发布于 2015-03-10 18:45:13

我想通了。

如果其他人希望使用InfiScr触发器+ KNPPaginatorBundle +过滤器(PHP)分页,请使用以下JS:

代码语言:javascript
复制
/**
 * 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();
    }
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28954580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档