11

我有一个关于 laravel 分页和无限滚动的问题:

首先,我有这个:

<div class="row">   

<div id="boxes">

    @forelse($duels->results as $d)

        <div class="col-span-4 box notizy">

            @include('versus.versus')

        </div>



    @empty



    @endforelse

</div>

<div class="col-span-12">
    <div class="paginate text-center">
        {{$duels->links()}}
    </div>
</div>

正如我们所看到的,我有一个 div #boxes ,其中包含 divs .box 。分页由 Laravel 设置,如下所示:

<div class="col-span-12">
    <div class="paginate text-center">
        <div class="pagination">
            <ul>
                <li class="previous_page disabled"><a href="#">&laquo; Previous</a></li>
                <li class="active"><a href="#">1</a></li> <li><a href="index.php?page=2">2</a></li>
                <li class="next_page"><a href="index.php?page=2">Next &raquo;</a></li>
            </ul>
            </div>      
      </div>
</div>

所以现在,我想放一个无限滚动而不是分页。我应该如何使用https://github.com/paulirish/infinite-scroll

如果您有任何问题,我会留在这里!

PS:我尝试了一些东西,但没有一个像:

    $('#boxes').infinitescroll({
    loading: {
        finished: undefined,
        finishedMsg: "<em>Congratulations, you've reached the end of the internet.</em>",
        msg: null,
        msgText: "<em>Loading the next set of posts...</em>",
        selector: null,
        speed: 'fast',
        start: undefined
    },
    state: {
        isDuringAjax: false,
        isInvalidPage: false,
        isDestroyed: false,
        isDone: false, // For when it goes all the way through the archive.
        isPaused: false,
        currPage: 1
    },
    debug: false,
    behavior: undefined,
    binder: $(window), // used to cache the selector for the element that will be scrolling
    nextSelector: "div.paginate li.active a",
    navSelector: "div.paginate",
    contentSelector: null, // rename to pageFragment
    extraScrollPx: 0,
    itemSelector: "div.notizy",
    animate: false,
    pathParse: undefined,
    dataType: 'html',
    appendCallback: true,
    bufferPx: 40,
    errorCallback: function () { },
    infid: 0, //Instance ID
    pixelsFromNavToBottom: undefined,
    path: undefined, // Can either be an array of URL parts (e.g. ["/page/", "/"]) or a function that accepts the page number and returns a URL
    prefill: false, // When the document is smaller than the window, load data until the document is larger or links are exhausted
    maxPage:undefined // to manually control maximum page (when maxPage is undefined, maximum page limitation is not work)
});

基于github页面的示例(并替换应该替换的内容),但这样做绝对没有效果。

4

3 回答 3

23

还有一种方法可以使用另一个无限滚动插件https://github.com/pklauzinski/jscroll来实现。

假设你有一个简单的 Blade 视图:

<div class="scroll">
<ol>
    @foreach($media as $m)
        <li>{{$m->title}}</li>
    @endforeach
</ol>

{{$media->links()}}
</div>

我们可以用下面的JS代码实现无限滚动

<?=HTML::script('<YOUR PATH HERE>jquery.jscroll/jquery.jscroll.min.js');?>
<script type="text/javascript">
$(function() {
    $('.scroll').jscroll({
        autoTrigger: true,
        nextSelector: '.pagination li.active + li a', 
        contentSelector: 'div.scroll',
        callback: function() {
            $('ul.pagination:visible:first').hide();
        }
    });
});
</script>

nextSelector属性会在你的默认 Laravel 分页中选择下一页链接,contentSelector选择需要的内容,回调函数隐藏分页内容(我不得不手动隐藏它,因为它们的属性pagingSelector似乎对我无效)。您可以在插件的主页上找到模式详细信息。

于 2014-02-26T22:49:48.707 回答
18

我找到了解决方案(为你,未来的人):

$('#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);

});

这有效,因为:

  • laravel 4 给出的分页和我们之前看到的一样
  • laravel 中的分页给出了一个类似 ....?page=x 的 url

重要的

您将遇到的错误是:

当您向下滚动超出最后一页时,您可能会发现您一遍又一遍地进入最后一页,从而导致真正的无限滚动。

要解决此问题,请转到 paginator.php(在 laravel 文件夹中)并将其更改如下:

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

希望有一天它会对某人有所帮助!

于 2013-05-10T20:25:07.887 回答
2

感谢 Pretty Good Pancake 的这个解决方案,它运作良好。但是我认为在 Laravel 4 中,Response Facade 不再有 error() 方法,所以类似App::abort('404', '...')or的东西Response::make('...', 404)会起作用。use Illuminate\Support\Facades\..由于文件是命名空间的,因此请记住将其添加到文件中。

我认为更简洁的方法可能是自己扩展 Paginator 类并实现 getCurrentPage 函数。php composer.phar update这样,当您执行可能会覆盖供应商目录中的文件时,更改不会被清除。

于 2013-09-17T10:19:20.727 回答