2010-02-19 2 views
5

Amis,Pagination dans Zend

Je veux créer une pagination dans Zend Framework. Je suis nouveau à ZF.

index.phtml est donnée ci-dessous

<table> 
<tr> 
    <th>Name</th> 
    <th>Quantity</th> 
    <th>&nbsp;</th> 
</tr> 
<?php foreach($this->orders as $order) : ?> 
<tr> 
    <td><?php echo $this->escape($order->name);?></td> 
    <td><?php echo $this->escape($order->quantity);?></td> 
    <td> 
     <a href="<?php echo $this->url(array('controller'=>'index','action'=>'edit', 'id'=>$order->id));?>">Edit</a> 
     <a href="<?php echo $this->url(array('controller'=>'index', 'action'=>'delete', 'id'=>$order->id));?>">Delete</a> 
    </td> 
</tr> 
<?php endforeach; ?> 
</table> 
<p><a href="<?php echo $this->url(array('controller'=>'index','action'=>'add'));?>">Add new album</a></p> 

Mon contrôleur d'index est inférieur

class IndexController extends Zend_Controller_Action 
{ 
    public function init() 
    { 
     /* Initialize action controller here */ 
    } 

    public function indexAction() 
    { 
     $this->view->title = "My Orders"; 
     $this->view->headTitle($this->view->title, 'PREPEND'); 
     $orders = new Model_DbTable_Orders(); 
     $this->view->orders = $orders->fetchAll(); 
    } 
} 
+0

Je suis une réponse et j'ai essayé et obtenir seulement output.I partielles am créer un « pagination.phtml » et emmagasinés dans « vues/scripts/» et ajouter la ligne donnée dans mon index .phtml '$ this-> paginationControl ($ this-> paginator,' Sliding ',' pagination.phtml ') ;. Mais seulement les 10 premiers éléments sont affichés, n'affichent pas les liens suivants.Comment le résoudre ?? – Rakes

Répondre

17

J'essaie de vous aider sur ce que j'ai dans mon projet ZF. Ainsi, dans votre classe Model_DbTable_Order, vous pouvez définir une méthode appelée par ex. getOnePageOfOrderEntries() comme suit:

/** 
* Return one page of order entries 
* 
* @param int $page page number 
* @return Zend_Paginator Zend_Paginator 
*/ 
public function getOnePageOfOrderEntries($page=1) { 

    $query = $this->select(); 
    $paginator = new Zend_Paginator(
      new Zend_Paginator_Adapter_DbTableSelect($query) 
    ); 
    $paginator->setItemCountPerPage(100); 
    $paginator->setCurrentPageNumber($page); 
    return $paginator; 
} 

Que dans indexAction vous pouvez avoir quelque chose comme ceci:

public function indexAction() 
{ 
    $this->view->title = "My Orders"; 
    $this->view->headTitle($this->view->title, 'PREPEND'); 
    $orders = new Model_DbTable_Orders(); 
    //$this->view->orders = $orders->fetchAll(); 

    $page = $this->_request->getParam('page'); 
    if (empty($page)) { $page = 1; } 

    $paginator = $orders->getOnePageOfOrderEntries($page); 
    $this->view->paginator = $paginator; 

} 

Dans votre index.phtml vue que vous pourriez avoir quelque chose de similaire à ceci:

<?php if (count($this->paginator)): ?> 
    <table> 
    <tr> 
     <th>Name</th> 
     <th>Quantity</th> 
     <th>&nbsp;</th> 
    </tr> 
      <?php foreach($this->paginator as $order): ?> 

    <tr> 

     <td><?php echo $order->name;?></td> 
     <td><?php echo $order->quantity;?></td> 
     <td><!-- And the rest what you want --></td>   
    </tr> 

    <?php endforeach; ?> 
</table> 
<?php endif; ?> 
    <?php echo $this->paginationControl($this->paginator, 
    'Sliding','partial/my_pagination_control.phtml'); ?> 

Où my_pagination_control.phtml est comme suit (il s'agit simplement de copier-coller ce que j'ai et il provient du quator de référence ZF):

<?php if ($this->pageCount): ?> 
    <div class="paginationControl"> 
    <!-- Previous page link --> 
    <?php if (isset($this->previous)): ?> 
    <a href="<?php echo $this->url(array('page' => $this->previous)); ?>"> 
    Previous 
     </a> <span class="bar"> | </span> 
     <?php else: ?> 
     <span class="disabled"> Previous</span> <span class="bar"> | </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> <span class="bar"> | </span> 
    <?php else: ?> 
    <?php echo $page; ?> <span class="bar"> | </span> 
    <?php endif; ?> 
<?php endforeach; ?> 
<!-- Next page link --> 
<?php if (isset($this->next)): ?> 
    <a href="<?php echo $this->url(array('page' => $this->next)); ?>"> 
    Next 
    </a> 
<?php else: ?> 
    <span class="disabled">Next </span> 
<?php endif; ?> 
</div> 
<?php endif; ?> 

Espérons que ce sera utile.

+1

+1 pour utiliser 'new Zend_Paginator_Adapter_DbTableSelect()' à la place du tableau entier; et renvoyer le 'Zend_Paginator' du modèle. – chelmertz

+0

+1 pour le modèle retournant paginator :) –

+0

Je remarque son ajouter -/action/page/pageno supplémentaire à l'url. Ce qui rend mes images et css à ne pas correctement. Quelle serait la cause. Comment charger mes images et css? – shorif2000