Yii使用ZF的分页方式

来源:互联网 发布:8051单片机基本特点 编辑:程序博客网 时间:2024/04/28 16:25
使用类似ZF的方式分页,需要单独写一个分页类,重载run方法,从CPagination得到需要的分页数据,然后使用控制器的renderPartial方法,不能使用render方法这个方法会在视图的控制器目录下找文件,这样指定的位置就不准确了。

<?php/*** 解决使用rpc来的数据没有相应的数据提供者的问题* 这里不使用dataProvider,仅仅用分页器和模板来显示分页页面*/class SLinkPager extends CBasePager{     /**     * 分页器使用的视图文件     * @var string     */     public $itemView;     /**     * @var integer maximum number of page buttons that can be displayed. Defaults to 10.     */     public $maxButtonCount=10;          /**     * Initializes the pager by setting some default property values.     */     public function init()     {          if($this->itemView===null)               throw new CException(Yii::t('linkpager','The property "itemView" cannot be empty.'));     }     /**     * Executes the widget.     * This overrides the parent implementation by displaying the generated page buttons.     */     public function run()     {          $this->registerClientScript();          if(($pageCount=$this->getPageCount())<=1)               return;          $data = array();          $data['pageCount'] = $this->getPageCount();          list($data['beginPage'],$data['endPage'])=$this->getPageRange();          $data['pagesInRange'] = range($data['beginPage'], $data['endPage']);          $data['current']=$this->getCurrentPage(false);          if ($data['endPage'] > $data['current']) {               $data['next'] = $data['current']+1;          }          if ($data['current'] > $data['beginPage']) {               $data['prev'] = $data['current']-1;          }          $data['pager']=$this->getPages();          $owner=$this->getOwner();          $owner->renderPartial($this->itemView,$data, false);     }

类中重点是run这块,另外添加itemView属性,再看模板里面
<!--See h ttp://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination--><?php if ($pageCount): ?>< div class ="page" ><?php if ( isset($prev )): ?>< a href =" <?php echo $pager->createPageUrl($this, $prev); ?>" > 上一页< b ></b ></ a><?php endif ;?><?php foreach ($pagesInRange as $page): ?><?php if ($page != $current ): ?>    <a href= "<?php echo $pager->createPageUrl($this, $page); ?> "> <?php echo $page+1; ?> </ a><?php else : ?>    <a href= "#" class ="focus" > <?php echo $page+1; ?> </ a>  <?php endif ; ?><?php endforeach ; ?><?php if ( isset($next )): ?>< a href =" <?php echo $pager->createPageUrl($this, $next); ?>" > 下一页< b ></b ></ a><?php endif ;?></ div><?php endif ;?>

这些写就OK了,最后是widget渲染部分,
<?php $this->widget( 'SLinkPager', array ('pages' => $pager, 'itemView' => 'widget/pagination')); ?>

这里的$pager是CPagination,个人认为ZF的这种方式可以不管分页结构和样式,直接把模板拿来套用就行。
提醒,这个renderPartial方法我重载了的,自己写的时候看看文档的实现,可能和我的不一样。
一句抱怨,为什么yii的分页的页码是从0开始的呢,这个到处都有修改,如果忘记了页面就错了,不是没事找事吗。

资料:
http://www.yiiframework.com/
http://www.yiichina.com/forum/thread-143-1-1.html