[ZF] Paginación con Zend_Paginator

En el controller (/application/default/controllers/MediabrowserController.php):

public function imagesListPrivate(){ ini_set('max_execution_time', 300); //300 seconds = 5 minutes // Modelo $cc_media_image = Sqy_Db_Table::getInstance('cc_media_image'); // Se obtienen todas las imágenes que coincidan con el filter y category recibidos por POST //$images = $cc_media_image->getAll(@trim($_POST['filter']), @trim($_POST['category'])); //$images = $cc_media_image->getAll(@trim($_POST['filter']), @trim('cocineros')); $images = $cc_media_image->getAll(@trim($_POST['filter']), @trim('recursos'));
// PAGINACION CON Zend_Paginator:
//    Method                     Description
//    setCurrentPageNumber     Sets the current page number (default 1).
//    setItemCountPerPage     Sets the maximum number of items to display on a page (default 10).
//    setPageRange             Sets the number of items to display in the pagination control (default 10). Note: Most of the time this number will be adhered to exactly, but scrolling styles do have the option of only using it as a guideline or starting value (e.g., Elastic).
//    setView                 Sets the view instance, for rendering convenience.
//$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($images));
$paginator = Zend_Paginator::factory($images);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(50);
$paginator->setPageRange(10);
$paginator->setView($this->view);
//Zend_Paginator::setDefaultScrollingStyle('Sliding');
//Zend_View_Helper_PaginationControl::setDefaultViewPartial('mediabrowser/my-pagination-control.phtml');
$this->view->paginator     = $paginator;
$this->view->images         = $images;
$this->view->JsonData     = $images;
}

En la vista (/application/default/views/private/mediabrowser/images-list.phtml):

<ul>
<?php
echo $this->paginationControl($this->paginator,'All','mediabrowser/my-pagination-control-1.phtml');
//echo $this->paginationControl($this->paginator,'Elastic','mediabrowser/my-pagination-control-2.phtml');
//echo $this->paginationControl($this->paginator,'Jumping','mediabrowser/my-pagination-control-3.phtml');
//echo $this->paginationControl($this->paginator,'Sliding','mediabrowser/my-pagination-control-4.phtml');
?>
<?php if($this->paginator){ ?>
<?php foreach($this->paginator as $key=> $image){?>
<li>
<?php
//echo Zend_Json::encode($image);
echo 'id: <strong>'.$image['id'].'</strong> ('.$image['width'].'x'.$image['height'].')';
echo '<a onclick="return hs.expand(this, { headingText: \'Original ('.$image['width'].'x'.$image['height'].') \' })" href="'.$image['path'].$image['name'].'">';
echo "<img title='{$image['name']}' src='/block/mediabrowser/image-zoom?f={$image['path']}{$image['name']}&w=120&h=90' />";
echo '</a>';
//echo $image['name'];
?>
</li>
<?php ob_flush(); ?>
<?php } ?>
<?php } ?>
</ul>

Vista para la navegacion de la paginación (/application/default/views/private/mediabrowser/my-pagination-control-1.phtml):

<!-- Ver: http://www.duckheads.co.uk/zend-paginator-example/265 --> <style> .paginationControl{padding:10px;font-family:arial;font-size:12px;color:#333;height:13px} .paginationControl_showing{float:left;position:relative;font-weight:bold} .paginationControl_showing strong.current{color:#a8050d} .paginationControl_pages{float:right;position:relative;font-weight:bold;color:#494848} .paginationControl_pages a{color:#494848} .paginationControl_pages strong{color:#a8050d} .paginationControl_pages .previous,.paginationControl_pages .next{color:#a8050d} </style> <?php if ($this->pageCount) : $midRange = floor(sizeof($this->pagesInRange) / 2); if (($this->current + $midRange) < $this->pageCount) : array_pop($this->pagesInRange); $display_end = true; endif; ?> <div> <div> Página <strong><?= $this->current; ?></strong> de <strong><?= $this->last; ?></strong> </div> <div> <!-- Previous page link --> <?php if (isset($this->previous)): ?> <a href="<?php echo $this->url(array('page' => $this->previous)) . $this->query; ?>">&lt; Anterior</a> | <?php else: ?> <span>&lt; Anterior</span> | <?php endif; ?> <?php if (($this->current - $midRange) > $this->first): ?> <?php array_shift($this->pagesInRange);?> <a href="<?php echo $this->url(array('page' => $this->first)) . $this->query; ?>" ><?php echo $this->first ?></a>... | <?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)) . $this->query; ?>" ><?= $page; ?></a> | <?php else: ?> <strong><?= $page; ?></strong> | <?php endif; ?> <?php endforeach; ?> <?php if (!empty($display_end)) : ?> ...<a href="<?php echo $this->url(array('page' => $this->last)) . $this->query; ?>" ><?php echo $this->last ?></a> | <?php endif; ?> <!-- Next page link --> <?php if (isset($this->next)): ?> <a href="<?php echo $this->url(array('page' => $this->next)) . $this->query; ?>">Siguiente &gt;</a> <?php else: ?> <span>Siguiente &gt;</span> <?php endif; ?> </div> </div> <?php endif; ?>

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s