【问题标题】:Pagination in ZendZend 中的分页
【发布时间】:2010-02-19 06:12:23
【问题描述】:

朋友们,

我想在 Zend Framework 中创建分页。我是采埃孚的新手。

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

【问题讨论】:

  • 我收到了一条回复,但我尝试只获得部分输出。我创建了一个“pagination.phtml”并将其存储在“views/scripts/”中,并将给定的行添加到我的 index.phtml '$this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml');.但是只显示前10个项目,不显示下一个-上一个链接。如何解决??

标签: zend-framework


【解决方案1】:

我会根据我在 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 参考手册):

   <?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 用于使用new Zend_Paginator_Adapter_DbTableSelect() 而不是整个数组;并从模型返回Zend_Paginator
  • 我注意到它在网址中添加了额外的 - /action/page/pageno。这使我的图像和 css 不正确。会是什么原因。如何加载我的图片和 CSS?
  • @sharif 使用绝对路径会有所帮助
【解决方案2】:
猜你喜欢
  • 2013-06-02
  • 2012-02-06
  • 2011-03-03
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多