2015-10-19 125 views
6

我成功地從控制器無限滾動jQuery和Laravel 5 PAGINATE

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> 

一切工作很好直到現在。

但是,我根本沒有得到官方文檔。什麼是'div.navigation'和'#content div.post'?他們應該在我的情況?

段從文檔:

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

編輯:我的JavaScript到目前爲止

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

的〔< [1] 2] 3]>]部分在頁面的底部創建,但無限滾動不起作用。另外,我在控制檯中沒有記錄或錯誤。

+0

好吧,它在評論中描述了那些選擇器。 'div.navigation'是你的導航(你沒有,但是你可以像'$ posts-> render()')那樣輸出它。和'itemSelector'是一個項目的選擇器(在你的情況下:'div.col-md-4'。想想爲'post'添加另一個類)。 – Tim

+0

我按照你的說法編輯了我的問題並添加了課程,但是,我仍然無法將它們連接在我的大腦中。你能告訴我嗎? – senty

+0

請參閱我的infinit滾動插件L5分頁這裏http://stackoverflow.com/questions/31853472/laravel-infinite-scroll-for-pagination-output –

回答

5

首先,你需要添加分頁本身像這樣關#content DIV後:

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

這將輸出類似:

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

要覆蓋分頁演示看看this answer on SO

那麼你的配置是這樣的:

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

基本上,無限滾動條會打電話給你的分頁鏈接,因此需要知道,這裏的一切是位於把它放在一起。

+0

非常感謝您的答案。我還有最後一個問題。我應該在哪裏放'class =「分頁」'?我應該改變哪一個?它明確必須是「ul」嗎? – senty

+0

你把'{!! $ posts-> render()!!}'放在'#content' div的末尾。您不必在任何地方放置'class =「分頁符」,因爲這會通過Laravel函數獲得輸出。是的,它必須是'ul',因爲這是'render'函數使用的格式。 – Tim

+0

我非常困惑。我明白,我只會把'{!! $ posts-> render()!!}'輸出'

    ...'位。所以在'class =「item」'裏面,是嗎?然後在那之下,我會加上'
' – senty