zend-framework - Zend 中的分页

标签 zend-framework

friend 们,

我想在 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>

我的索引 Controller 在下面
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();
    }
}

最佳答案

我尝试根据我在 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 View 中,您可能有类似的内容:
  <?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; ?>

希望它会很有用。

关于zend-framework - Zend 中的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2294429/

相关文章:

php - Zend 从同一目录自动加载不同的命名空间?

php - 加入后选择同名的列

php - ZF2 如何或在哪里()

mysql - 如何在 MySQL for Mac 上设置默认端口

php - zend 表单检查记录在数据库中不存在

zend-framework - 在 Zend Framework 中找不到类

php - Zend_Session : Session must be started before any output has been sent to the browser

zend-framework - 禅德数据库 : joinLeft problem

javascript - 如何将外部 javascript 文件添加到 Zend Framework 2 应用程序?

zend-framework - Zend Framework 表单元素验证器 - 即使不需要也验证字段