Yii使用CLinkPager分页实例详解,yiiclinkpager_PHP教程
Yii使用CLinkPager分页实例详解,yiiclinkpager
本文主要讲解了YII中使用CLinkPager分页的方法,这里我们采用物件的形式来定义分页:
首先在components中自定义LinkPager,并继承CLinkPager
具体代码如下:
<?php /** * CLinkPager class file. * * @link http://www.yiiframework.com/ * @copyright Copyright © 2008-2011 Yii Software LLC * @license http://www.yiiframework.com/license/ */ /** * CLinkPager displays a list of hyperlinks that lead to different pages of target. * * @version $Id$ * @package system.web.widgets.pagers * @since 1.0 */ class LinkPager extends CLinkPager { const CSS_TOTAL_PAGE='total_page'; const CSS_TOTAL_ROW='total_row'; /** * @var string the text label for the first page button. Defaults to '<< First'. */ public $totalPageLabel; /** * @var string the text label for the last page button. Defaults to 'Last >>'. */ public $totalRowLabel; /** * Creates the page buttons. * @return array a list of page buttons (in HTML code). */ protected function createPageButtons() { $this->maxButtonCount=8; $this->firstPageLabel="首页"; $this->lastPageLabel='末页'; $this->nextPageLabel='下一页'; $this->prevPageLabel='上一页'; $this->header=""; if(($pageCount=$this->getPageCount())<=1) return array(); list($beginPage,$endPage)=$this->getPageRange(); $currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange() $buttons=array(); // first page $buttons[]=$this->createPageButton($this->firstPageLabel,0,self::CSS_FIRST_PAGE,$currentPage<=0,false); // prev page if(($page=$currentPage-1)<0) $page=0; $buttons[]=$this->createPageButton($this->prevPageLabel,$page,self::CSS_PREVIOUS_PAGE,$currentPage<=0,false); // internal pages for($i=$beginPage;$i<=$endPage;++$i) $buttons[]=$this->createPageButton($i+1,$i,self::CSS_INTERNAL_PAGE,false,$i==$currentPage); // next page if(($page=$currentPage+1)>=$pageCount-1) $page=$pageCount-1; $buttons[]=$this->createPageButton($this->nextPageLabel,$page,self::CSS_NEXT_PAGE,$currentPage>=$pageCount-1,false); // last page $buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,self::CSS_LAST_PAGE,$currentPage>=$pageCount-1,false); // 页数统计 $buttons[]=$this->createTotalButton(($currentPage+1)."/{$pageCount}",self::CSS_TOTAL_PAGE,false,false); // 条数统计 $buttons[]=$this->createTotalButton("共{$this->getItemCount()}条",self::CSS_TOTAL_ROW,false,false); return $buttons; } protected function createTotalButton($label,$class,$hidden,$selected) { if($hidden || $selected) $class.=' '.($hidden ? self::CSS_HIDDEN_PAGE : self::CSS_SELECTED_PAGE); return '<li class="'.$class.'">'.CHtml::label($label,false).'</li>'; } /** * Registers the needed client scripts (mainly CSS file). */ public function registerClientScript() { if($this->cssFile!==false) self::registerCssFile($this->cssFile); } /** * Registers the needed CSS file. * @param string $url the CSS URL. If null, a default CSS URL will be used. */ public static function registerCssFile($url=null) { if($url===null) $url=CHtml::asset(Yii::getPathOfAlias('application.components.views.LinkPager.pager').'.css'); Yii::app()->getClientScript()->registerCssFile($url); } }
定义CSS样式
/** * 翻页样式 */ .page_blue{ margin: 3px; padding: 3px; text-align: center; font: 12px verdana, arial, helvetica, sans-serif; } ul.bluePager,ul.yiiPager { font-size:11px; border:0; margin:0; padding:0; line-height:100%; display:inline; text-aligin:center; } ul.bluePager li,ul.yiiPager li { display:inline; } ul.bluePager a:link,ul.yiiPager a:link, ul.bluePager a:visited,ul.yiiPager a:visited, ul.bluePager .total_page label,ul.yiiPager .total_page label, ul.bluePager .total_row label,ul.yiiPager .total_row label { border: #ddd 1px solid; color: #888888 !important; padding:2px 5px; text-decoration:none; } ul.bluePager .page a,ul.yiiPager .page a { font-weight:normal; } ul.bluePager a:hover,ul.yiiPager a:hover { color:#FFF !important; border:#156a9a 1px solid; background-color:#2b78a3 } ul.bluePager .selected a,ul.yiiPager bluePager .selected a { color:#3aa1d0 !important; border: 1px solid #3aa1d0; } ul.bluePager .selected a:hover,ul.yiiPager .selected a:hover { color:#FFF !important; } ul.bluePager .hidden a,ul.yiiPager .hidden a { border:solid 1px #DEDEDE; color:#888888; } ul.bluePager .hidden,ul.yiiPager .hidden { display:none; }
controller中操作:
//分页操作 $criteria=new CDbCriteria; $criteria->order='id DESC'; $criteria->select=array('id','uid','username','title','thumb','url','clicks','time','dateline','countfavorite','quality'); $criteria->condition=$sql; $total = Video::model()->count($criteria); $pages = new CPagination($total); $pages->pageSize=self::PAGE_SIZE; $pages->applyLimit($criteria); $list = Video::model()->findAll($criteria); $title = CommonClass::model()->find(array( 'select'=>array('cname'), 'condition'=>'id = '.$id, )); $this->render('application.views.video.list',array( 'array'=>$array, 'arr'=>$arr, 'result'=>$result, 'list'=>$list, 'pages'=>$pages, 'title'=>$title, ));
在views/video/list.php中引用:
<?php $this->widget('LinkPager', array('pages' => $pages,)); ?>
一样的啊··框架只是加快开发效率而已···你哪一步卡住了··具体说···我把代码贴上来·你也学不了什么·很快就忘记了··自己一步步解决·才记得牢··
sql用limit和offset。分页用$pages = new CPagination ();

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP開發:如何實現表格資料排序和分頁功能在進行Web開發中,處理大量資料是一項常見的任務。對於需要展示大量資料的表格,通常需要實現資料排序和分頁功能,以提供良好的使用者體驗和最佳化系統效能。本文將介紹如何使用PHP實作表格資料的排序和分頁功能,並給出具體的程式碼範例。排序功能實作在表格中實作排序功能,可以讓使用者根據不同的欄位進行升序或降序排序。以下是一個實作表格

隨著數據的不斷增長,表格顯示變得更加困難。大多數情況下,表格中的資料量過大,導致表格在載入時變得緩慢,而且使用者需要不斷地瀏覽頁面才能找到自己想要的資料。本文將介紹如何使用JavaScript實作表格資料的分頁顯示,讓使用者更容易找到自己想要的資料。一、動態建立表格為了讓分頁功能更可控,需要動態建立表格。在HTML頁面中,新增一個類似下面的table元素。

如何使用JavaScript實作表格分頁功能?隨著網路的發展,越來越多的網站都會使用表格來展示數據。在某些資料量較大的情況下,需要將資料進行分頁展示,以提升使用者體驗。本文將介紹如何使用JavaScript實作表格分頁功能,並提供具體的程式碼範例。一、HTML結構首先,我們需要準備一個HTML結構來承載表格和分頁按鈕。我們可以使用<tab

MyBatis是一個優秀的持久層框架,它支援基於XML和註解的方式操作資料庫,簡單易用,同時也提供了豐富的插件機制。其中,分頁插件是使用頻率較高的插件之一。本文將深入探討MyBatis分頁外掛的原理,並結合具體的程式碼範例進行說明。一、分頁外掛原理MyBatis本身並沒有提供原生的分頁功能,但可以藉助外掛程式來實現分頁查詢。分頁插件的原理主要是透過攔截MyBatis

如何利用Layui開發一個具有分頁功能的資料展示頁面Layui是一個輕量級的前端UI框架,提供了簡潔美觀的介面元件和豐富的互動體驗。在開發中,我們經常會遇到需要展示大量資料並進行分頁的情況。以下是利用Layui開發的具有分頁功能的資料展示頁面的範例。首先,我們需要引入Layui的相關文件和依賴。在html頁面的<head>標籤中加入以下代

Vue元件實戰:分頁元件開發介紹在網路應用程式中,分頁功能是不可或缺的一個元件。一個好的分頁元件應該展示簡潔明了,功能豐富,而且易於整合和使用。在本文中,我們將介紹如何使用Vue.js框架來開發一個高度可自訂化的分頁元件。我們將透過程式碼範例來詳細說明如何使用Vue元件開發。技術堆疊Vue.js2.xJavaScript(ES6)HTML5和CSS3開發環

Vue是一種流行的JavaScript框架,用於建立使用者介面。在Vue技術開發中,實現分頁功能是常見的需求。本文將介紹如何使用Vue來實現分頁功能,並提供具體程式碼範例。在開始之前,我們需要提前準備一些基本知識。首先,我們需要了解Vue的基本概念和文法。其次,我們需要知道如何使用Vue元件來建立我們的應用程式。在開始之前,我們需要在Vue專案中安裝一個分頁插件,

在當前資訊時代,大數據、人工智慧、雲端運算等技術已成為了各大企業關注的熱點。在這些技術中,顯示卡渲染技術作為一種高效能圖形處理技術,受到了越來越多的關注。顯示卡渲染技術廣泛應用於遊戲開發、影視特效、工程建模等領域。而對於開發者來說,選擇一個適合自己專案的框架,是一個非常重要的決策。在目前的語言中,PHP是一種相當有活力的語言,一些優秀的PHP框架如Yii2、Ph
