2015-08-25 88 views
0

即時嘗試實現一個視圖,如獅身人面像文檔,您可以點擊下一頁和上一頁在頁面之間導航。不知道如果即時使用陣列上的自定義paginator權利,我不能得到頁面導航鏈接顯示在視圖頁面上。這是代碼:laravel 5自定義分頁程序不工作

public function paginate($array, $perPage, $pageStart=1) { 

    $offset = ($pageStart * $perPage) - $perPage; 

    return new Paginator(array_slice($array, $offset, $perPage, true), $perPage, $pageStart); 
    } 

    view()->composer('layouts.book', function($view) 
    { 
     //some other code 
     $pages = []; 
     foreach($book->textsection_pages as $textsection) { 
      $pages[] = $textsection->alias; 
     } 
     foreach($book->task_pages as $task) { 
      $pages[] = $task->alias; 
     } 
     foreach($book->tutor_pages as $tutor) { 
      $pages[] = $tutor->alias; 
     } 
     foreach($book->eval_pages as $eval) { 
      $pages[] = $eval->alias; 
     } 

     $chapters = $book->chapters()->orderBy('weight', 'asc')->get(); 
     $paginated = $this->paginate($pages, 1); 
     $view->with(['chapters' => $chapters, 'book' => $book, 'paginated' => $paginated]); 

    }); 

並在視圖我打電話{!! $paginated->render() !!},但不顯示導航鏈接。

+0

有託運laravel 5 API,分頁程序的構造是'__construct(混合$項目,詮釋$總,詮釋$ perPage,INT | null $ currentPage = null,array $ options = array())' – yangqi

+0

是的,我做過。它的'__construct($ items,$ perPage,$ currentPage = null,array $ options = [])' –

+0

啊,我看到這是因爲你在一個閉包中調用了paginate()方法,'$ this'沒有引用去上課。 – yangqi

回答

0

這是我在我的應用程序(Laravel 5.1)中處理自定義分頁的方式。創建的局部視圖resources/views/partials/pagination.blade.php

@if ($paginator->lastPage() > 1) 
<ul id="pagination"> 
    <li> 
    @if ($paginator->currentPage() > 1) 
     <a class="prev" href="{{ $paginator->url($paginator->currentPage()-1) }}">Previous</a> 
    @else 
     <span class="disabled">Previous</span> 
    @endif 
    </li> 

    <li> 
    @if ($paginator->currentPage() !== $paginator->lastPage()) 
     <a class="next" href="{{ $paginator->url($paginator->currentPage()+1) }}" >Next</a> 
    @else 
     <span class="disabled">Next</span> 
    @endif 
    </li> 
</ul> 
@endif 

在我的控制器,我通過通常的方法$model->paginate(10)。然後,在我看來,最後,我想使用分頁程序,resources/views/list.blade.php我這樣做:

@foreach ($objects as $object) 
    // Here we list the object properties 
@endforeach 
@include("partials.pagination", ['paginator' => $objects]) 
+0

嘿,你仍然在模型上使用內置的分頁器。我想要一個paginator,在一個集合或數組上工作。 –

+0

哦,我明白了。我的錯。 – CreativityKills