Zend 中的分页 [英] Pagination in Zend
问题描述
朋友们,
我想在 Zend Framework 中创建分页.我是采埃孚的新手.
index.phtml 如下
<tr><th>姓名</th><th>数量</th><th> </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));?>">编辑<a href="<?php echo $this->url(array('controller'=>'index', 'action'=>'delete', 'id'=>$order->;id));?>">删除</a></td></tr><?php endforeach;?>
<p><a href="<?php echo $this->url(array('controller'=>'index','action'=>'add'));?>">添加新专辑</a></p>
我的索引控制器在下面
class IndexController 扩展 Zend_Controller_Action{公共函数 init(){/* 在这里初始化动作控制器 */}公共函数 indexAction(){$this->view->title = "我的订单";$this->view->headTitle($this->view->title, 'PREPEND');$orders = new Model_DbTable_Orders();$this->view->orders = $orders->fetchAll();}}
我尝试根据我在 ZF 项目中的内容为您提供帮助.
因此,在您的类 Model_DbTable_Order 中,您可以定义一个方法,例如getOnePageOfOrderEntries() 如下:
/*** 返回一页订单条目** @param int $page 页码* @return Zend_Paginator Zend_Paginator*/公共函数 getOnePageOfOrderEntries($page=1) {$query = $this->select();$paginator = new Zend_Paginator(新 Zend_Paginator_Adapter_DbTableSelect($query));$paginator->setItemCountPerPage(100);$paginator->setCurrentPageNumber($page);返回 $paginator;}
比在 indexAction 中你可以有这样的东西:
公共函数 indexAction(){$this->view->title = "我的订单";$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 视图中,您可能有类似的内容:
paginator)): ?><表格><tr><th>姓名</th><th>数量</th><th> </th></tr><?php foreach($this->paginator as $order): ?><tr><td><?php echo $order->name;?></td><td><?php echo $order->quantity;?></td><td><!-- 剩下的你想要什么--></td></tr><?php endforeach;?><?php endif;?><?php echo $this->paginationControl($this->paginator,'滑动','partial/my_pagination_control.phtml');?>
其中 my_pagination_control.phtml 如下(这只是复制粘贴我所拥有的,它来自 ZF 参考资料):
pageCount): ?><div class="paginationControl"><!-- 上一页链接--><?php if (isset($this->previous)): ?><a href="<?php echo $this->url(array('page' => $this->previous)); ?>">以前的</a><span class="bar">|</span><?php 其他:?><span class="disabled">上一个<span class="bar">|</span><?php endif;?><!-- 编号页面链接--><?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 其他:?><?php echo $page;?><span class="bar">|</span><?php endif;?><?php endforeach;?><!-- 下一页链接--><?php if (isset($this->next)): ?><a href="<?php echo $this->url(array('page' => $this->next)); ?>">下一个</a><?php 其他:?><span class="disabled">下一步 </span><?php endif;?>
<?php endif;?>
希望有用.
Friends,
I want to create pagination in Zend Framework. I'm new to ZF.
index.phtml is given below
<table>
<tr>
<th>Name</th>
<th>Quantity</th>
<th> </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>
My index controller is below
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();
}
}
I try to help you based on what I have in my ZF project.
So, in your class Model_DbTable_Order you can defined a method called e.g. getOnePageOfOrderEntries() as follows:
/**
* 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;
}
Than in indexAction you can have something like this:
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;
}
In your index.phtml view you could have something similar to this:
<?php if (count($this->paginator)): ?>
<table>
<tr>
<th>Name</th>
<th>Quantity</th>
<th> </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'); ?>
Where my_pagination_control.phtml is as follows (this is just copy-paste what I have and it is from the 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; ?>
Hope it will be useful.
这篇关于Zend 中的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!