简体   繁体   English

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

[英]Symfony2 - KnpPaginator - AJAX/Embedded Controller

I'm having an issue with Knp, an AJAX request, and a filter. 我在Knp,AJAX请求和过滤器方面遇到问题。 I think I'm doing something very wrong here, but I am not sure how exactly KnpPaginator works internally, and I don't have the time to figure it out on this project. 我想我在这里做错了什么,但我不确定KnpPaginator在内部的工作方式如何,而且我没有时间在这个项目上弄清楚。

Anyway, basically, my page has an embedded controller which renders a table on the page. 无论如何,基本上,我的页面具有嵌入式控制器,可在页面上呈现表格。 When paginator is called from twig, it returns the route to the container page, which results in paginator failing to work with my GET requests to that uri. 从树枝中调用paginator时,它会将路由返回到容器页面,这导致paginator无法处理我对该uri的GET请求。

I'm not sure if any of you have come across this - I'm happy to listen if there is a better solution to the problem (I'm quite sure there is). 我不确定是否有人遇到过-我很高兴听听是否有更好的解决方案(我很确定)。 Here is my code: 这是我的代码:

CONTROLLER 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
                )
            );
        }
}

I'm not posting the view here, since it is a simple render(controller(MyBundle:Controller:myAction)). 我不在这里发布视图,因为它是一个简单的render(controller(MyBundle:Controller:myAction))。 As you can see, there is a form I'm submitting on the page, to filter the posts. 如您所见,我正在页面上提交表单,以过滤帖子。 That also poses a problem, since it seems paginator doesn't keep the query after I've run it through the filter. 这也带来了一个问题,因为在我通过过滤器运行它之后,分页器似乎没有保留查询。

Thanks for any help! 谢谢你的帮助! I would love if someone has done this before and has come up with a better solution than my rather convoluted one (which also involves too many queries for my liking). 如果有人以前曾经这样做过,并且提出了比我比较费解的解决方案更好的解决方案,那么我会很乐意的。

I figured it out. 我想到了。

If anyone else would like to paginate with InfiScr trigger + KNPPaginatorBundle + filter (PHP), use this JS: 如果其他人想使用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();
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM