新聞中心
本篇文章給大家分享的是有關(guān)如何使用Yii框架實(shí)現(xiàn)分頁(yè),小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
首頁(yè),在models目錄里創(chuàng)建個(gè)Auth.php的模型文件,里面寫入
class Auth extends CActiveRecord { public static function model($className = __CLASS__) { return parent::model($className); } public function tableName() { return '{{auth}}'; } }
接著在controllers目錄里創(chuàng)建IndexController.php的控制文件,里面寫入
class IndexController extends Controller { public function actionList() { $criteria = new CDbCriteria(); $criteria->order = 'a_id desc'; $count = Auth::model()->count($criteria); $page = new CPagination($count); $page->pageSize = 10; $page->applyLimit($criteria); $auth = Auth::model()->findAll($criteria); $this->renderPartial('auth', array('page' => $page, 'list' => $auth)); } public function actionList1() { $p = isset($_GET['page']) ? $_GET['page'] : 0; $criteria = new CDbCriteria(); $criteria->select = "a_id,a_nickname"; $criteria->condition=''; $criteria->limit = 10; $criteria->offset = $p == 0 ? 0 : (($p-1)*10); $criteria->order = 'a_id desc'; $auth = Auth::model()->findAll($criteria); $count = Auth::model()->count($criteria); $page = new CPagination($count); $page->pageSize = 10; $page->applyLimit($criteria); $this->renderPartial('auth', array('page' => $page, 'list' => $auth)); } }
其中actionList和actionList1是$criteria的兩種寫法
最后在views目錄里添加index目錄,并在index目錄內(nèi)添加auth.php文件,里面寫入
widget('CLinkPager',array( 'firstPageLabel'=>'首頁(yè)', 'lastPageLabel'=>'末頁(yè)', 'prevPageLabel'=>'上一頁(yè)', 'nextPageLabel'=>'下一頁(yè)', 'pages'=>$page, 'maxButtonCount'=>13, ) ); ?>
上面是yii自帶的寫法,這里引入tp的分頁(yè)類,做個(gè)簡(jiǎn)單的改動(dòng),步驟如下
首先,把tp的AjaxPage.class.php和Page.class.php移動(dòng)到y(tǒng)ii的項(xiàng)目目錄下的 protected/components下面,并且把文件名稱分布改為AjaxPage.php和Page.php,分別進(jìn)入兩個(gè)文件,把里面的C方法去掉,也就是這一句
$this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
改為
$this->varPage = 'p' ;
改完之后,這個(gè)兩個(gè)文件是不需要引入的,因?yàn)閥ii在啟動(dòng)時(shí)會(huì)自動(dòng)加載的。具體的可見protected/config.php/main.php的配置中的
// autoloading model and component classes 'import'=>array( 'application.models.*', 'application.components.*', ),
其次,在protected/config.php/目錄里新建一個(gè)common.php文件,這個(gè)文件就放些項(xiàng)目的公共函數(shù),熟悉tp的朋友應(yīng)該知道tp也有公共函數(shù)文件,很好用,這里借鑒下,yii應(yīng)該也有吧,目前還沒發(fā)現(xiàn)。在該文件寫入
// 根據(jù)頁(yè)碼獲取列表 function getListByPage($model, $select = '*', $condition = '', $limit = 10, $order = '', $p = '', $ajax = 0) { // 初始化參數(shù) $_GET['p'] = isset($_GET['p']) ? intval($_GET['p']) : 1; $limit = intval($limit) > 0 ? intval($limit) : 10; if ($p) { $_GET['p'] = intval($p) ? intval($p) : 1; } $criteria = new CDbCriteria(); $count = $model->count($criteria); if ($ajax) { $Page = new AjaxPage($count, $limit); } else { $Page = new Page($count, $limit); } $result['page'] = trim($Page->show()); $criteria->select = $select; $criteria->condition = $condition; $criteria->limit = $Page->listRows; $criteria->offset = $Page->firstRow; $criteria->order = $order; $list = $model->findAll($criteria); $result['list'] = $list; return $result; }
這個(gè)文件可就要引入了,不然加載不了,可以在項(xiàng)目的入口文件index.php里自行引入,代碼如下
require_once(dirname($config) . '/common.php');
最后在indexController.php中用到分頁(yè)的地方調(diào)用該方法
public function actionPage() { $model = Auth::model(); $info = getListByPage($model); $this->renderPartial('page', array('info' => $info)); }
封裝了此方法,以后調(diào)用分頁(yè)時(shí),只需傳幾個(gè)參數(shù),簡(jiǎn)單又快捷。在page.php頁(yè)面上調(diào)用
同時(shí)利用findAll也可以實(shí)現(xiàn)分頁(yè)的查詢效果,代碼如下
function getListByPage($model, $select = '*', $condition = '', $limit = 10, $order = '', $p = '', $ajax = 0) { if (!$model) { return array();; } // 初始化參數(shù) $_GET['p'] = isset($_GET['p']) ? intval($_GET['p']) : 1; $limit = intval($limit) > 0 ? intval($limit) : 10; if ($p) { $_GET['p'] = intval($p) ? intval($p) : 1; } $count = $model->count(); if ($ajax) { $Page = new AjaxPage($count, $limit); } else { $Page = new Page($count, $limit); } $result['page'] = trim($Page->show()); $result['list'] = $model->findAll(array( 'select' => $select, 'condition' => $condition, 'order' => $order, 'limit' => $Page->listRows, 'offset' => $Page->firstRow, )); return $result; }
以上就是如何使用Yii框架實(shí)現(xiàn)分頁(yè),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:如何使用Yii框架實(shí)現(xiàn)分頁(yè)-創(chuàng)新互聯(lián)
URL分享:http://www.ef60e0e.cn/article/gisjj.html