Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite Scroll jQuery & Laravel 5 Paginate

I am successfully returning the data from Controller

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

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

Also, I am successfully showing everything in 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>

Everything is working nicely until now.

However, I didn't get the official documentation at all. What is 'div.navigation' and '#content div.post'? What should they be in my case?

Snippet From Documentation:

$('#content').infinitescroll({

   navSelector  : "div.navigation",            
                   // selector for the paged navigation (it will be ?>hidden)
    nextSelector : "div.navigation a:first",    
                   // selector for the NEXT link (to page 2)
    itemSelector : "#content div.post"          
                   // selector for all items you'll retrieve
});

Edit: My Javascript So Far

$(document).ready(function() {
(function() {
     var loading_options = {
        finishedMsg: "<div class='end-msg'>Congratulations! You've reached the end of the internet</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.navigation a:first",
         itemSelector: "#content div.item"
     });
 });
}); 

The [<[1]2]3]>] part is created at the bottom of the page but infinite scroll doesn't work. Also, I get no logs or errors in the console.

like image 459
senty Avatar asked Oct 19 '15 18:10

senty


People also ask

What is infinite scrolling in jQuery?

The jQuery Infinite Scroll feature has become quite common now days. Different websites including Facebook and Twitter are already using it. This feature helps in creating Auto Paging (not link-based paging) in a web page so when a user scrolls down to the bottom, the next page's content are automatically loaded.

How do you make infinite scroll?

First, create a new folder called infinite-scroll . Inside that folder, create two subfolders css and js . Second, create the style. css in the css folder and app.

What is an infinite scrolling page?

Infinite scrolling is a web-design technique that loads content continuously as the user scrolls down the page, eliminating the need for pagination. The success of infinite scrolling on social media sites such as Twitter have made this technique popular, but that doesn't mean you should do it too.

What is infinite Ajax scroll?

Infinite Ajax Scroll empowers web professionals to create powerful interactive paginations. Back-button proof. The browser url is automatically updated when you scroll between pages. Users can reload or bookmark the page and always return to the right place.


1 Answers

First you need to add the pagination itself like this after the closing #content div:

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

This will output something like:

<ul class="pagination"><li><a>...</a></li>

To overwrite the pagination presenter have a look at this answer on SO.

Then your configuration looks like this:

$(document).ready(function() {
    var loading_options = {
        finishedMsg: "<div class='end-msg'>Congratulations! You've reached the end of the internet</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 a:first",
        itemSelector: "#content div.item"
    });
}); 

Basically, the infinite scroller will call the pagination links for you and thus needs to know, where everything is located to put it all together.

like image 52
Tim Avatar answered Oct 13 '22 06:10

Tim