Laravel和Infinite Scroll

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

首先,我有这个:

@forelse($duels->results as $d)
@include('versus.versus')
@empty @endforelse
{{$duels->links()}}

我们可以看到,我有一个div #boxes,其中包含div .box。 分页由Laravel设置,看起来像这样:

  

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

如果你有疑问,我留在这儿!

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

  $('#boxes').infinitescroll({ loading: { finished: undefined, finishedMsg: "Congratulations, you've reached the end of the internet.", msg: null, msgText: "Loading the next set of posts...", 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 (eg ["/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页面的示例(并替换应该替换的内容),但这样做绝对没有效果。

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

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

 
    @foreach($media as $m)
  1. {{$m->title}}
  2. @endforeach
{{$media->links()}}

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

 jquery.jscroll/jquery.jscroll.min.js');?>  

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

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

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

希望有一天能帮助别人!

感谢Pretty Good Pancake这个解决方案,效果很好。 但是我认为在Laravel 4中,Response Facade不再有error()方法,所以类似App::abort('404', '...')Response::make('...', 404)会工作。 只需记住将文件添加use Illuminate\Support\Facades\.. ,因为该文件是命名空间。

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