970_90

Tuesday, 12 May 2015

Zend framework pagination with array adapter - with code snippets

Zend pagination can be implemented with used of pagination component which is available with Zend Framework v1.6.
It is so loosely coupled that you can use it wherever you want without worrying about any other component at all.

Zend framework pagination with array adapter


In zend frameowork 1.12, Zend paging component provides 5 types of Adapter and are following:
1. Array:
Use a PHP array.
2. DbSelect: Use a Zend_Db_Select instance which return an array.
3. DbTableSelect: Use a Zend_Db_Table_Select instance, which will return the instance of Zend_Db_Table_Rowset_Abstract.
4. Iterator: Use an Iterator instance.
5. Null: Do not use Zend_Paginator to manage data pagination. You can still take advantage of the pagination control feature.

Above out of 5, Here we are using first one with code snippets.



Add following code in views/scripts/mypaging.phtml (This is global file paging)
<div class="pagination" style="width:100%">
    <div style="float:left;width:28%">
    </div>
    <div style="float:right;width:70%;">
        <!-- First page link -->
        <?php if (isset($this->previous)): ?>
              <a href="/<?php echo $this->url(array('page' => $this->first)); ?>">Start</a> |
        <?php else: ?>
                <span class="disabled">Start</span> |
        <?php endif; ?>
 
        <!-- Previous page link -->
 
        <?php if (isset($this->previous)): ?>
              <a href="/<?php echo $this->url(array('page' => $this->previous)); ?>">&lt; Previous</a> |
        <?php else: ?>
            <span class="disabled">&lt; Previous</span> |
        <?php endif; ?>
        <!-- Numbered page links -->
        <?php foreach ($this->pagesInRange as $page): ?>
            <?php if ($page != $this->current): ?>
                <a href="/<?php echo $this->url(array('page' => $page)); ?>"><?php echo $page; ?></a>
            <?php else: ?>
                <?php echo $page; ?>
            <?php endif; ?>
        <?php endforeach; ?>
        <!-- Next page link -->
        <?php if (isset($this->next)): ?>
              | <a href="/<?php echo $this->url(array('page' => $this->next)); ?>">Next &gt;</a> |
        <?php else: ?>
            | <span class="disabled">Next &gt;</span> |
        <?php endif; ?>
        <!-- Last page link -->
        <?php if (isset($this->next)): ?>
              <a href="/<?php echo $this->url(array('page' => $this->last)); ?>">End</a>
        <?php else: ?>
         
        <?php endif; ?>      
    </div>
 </div>


Add Following CSS File for paging- It will design the paging and you can change as per your requirement.
.pagination li {
        border: 0;
        margin: 0;
        padding: 0;
        font-size: 11px;
        list-style: none; /* savers */
        float: left;
}

/* savers .pagination li,*/
.pagination a {
        border-right: solid 1px #DEDEDE;
        margin-right: 2px;
}

.pagination .previous-off,.pagination .next-off {
        color: #888888;
        display: block;
        float: left;
        font-weight: bold;
        padding: 3px 4px;
}

.pagination .next a,.pagination previous a {
        border: none;
        font-weight: bold;
}

.pagination .active {
        color: #000000;
        font-weight: bold;
        display: block;
        float: left;
        padding: 3px 6px; /* savers */
        border-right: solid 1px #DEDEDE;
}

.pagination a:link,.pagination a:visited {
        color: #0e509e;
        display: block;
        float: left;
        padding: 3px 6px;
        text-decoration: underline;
}

.pagination a:hover {
        text-decoration: none;
}



Add Following code in your action of controller.
This is will responsible only for paging.
$data = range(1, 100);
$page = $this->getRequest()->getParam('page', 0);
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($data));
$paginator->setCurrentPageNumber($page);//This is current page       
$paginator->setItemCountPerPage(20); //Total number of records per page
$this->view->paginator = $paginator;  



Add Following code in your view file of action.
This will list the records.

<?php if (count($this->paginator)): ?>
    <ul>
        <?php foreach ($this->paginator as $item): ?>
            <li><?php echo $item; ?></li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

<?php
//This is used for pagination
echo $this->paginationControl($this->paginator, 'Sliding', 'my_pagination_control.phtml');
?>



No comments:

Post a Comment