Zend中的分页

时间:2010-02-19 06:12:24

标签: zend-framework

朋友,

我想在Zend Framework中创建分页。我是ZF的新手。

index.phtml如下所示

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

我的索引控制器低于

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

2 个答案:

答案 0 :(得分:17)

我试着根据我在ZF项目中的内容来帮助你。

因此,在您的类Model_DbTable_Order中,您可以定义一个名为getOnePageOfOrderEntries()如下:

 /**
 * 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;
}

比在indexAction中你可以这样:

 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;

}

在index.phtml视图中,你可以有类似的东西:

  <?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'); ?>

my_pagination_control.phtml如下所示(这只是复制粘贴我所拥有的,它来自ZF Reference quide):

   <?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; ?>

希望它会有用。

答案 1 :(得分:1)