2014-10-30

Creating pagination in zf1

For creating pagination in zend framework 1 you have to perform several steps:
1) Set up pagination template.
Create file application/views/scripts/pagination.phtml:
<div>
    <ul class="pagination">
        <?php if (isset($this->previous)): ?>
            <li><a href="<?= $this->url(array('page' => $this->first)); ?>"><span>&laquo;</span></a>
        <?php else: ?>
            <li class="disabled"><span>&laquo;</span></li>
        <?php endif; ?>

        <?php if (isset($this->previous)): ?>
            <li><a href="<?= $this->url(array('page' => $this->previous)); ?>">&larr;</a></li>
        <?php else: ?>
            <li class="disabled"><span>&larr;</span></li>
        <?php endif; ?>
        <!-- Numbered page links -->
        <?php foreach ($this->pagesInRange as $page): ?>
            <?php if ($page != $this->current): ?>
                <li><a href="<?= $this->url(array('page' => $page)); ?>"><?= $page; ?></a></li>
            <?php else: ?>
                <li class="active"><span><?= $page; ?></span></li>
            <?php endif; ?>
        <?php endforeach; ?>
        <!-- Next page link -->
        <?php if (isset($this->next)): ?>
            <li><a href="<?= $this->url(array('page' => $this->next)); ?>">&rarr;</a></li>
        <?php else: ?>
            <li class="disabled"><span>&rarr;</span></li>
        <?php endif; ?>
        <!-- Last page link -->
        <?php if (isset($this->next)): ?>
            <li><a href="<?= $this->url(array('page' => $this->last)); ?>">&raquo;</a></li>
        <?php else: ?>
            <li class="disabled"><span>&raquo;</span></li>
        <?php endif; ?>
    </ul>
</div>
2) Create an instance of Zend_Paginator with paginator adapter.
For instance, you can create method getPaginator in your table model:
/**
 * Class Application_Model_DbTable_Task
 */
class Application_Model_DbTable_Task extends Zend_Db_Table_Abstract
{
    protected $_name = 'task';
    protected $_rowClass = 'Application_Model_Task';

    public function getPaginator(array $where = [])
    {
        $db = $this->getAdapter();
        $select = $db->select();
        $select->from($this->_name);

        foreach ($where as $key => $value) {
            $select->where($key, $value);
        }

        $select->order('created_at DESC');

        $adapter = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($adapter);

        return $paginator;
    }
}
and in your controller:
$user = Application_Model_User::getCurrent();
$tableTask = new Application_Model_DbTable_Task();
$paginator = $tableTask->getPaginator(['id_user = ?' => $user->id]);

$page = $this->getRequest()->getParam('page', 1);
$paginator->setCurrentPageNumber($page);
$config = $this->getInvokeArg('bootstrap')->getOptions();
$paginator->setItemCountPerPage($config['pagination']['per_page']);

$this->view->paginator = $paginator;
3) Configure pagination in settings file and in your bootstrap.
Add to application.ini:
pagination.per_page = 10
and in bootstrap:
protected function _initPagination()
{
    Zend_Paginator::setDefaultScrollingStyle('Sliding');
    Zend_View_Helper_PaginationControl::setDefaultViewPartial(
        'pagination.phtml'
    );
}
4) Print pagination html.
Add in view template:
<?php if (count($this->paginator)): ?>
    <?php foreach ($this->paginator as $item): ?>
        <div class="item">
            <?= $item['value'] ?> (<?= $item['state'] ?>)
        </div>
    <?php endforeach; ?>
<?php else: ?>
    <div class="empty">
        Nothing is found.
    </div>
<?php endif; ?>

<?= $this->paginator ?>
where $item - a row from db

No comments:

Post a Comment