简体   繁体   English

Zend分页器1

[英]Zend Paginator 1

So I'm using ZF with doctrine, and I'm trying to use pagination like this: 因此,我将ZF与教义结合使用,并且尝试使用这样的分页:

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

But I need a way to set the total count of the pages for the paginator because the array that I'm passing is always going to contain 10 items per page, is there a way to do this? 但是I need a way to set the total count of the pages for the paginator因为我传递的数组总是每页包含10个项目,有没有办法做到这一点? I need this in order for the paginator to work, because right now is displaying this: 我需要此命令以便分页器正常工作,因为现在正在显示以下内容:

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) }

As you can see the pageCount is 1 and I have over 100 items in the table. 如您所见, pageCount为1,表中有100多个项目。

I didn't find any methods like ->setToalPagesCount() for the paginator, is there a way to set something like this? 我没有找到用于分页程序的->setToalPagesCount()之类的方法,有没有办法设置类似这样的方法?

如果您尝试设置每页项目,则可以使用以下方法:

$paginator->setItemCountPerPage( ItemsPerPage );

So I have found a solution and the solution was to create my own paginator adapter and add the setCount method so this way I could provide the total count. 因此,我找到了一个解决方案,解决方案是创建自己的分页器适配器并添加setCount方法,这样我就可以提供总数。 You can use what namespace you like I had Bisna so the path is: library\\Bisna\\Application\\Paginator\\Array 您可以使用喜欢的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;
    }
}

So now I have the setCount() method, the way you will use this is: 现在我有了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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM