2017-05-09 65 views
0

所以我使用ZF與教義,我試圖使用分頁這樣的:因爲我傳遞數組總是要包含10項Zend的分頁程序1

$d = Zend_Paginator::factory($this->items->displayItems(['page' => 1]),'Array'); 
    $d->setCurrentPageNumber(1); 
    var_dump($d->getPages()); 

I need a way to set the total count of the pages for the paginator每頁,有沒有辦法做到這一點?我需要這個爲了使分頁程序工作,因爲現在正在顯示這樣的:

object(stdClass)#285 (12) { ["pageCount"]=> int(1) ["itemCountPerPage"]=> int(10) ["first"]=> int(1) ["current"]=> int(1) ["last"]=> int(1) ["pagesInRange"]=> array(1) { [1]=> int(1) } ["firstPageInRange"]=> int(1) ["lastPageInRange"]=> int(1) ["currentItemCount"]=> int(10) ["totalItemCount"]=> int(10) ["firstItemNumber"]=> int(1) ["lastItemNumber"]=> int(10) } 

正如你可以看到pageCount爲1,我有表中超過100個項目。

我沒有找到任何方法,如->setToalPagesCount()爲paginator,有沒有辦法設置這樣的東西?

回答

0

如果你想設置每本頁面項目是方法:

$paginator->setItemCountPerPage(ItemsPerPage); 
+0

不,算總的I確實找到了解決方案,我會將其作爲答案發布。 – Uffo

0

所以,我已經找到了解決方案,該解決方案是創建自己的分頁程序適配器並添加setCount方法,因此這種方式我可以提供總數。你可以用你喜歡的命名空間我Bisna這樣的路徑是:library\Bisna\Application\Paginator\Array

<?php 
/** 
* Zend Framework 
* 
* LICENSE 
* 
* This source file is subject to the new BSD license that is bundled 
* with this package in the file LICENSE.txt. 
* It is also available through the world-wide-web at this URL: 
* http://framework.zend.com/license/new-bsd 
* If you did not receive a copy of the license and are unable to 
* obtain it through the world-wide-web, please send an email 
* to [email protected] so we can send you a copy immediately. 
* 
* @category Zend 
* @package Zend_Paginator 
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 
* @license http://framework.zend.com/license/new-bsd  New BSD License 
* @version $Id$ 
*/ 

/** 
* @see Zend_Paginator_Adapter_Interface 
*/ 
require_once 'Zend/Paginator/Adapter/Interface.php'; 

/** 
* @category Zend 
* @package Zend_Paginator 
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 
* @license http://framework.zend.com/license/new-bsd  New BSD License 
*/ 
class Bisna_Application_Paginator_Array implements Zend_Paginator_Adapter_Interface 
{ 
    /** 
    * Array 
    * 
    * @var array 
    */ 
    protected $_array = null; 

    /** 
    * Item count 
    * 
    * @var integer 
    */ 
    protected $_count = null; 

    /** 
    * Constructor. 
    * 
    * @param array $array Array to paginate 
    */ 
    public function __construct(array $array) 
    { 
     $this->_array = $array; 
    } 

    /** 
    * Returns an array of items for a page. 
    * 
    * @param integer $offset Page offset 
    * @param integer $itemCountPerPage Number of items per page 
    * @return array 
    */ 
    public function getItems($offset, $itemCountPerPage) 
    { 
     return array_slice($this->_array, $offset, $itemCountPerPage); 
    } 

    /** 
    * @param $count 
    */ 
    public function setCount($count){ 
     $this->_count = $count; 
    } 

    /** 
    * Returns the total number of rows in the array. 
    * 
    * @return integer 
    */ 
    public function count() 
    { 
     return $this->_count; 
    } 
} 

所以現在我有setCount()方法,你會用的方式是這樣的:

$adapter = new Bisna_Application_Paginator_Array($this->items->list('pass arguments with the active page number')); 
$adapter->setCount(1000); //maybe use another query from db here to calculate the total amount of pages 
$paginator = new Zend_Paginator($adapter);