当前位置:首页 > PHP教程 > PHP常见问题

yii使用bootstrap分页样式的实例

bootstrap是twitter推出的一个开源的用于前端开发的工具包。它由twitter的设计师mark otto和jacob thornton合作开发,是一个css/html框架。bootstrap提供了优雅的html和css规范,它即是由动态css语言less写成。bootstrap一经推出后颇受欢迎,一直是github上的热门开源项目,包括nasa的msnbc(微软全国广播公司)的breaking news都使用了该项目。

本文为大家介绍的是yii使用bootstrap分页样式方法,感兴趣的同学参考下。

yii自带了分页类和页面样式,但如果是yii+bootstrap开发的项目如何在不修改yii的情况下使用bootstrap分页样式呢。

本文就为大家介绍一种非常简单的方式,想在yii的分页中套用bootstrap样式主要依赖yii clinkpager中的二个属性htmloptions和selectedpagecssclass

控制器示例代码

public function actionindex() { $cid = intval($_get['cid']); $criteria = new cdbcriteria(); $criteria->addcondition("t.status=1"); $criteria->addcondition("cid='$cid'"); $criteria->order="t.time desc"; $count = article::model()->count($criteria); $pager = new cpagination($count); $pager->pagesize=20; $pager->applylimit($criteria); $lists = article::model()->findall($criteria); $this->render('index',array('lists'=>$lists,"pager"=>$pager)); }

上面 代码实现了yii分页,并把$pager分页对象传到视图里,我们再来看一下视图代码

视图代码

<nav> <?php $this->widget('clinkpager',array( 'header'=>'', 'firstpagelabel' => '首页', 'lastpagelabel' => '末页', 'prevpagelabel' => '上一页', 'nextpagelabel' => '下一页', 'pages' => $pager, 'maxbuttoncount'=>8, 'cssfile'=>false, 'htmloptions' =>array("class"=>"pagination"), 'selectedpagecssclass'=>"active" ) ); ?> </nav>

上面的视图代码要注意以下几个点

1.分页必须在<nav>里

2,htmloptions选项是必须的,他指定了yii生成的分页div的class名称,在这里我们使用bootstrap的class名

3,selectedpagecssclass选项指定了当前选中页的样多,在这里我们使用bootstrap的active

4.另外还需要把cssfile设为false,不加载分页css样式文件

参考bootstrap官网提供的分页代码,如下图

 

最终实现的效果图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!