Zend Paginator 1

时间:2017-05-09 08:49:34

标签: php zend-framework pagination

所以我使用ZF和学说,我试图像这样使用分页:

    $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因为我传递的数组总是每页包含10个项目,有没有办法做到这一点?我需要这个才能让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()这样的任何方法用于分页器,有没有办法设置这样的东西?

2 个答案:

答案 0 :(得分:0)

如果您尝试在每页设置项目,则这是方法:

$paginator->setItemCountPerPage( ItemsPerPage );

答案 1 :(得分:0)

所以我找到了一个解决方案,解决方案是创建自己的paginator适配器并添加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 license@zend.com 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);