2015-09-03 58 views
0

我有來自API的數據,我想使用CakePHP的分頁顯示結果。我在分頁中看到的所有內容都需要傳入一個查詢或模型對象。是否可以傳入類似數據或總項目,當前項目和每頁項目的數組?CakePHP 3.0無模式分頁

回答

1

該解決方案比我預想的要容易。我能夠向請求參數添加分頁元素。分頁組件根本不需要被觸摸。

$data = $Utility->getData(); 
$perPage = 20; 
$count = count($data); 
$page = isset($this->request->query['page']) ? $this->request->query['page'] : 1; 
$offset = $perPage * ($page - 1); 
$pageCount = floor($count/$perPage); 
$this->request->params['paging'] = [ 
    'Messages' => [ 
     'page' => $page, 
     'current' => $offset, 
     'count' => $count, 
     'perPage' => $perPage, 
     'prevPage' => $page != 1, 
     'nextPage' => $page != $pageCount, 
     'pageCount' => $pageCount, 
    ] 
]; 
+0

更可能的密鑰可以在** HTTPS發現://github.com/cakephp/cakephp/blob/3.0.12/src/Controller/Component/PaginatorComponent.php#L193** – ndm

+0

我想你計算$ pageCount時應使用ceil()而不是floor() – miro