当前位置: 移动技术网 > IT编程>开发语言>PHP > YII CLinkPager分页类扩展增加显示共多少页

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

yii的分页类clinkpager默认是不支持显示共x页的,那么现在需求来了,要在分页的后面显示共多少页,怎么办喃?我们来看解决办法

1、默认的clinkpager显示的效果

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

<div class="page-link">
<?php $this->widget('clinkpager',array(
'header' => '',
'firstpagelabel' => '首页',
'lastpagelabel' => '尾页',
'prevpagelabel' => '<',
'nextpagelabel' => '>',
'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' => '<',
* 'nextpagelabel' => '>',
* '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' => '<',
'nextpagelabel' => '>',
'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进行数据库查询很重要,这样分页很简单。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网