2015-10-19 60 views
4

我正在尝试使用paginate()来实现无限滚动。我认为最简单的方法是使用'无限滚动'来实现这一点。如果您有任何其他建议,如何做到这一点没有无限滚动库,只是使用jQuery,我很乐意知道..Laravel 5分页+无限滚动jQuery

我返回变量查看这样的:

public function index() 
{ 
    $posts = Post::with('status' == 'verified') 
         ->paginate(30); 

    return view ('show')->with(compact('posts')); 
} 

我的观点:

<div id="content" class="col-md-10"> 
    @foreach (array_chunk($posts->all(), 3) as $row) 
     <div class="post row"> 
      @foreach($row as $post) 
       <div class="item col-md-4"> 
        <!-- SHOW POST --> 
       </div> 
      @endforeach 
     </div> 
    @endforeach 
    {!! $posts->render() !!} 
</div> 

JavaScript部分:

$(document).ready(function() { 
    (function() { 
    var loading_options = { 
     finishedMsg: "<div class='end-msg'>End of content!</div>", 
     msgText: "<div class='center'>Loading news items...</div>", 
     img: "/assets/img/ajax-loader.gif" 
    }; 

    $('#content').infinitescroll({ 
     loading: loading_options, 
     navSelector: "ul.pagination", 
     nextSelector: "ul.pagination li:last a", // is this where it's failing? 
     itemSelector: "#content div.item" 
    }); 
    }); 
}); 

但是,这是行不通的。 - > render()部分正在工作,因为我正在获取[< [1] 2] 3]>]部分。但是,无限滚动不起作用。我也不会在控制台中发现任何错误。

[< [1] 2 3]>]是这样的观点:来源:

<ul class="pagination"> 
     <li class="disabled"><span>«</span> </li>     // « 
     <li class="active"><span>1</span></li>      // 1 
     <li><a href="http://test.dev/?page=2">2</a></li>    // 2 
     <li><a href="http://test.dev/?page=3">3</a></li>    // 3 
     <li><a href="http://test.dev/?page=2" rel="next">»</a></li> // » 
</ul> 
+0

我得承认,我” m不熟悉infinitescroll插件,但我做了一些我自己构建的无限滚动条。我将这些页面变量保存在一个隐藏的输入框中,当您滚动到底部时,页面计数得到更新,并且基于新页面编号抽取新条目的函数被调用并将新条目追加到容器底部。我还设置了一个max_pages隐藏输入框,因此当用户点击该max_page数时,您会看到类似页面末尾的消息。我不知道这是否是最好的方式,但它对我来说最合适。你想看到一些经验吗? – cbloss793

+0

只要我也可以使用加载文本/图像(因此具有加载状态的功能),我愿意接受任何建议。我认为最好自己做,而不是使用图书馆,但我想我会喜欢Laravel的'paginate()'。我很想看看你的方法。你能适应我的情况吗? – senty

+0

当然!在下面查看我的答案。 :) – cbloss793

回答

1

您应该能够就好了,只要使用分页为您的电话,以获得新的职位与页面加载不同。所以你会有两个Laravel调用:

1.)提供页面模板(包括jQuery,CSS和你的max_page count变量 - 查看HTML) 2.)为了AJAX来调用帖子的基础在你给它的页面上。

这是我得到了我的无限滚动工作...

HTML:

<!-- Your code hasn't changed--> 
<div id="content" class="col-md-10"> 
    @foreach (array_chunk($posts->all(), 3) as $row) 
    <div class="post row"> 
     @foreach($row as $post) 
      <div class="item col-md-4"> 
       <!-- SHOW POST --> 
      </div> 
     @endforeach 
    </div> 
    @endforeach 
    {!! $posts->render() !!} 
</div> 

<!-- Holds your page information!! --> 
<input type="hidden" id="page" value="1" /> 
<input type="hidden" id="max_page" value="<?php echo $max_page ?>" /> 

<!-- Your End of page message. Hidden by default --> 
<div id="end_of_page" class="center"> 
    <hr/> 
    <span>You've reached the end of the feed.</span> 
