YII CLinkPager分页类扩展增加如何显示共多少页

发布时间:2021-09-29 11:38:34 作者:iii
来源:亿速云 阅读:134

本篇内容介绍了“YII CLinkPager分页类扩展增加如何显示共多少页”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1、默认的CLinkPager显示的效果

上面这里写了css的样式哈,我们来看pager代码:

<div class="page-link">
<?php $this->widget('CLinkPager',array(
'header' => '',
'firstPageLabel' => '首页',
'lastPageLabel' => '尾页',
'prevPageLabel' => '&lt;',
'nextPageLabel' => '&gt;',
'pages' => $pages,
'maxButtonCount'=>5,
'htmlOptions' => array('class' => 'page-link'), //分页要使用的css样式
));?>
</div>

2、我们来看想要的分页类效果

也就是说后面增加显示了共多少页,这个怎么做到的喃?这里我稍微小小的扩展了一下widget组件CLinkPager,看上去也是非常的狠狠简单呐,废话不多少,来来先看代码:

<?php
/**
* 分页组建ClinkPager扩展
* @description page-tab-tog为分页的样式class
* @author <[<xm 杭州>]>
* @time 2016-01-29
* @example
* <div class="page-tab-tog">
* <?php $this->widget('MLinkPager',array(
* 'header' => '',
* 'firstPageLabel' => '首页',
* 'lastPageLabel' => '尾页',
* 'prevPageLabel' => '&lt;',
* 'nextPageLabel' => '&gt;',
* 'pages' => $pages,
* 'maxButtonCount'=>5,
* 'htmlOptions' => array('class' => 'page-tab-tog'),
* ));?>
* </div>
*/
class MLinkPager extends CLinkPager
{
//设置为true的时候,显示共X页,$this->forceTotalPage值优先该值
public $mCountPage = false;
//是否强制显示共x页,设置为true时,$this->mCountPage和$this->getPageRange()无效
public $forceTotalPage = false;
public function init()
{
}
public function run()
{
$this->registerClientScript();
$buttons=$this->createPageButtons();
list($beginPage,$endPage)=$this->getPageRange();
if ($this->forceTotalPage)
{
$buttons[] = CHtml::tag('li', array('class'=>'totle'),'共'.$this->getPageCount().'页');
}
else
{
if ($this->mCountPage && $endPage > 0)
{
$buttons[] = CHtml::tag('li', array('class'=>'totle'),'共'.$this->getPageCount().'页');
}
}
if(empty($buttons))
return;
echo $this->header;
echo CHtml::tag('div',$this->htmlOptions,implode("\n",$buttons));
echo $this->footer;
}
}

有人说了,一看那么一堆代码,头疼,你这玩意怎么能以最快的速度见到效果呢?来来我们继续看怎么使用,首先呢,你需要先把上面的扩展MLinkPager原封不动的拷贝到本地的components目录下的MlinkPager文件里,什么,你没有这个文件,自己创建,^~^!好了以后咱们来看下view里面是怎么使用的,那是简单的不能再过于简单了。

<div class="page-tab-tog">
<?php $this->widget('MLinkPager',array(
'header' => '',
'firstPageLabel' => '首页',
'lastPageLabel' => '尾页',
'prevPageLabel' => '&lt;',
'nextPageLabel' => '&gt;',
'pages' => $pages,
'maxButtonCount'=>5,
'mCountPage' => true, //!!!注意看这里,加一行代码就ok了
'htmlOptions' => array('class' => 'page-tab-tog'),
));?>
</div>

什么?你刚睡醒眼神不好,没看出来区别?注意看MLinkPager的配置项mCountPage,这个设置为true就万事大吉了!

特别说明:如果你的列表没有数据的话,分页是不显示页码的,但是如果有刁蛮产品要的需求是没有列表数据,但但但你必须得吧共0页显示出来,我们的MlinkPager只需要设置下配置项forceTotalPage为true即可,此时设置mCountPager无效了咯,具体详细请看MlinkPage类,次类可自己再进行扩展

下面给大家介绍在在yii中使用分页

yii中使用分页很方便,如下两种方法:

在控制器中:

1、

$criteria = new CDbCriteria(); //new cdbcriteria数据库<br>$criteria->id = 'id ASC'; //排序规则
$count = Exchange::model()->count($criteria);
$pager = new CPagination($count);
$pager->pageSize=30;
$pager->applyLimit($criteria);
$categoryInfo = Category::model()->findAll($criteria); //根据条件查询

  2、

$criteria = new CDbCriteria();
$criteria->order = 'id ASC';
$criteria->addCondition('status=1'); //根据条件查询
$criteria->addCondition('exchange_status=0');
$count = Exchange::model()->count($criteria);
$pager = new CPagination($count);
$pager->pageSize=30;
$pager->applyLimit($criteria); 
$exchangeInfo = Exchange::model()->findAll($criteria);

 render中传入参数:

array("pages" => $pager)

 视图中加入:

$this->widget('CLinkPager',array(
'header'=>'',
'firstPageLabel' => '首页',
'lastPageLabel' => '末页',
'prevPageLabel' => '上一页',
'nextPageLabel' => '下一页',
'pages' => $pages,
'maxButtonCount'=>8,
)
);

  分页思想:

1、计算数据库中总的条数

2、分页大小

3、设置偏移量limit

在Yii中,分页时会用这个类CDBcritria进行数据库查询很重要,这样分页很简单。

“YII CLinkPager分页类扩展增加如何显示共多少页”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. Yii 中使用分页
  2. 利用yii框架怎么实现分页

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

yii clinkpager

上一篇:.net开发微信公众平台实例教程

下一篇:如何面向文档的数据库系统MongoDB

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》