【问题标题】:Zend Paginator 1Zend 分页器 1
【发布时间】:2017-05-09 17:25:53
【问题描述】:

所以我将 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 个项目,有没有办法做到这一点?我需要这个才能让分页器工作,因为现在正在显示这个:

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()这样的分页器方法,有没有办法设置这样的东西?

【问题讨论】:

    标签: php zend-framework pagination


    【解决方案1】:

    如果您尝试为每页设置项目,方法如下:

    $paginator->setItemCountPerPage( ItemsPerPage );
    

    【讨论】:

    • 不,总计我确实找到了解决方案,并将其作为答案发布。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 2013-06-02
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多