Laravel and Infinite Scroll

前端 未结 3 1719
小鲜肉
小鲜肉 2020-12-14 04:48

I have a question about laravel pagination and infinite scroll :

First of all, I have this :

3条回答
  •  抹茶落季
    2020-12-14 05:03

    I've found the solution (for you, people of the future) :

    $('#boxes').infinitescroll({
        navSelector     : ".paginate",
        nextSelector    : ".paginate a:last",
        itemSelector    : ".box",
        debug           : false,
        dataType        : 'html',
        path: function(index) {
            return "?page=" + index;
        }
    }, function(newElements, data, url){
    
        var $newElems = $( newElements );
        $('#boxes').masonry( 'appended', $newElems, true);
    
    });
    

    This works because :

    • The pagination given by laravel 4 is like we saw before
    • The pagination in laravel give an url like ....?page=x

    IMPORTANT

    The bug you will encounter is :

    When you scroll down beyond what should be the last page, you’ll probably find that you keep getting the last page over and and over again, causing genuinely infinite scrolling.

    to fix this, go to the paginator.php (in the laravel folder) and change it as follow :

    if (is_numeric($page) and $page > $last = ceil($total / $per_page))
        {
            return Response::error('404');
        }
    

    Hope it will help someone someday !

提交回复
热议问题