</div> 

在页面加载,你将在MAX_PAGE可变填写(所以做这样的事情:ceil(Post::with('status' == 'verified')->count()/30);

接下来,你的jQuery:

var outerPane = $('#content'), 
didScroll = false; 

$(window).scroll(function() { //watches scroll of the window 
    didScroll = true; 
}); 

//Sets an interval so your window.scroll event doesn't fire constantly. This waits for the user to stop scrolling for not even a second and then fires the pageCountUpdate function (and then the getPost function) 
setInterval(function() { 
    if (didScroll){ 
     didScroll = false; 
     if(($(document).height()-$(window).height())-$(window).scrollTop() < 10){ 
     pageCountUpdate(); 
    } 
    } 
}, 250); 

//This function runs when user scrolls. It will call the new posts if the max_page isn't met and will fade in/fade out the end of page message 
function pageCountUpdate(){ 
    var page = parseInt($('#page').val()); 
    var max_page = parseInt($('#max_page').val()); 

    if(page < max_page){ 
     $('#page').val(page+1); 
     getPosts(); 
     $('#end_of_page').hide(); 
    } else { 
     $('#end_of_page').fadeIn(); 
    } 
} 


//Ajax call to get your new posts 
function getPosts(){ 
    $.ajax({ 
     type: "POST", 
     url: "/load", // whatever your URL is 
     data: { page: page }, 
     beforeSend: function(){ //This is your loading message ADD AN ID 
      $('#content').append("<div id='loading' class='center'>Loading news items...</div>"); 
     }, 
     complete: function(){ //remove the loading message 
      $('#loading').remove 
     }, 
     success: function(html) { // success! YAY!! Add HTML to content container 
      $('#content').append(html); 
     } 
    }); 

} //end of getPosts function 

有雅去临屋都是。我使用Masonry这个代码,所以动画效果非常好。

+0

感谢您的回答。这真的很好解释。然后,我调整了你的代码,结果是'Uncaught SyntaxError:Unexpected token')。我相信这是因为我在没有csrf token的情况下尝试使用ajax post。我尝试添加'var currentPage = {'page':$('#page')。val(),“_token”:“{{{csrf_token()}}}”};'并改变'data:{page:数据:当前页面“,以及”数据:{页面:当前页面}“,但无法使其工作。 – senty

+0

意外的令牌错误通常意味着存在语法错误。你有没有检查以确保所有括号都正确关闭?你还使用Laravel 5吗? – cbloss793

+0

是的,我正在使用L5。我发现了这个问题,并根据语法进行了修正。但是现在,我仍然在页面末尾看到'[<[1]2]3]>]'以及'您已达到Feed的结尾'。现在,即使我尝试了'{'page':$('#page')。val(),“_token”:“{{{csrf_token()}}}},我开始在VerifyCsrfToken.php中得到'TokenMismatchException。 ;'* *修正语法错误后我确实做了什么*:改成'var currentPage = {'page':page,“_token”:“{{{csrf_token()}}}”}'和'data: currentPage,' – senty

1

易于和乐于助人是本教程 - http://laraget.com/blog/implementing-infinite-scroll-pagination-using-laravel-and-jscroll

最终脚本可能是这样的一个

{!! HTML::script('assets/js/jscroll.js') !!} 
<script> 
    $('.link-pagination').hide(); 
    $(function() { 
     $('.infinite-scroll').jscroll({ 
      autoTrigger: true, 
      loadingHtml: '<img class="center-block" src="/imgs/icons/loading.gif" alt="Loading..." />', // MAKE SURE THAT YOU PUT THE CORRECT IMG PATH 
      padding: 0, 
      nextSelector: '.pagination li.active + li a', 
      contentSelector: 'div.infinite-scroll', 
      callback: function() { 
       $('.link-pagination').remove(); 
      } 
     }); 
    }); 
</script> 

你只需要使用laravel的分页

{!! $restaurants->links() !!}