Laravel 5 分页 + 无限滚动 jQuery [英] Laravel 5 Paginate + Infinite Scroll jQuery

查看:15
本文介绍了Laravel 5 分页 + 无限滚动 jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 paginate() 来实现无限滚动.我认为最简单的方法是使用无限滚动"来实现这一点.如果您有任何其他建议如何在没有无限滚动库的情况下做到这一点,只需使用 jQuery,我很高兴知道..

我正在返回变量以查看如下:

公共函数索引(){$posts = Post::with('status' == 'verified')-> 分页(30);返回视图 ('show')->with(compact('posts'));}

我的观点:

@foreach (array_chunk($posts->all(), 3) as $row)<div class="post row">@foreach($row 作为 $post)<div class="item col-md-4"><!-- 显示帖子-->

@endforeach

@endforeach{!!$posts->render() !!}

Javascript 部分:

$(document).ready(function() {(功能() {var loading_options = {finishMsg: "<div class='end-msg'>内容结束!</div>",msgText: "<div class='center'>正在加载新闻项目...</div>",img: "/assets/img/ajax-loader.gif"};$('#content').infinitescroll({加载:loading_options,navSelector: "ul.pagination",nextSelector: "ul.pagination li:last a",//这是失败的地方吗?itemSelector: "#content div.item"});});});

然而,这行不通.->render() 部分正在工作,因为我得到了 [<[1]2]3]>] 部分.但是,无限滚动不起作用.我也没有在控制台中收到任何错误.

[<[1]2]3]>] 在视图中是这样的:来源:

    <li class="disabled"><span>«</span>//«<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>//»

解决方案

只要您获取新帖子的调用与页面加载不同,您就应该能够很好地使用分页.所以你有两个 Laravel 调用:

1.) 提供页面的模板(包括 jQuery、CSS 和您的 max_page 计数变量——查看 HTML)2.) 让 AJAX 根据您提供的页面调用帖子.

这就是我如何让我的无限卷轴工作......

HTML:

<div id="content" class="col-md-10">@foreach (array_chunk($posts->all(), 3) as $row)<div class="post row">@foreach($row 作为 $post)<div class="item col-md-4"><!-- 显示帖子-->

@endforeach

@endforeach{!!$posts->render() !!}

<!-- 保存您的页面信息!!--><input type="hidden" id="page" value="1"/><input type="hidden" id="max_page" value="<?php echo $max_page ?>"/><!-- 您的页面结束消息.默认隐藏 --><div id="end_of_page" class="center"><小时/><span>您已到达提要的结尾.</span>

在页面加载时,您将填写 max_page 变量(因此请执行以下操作:ceil(Post::with('status' == 'verified')->count()/30);.

接下来,您的 jQuery:

var outerPane = $('#content'),didScroll = 假;$(window).scroll(function() {//观察窗口滚动didScroll = 真;});//设置一个时间间隔,这样你的 window.scroll 事件就不会一直触发.这会等待用户停止滚动甚至一秒钟,然后触发 pageCountUpdate 函数(然后是 getPost 函数)设置间隔(函数(){如果(didScroll){didScroll = 假;if((($(document).height()-$(window).height())-$(window).scrollTop() <10){pageCountUpdate();}}}, 250);//该函数在用户滚动时运行.如果不满足 max_page ,它将调用新帖子,并将淡入/淡出页面消息的末尾函数 pageCountUpdate(){var page = parseInt($('#page').val());var max_page = parseInt($('#max_page').val());如果(页面

你去吧!就这样.我也在此代码中使用了 Masonry,因此动画效果非常好.

I am trying to use paginate() to achieve infinite scroll. I think the easiest way is using the 'infinite-scroll' to achieve this. If you have any other suggestion how to do it without infinite-scroll library, just using jQuery, I'd be happy to know..

I am returning the variable to view like this:

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

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

My View:

<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 Part:

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

However, this doesn't work. The ->render() part is working because I am getting [<[1]2]3]>] part. However, the infinite scroll doesn't work. I also don't get any errors in the console.

[<[1]2]3]>] is like this in the view:source:

<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>

解决方案

You should be able to use the Pagination just fine as long as your call to get new posts is different than page load. So you'd have two Laravel calls:

1.) To provide the template of the page (including jQuery, CSS, and your max_page count variable -- view HTML) 2.) For the AJAX to call posts based on the page you give it.

This is how I got my infinity scroll to work...

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>

On page load, you will fill in the max_page variable (so do something like this: ceil(Post::with('status' == 'verified')->count() / 30);.

Next, your 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

There ya go! That's all. I was using Masonry with this code also so the animation worked wonderfully.

这篇关于Laravel 5 分页 + 无限滚动 jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