簡體   English   中英

無限滾動 laravel 和 ajax

[英]infinite scroll laravel and ajax

there is a reference for me to create an infinite scroll with laravel blade and ajax to request api and switch pages.I've tried to directly call with the database I can do it but when I call the api I don't get the logic

The logic is: once the page scrolled to a specific height The page call an ajax request to call the next data and append it with jquery or javascript.

假設我們有一個包含許多帖子的頁面,並且您想在其上執行無限滾動。

首先在索引 function 中,查看評論:-

  public function Index(Request $request)
    {
    
    // First we Call all our posts , which will be the first data to be shown in the page before scroll .
        $posts = Post::paginate(12); 
    
// Then we're checking , if this request an ajax-request , return a response  contains the post component to be rendered during the scroll 
 
        if ($request->ajax()) {
            $view = view('data',compact('posts'))->render();
            return response()->json(['html'=>$view]);
        }
    
    
        return view('my-post',compact('posts'));
    }

假設那是我的帖子刀片:-

<!DOCTYPE html>
<html>
<head>
    <title>Laravel infinite scroll pagination</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <style type="text/css">
        .ajax-load{
            background: #e1e1e1;
            padding: 10px 0px;
            width: 100%;
        }
    </style>
</head>
<body>


<div class="container">
    <h2 class="text-center">Laravel infinite scroll pagination</h2>
    <br/>
    <div class="col-md-12" id="post-data">
        @include('data')
    </div>
</div>


<div class="ajax-load text-center" style="display:none">
    <p><img src="http://demo.itsolutionstuff.com/plugin/loader.gif">Loading More post</p>
</div>


<script type="text/javascript">
    var page = 1;
    $(window).scroll(function() {
        if($(window).scrollTop() + $(window).height() >= $(document).height()) {
            page++;
            loadMoreData(page);
        }
    });


    function loadMoreData(page){
      $.ajax(
            {
                url: '?page=' + page,
                type: "get",
                beforeSend: function()
                {
                    $('.ajax-load').show();
                }
            })
            .done(function(data)
            {
                if(data.html == " "){
                    $('.ajax-load').html("No more records found");
                    return;
                }
                $('.ajax-load').hide();
                $("#post-data").append(data.html);
            })
            .fail(function(jqXHR, ajaxOptions, thrownError)
            {
                  alert('server not responding...');
            });
    }
</script>


</body>
</html>

這將是滾動期間呈現的組件:-

@foreach($posts as $post)
<div>
    <h3><a href="">{{ $post->title }}</a></h3>
    <p>{{ str_limit($post->description, 400) }}</p>


    <div class="text-right">
        <button class="btn btn-success">Read More</button>
    </div>


    <hr style="margin-top:5px;">
</div>
@endforeach

而已

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM