這篇文章主要介紹“Yii中CGridView關(guān)聯(lián)表搜索的排序方法”,在日常操作中,相信很多人在Yii中CGridView關(guān)聯(lián)表搜索的排序方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Yii中CGridView關(guān)聯(lián)表搜索的排序方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
首先,檢查你的blog demo里的protectedmodelsComment.php,確保Comment模型有一個(gè)search的方法,如果沒有,就用gii生成一個(gè),我下載到的blog demo里倒是沒有。
然后,寫代碼的時(shí)間到了,我們從 CommentController 開始,我們給它加一個(gè) actionList:
復(fù)制代碼 代碼如下:
public function actionList()
{
$model=new Comment('search');
$model->unsetAttributes();
if(isset($_GET['Comment']))
$model->attributes=$_GET['Comment'];
$this->render('list',array(
'model'=>$model,
));
}
著看起來沒什么了不起的,跟你用gii生成的crud代碼里的一樣。現(xiàn)在讓我來創(chuàng)建view,在 /protected/views/comment/ 目錄下創(chuàng)建list.php然后粘貼以下代碼
復(fù)制代碼 代碼如下:
<?php $this->breadcrumbs=array(
'Comments',
);
?>
<h2>Manage Comments</h2>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns' => array(
'content',
'post.title',
'status',
'author'
),
));
?>
Comment List
這是一個(gè)基本的 CGridView 只顯示評論的‘content', ‘status' and ‘a(chǎn)uthor', 和文章的標(biāo)題。我們假設(shè)想要往這張list里添加一列文章的標(biāo)題,我們只需要添加post.title 就行了:
復(fù)制代碼 代碼如下:
'columns'=>array(
'content',
'post.title',
'status',
'author',
),
現(xiàn)在如果你訪問以下這個(gè)頁面,發(fā)現(xiàn)文章的標(biāo)題的確顯示出來了
問題:
如果你仔細(xì)瞅瞅這個(gè)頁面你會發(fā)現(xiàn)你無法搜索文章標(biāo)題,你也沒辦法按文章標(biāo)題排序,這是因?yàn)?CGridView 在給定的 column name 里面發(fā)現(xiàn)了一個(gè)‘.',也就是 post.title 的點(diǎn)。如果有點(diǎn)號的話,它就不會生成搜索框。
解決方案:
要想解決這個(gè)問題,我們得費(fèi)點(diǎn)力氣。首先我們得給Commen模型添加一個(gè) getter 和一個(gè) setter ,比如說這么寫:
復(fù)制代碼 代碼如下:
private $_postTitle = null;
public function getPostTitle()
{
if ($this->_postTitle === null && $this->post !== null)
{
$this->_postTitle = $this->post->title;
}
return $this->_postTitle;
}
public function setPostTitle($value)
{
$this->_postTitle = $value;
}
接下來將這個(gè)屬性添加到 rules 函數(shù)里:
復(fù)制代碼 代碼如下:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('content, author, email', 'required'),
array('author, email, url', 'length', 'max'=>128),
array('email','email'),
array('url','url')
array('content, postTitle, status, author', 'safe', 'on'=>'search'),
);
}
這還不夠,最需要改動(dòng)的是我們的 search 函數(shù)。首先我們要添一個(gè) criteria:
復(fù)制代碼 代碼如下:
$criteria=new CDbCriteria;
$criteria->with = "post"; // 確保查詢 post 表
$criteria->compare('t.content',$this->content,true);
$criteria->compare('t.status',$this->status);
$criteria->compare('t.author',$this->author,true);
$criteria->compare('post.title', $this->postTitle,true);
然后我們添加排序:
復(fù)制代碼 代碼如下:
$sort = new CSort();
$sort->attributes = array(
'defaultOrder'=>'t.create_time DESC',
'content'=>array(
'asc'=>'t.content',
'desc'=>'t.content desc',
),
'status'=>array(
'asc'=>'t.status',
'desc'=>'t.status desc',
),
'author'=>array(
'asc'=>'t.author',
'desc'=>'t.author desc',
),
'postTitle'=>array(
'asc'=>'post.title',
'desc'=>'post.title desc',
),
);
你也許注意到了我在使用完整的 ‘tablename'.'columnname'語法,我這么做的原因是為了避免 mysql 拋出‘column is ambigious error'。
為了保證這一切正常運(yùn)行,我們必須傳遞 CSort 實(shí)例和 CDbCriteria 實(shí)例給 CActiveDataProvider :
復(fù)制代碼 代碼如下:
return new CActiveDataProvider('Comment', array(
'criteria'=>$criteria,
'sort'=>$sort
));
return new CActiveDataProvider('Comment', array(
'criteria'=>$criteria,
'sort'=>$sort
));
現(xiàn)在我們要做的就是修改我們的 view 以便它在 CGridView 顯示想要顯示的屬性:
復(fù)制代碼 代碼如下:
'columns'=>array(
'content',
'postTitle',
'status',
'author',
),
刷新一下,應(yīng)該可以了,效果如下圖所示:
到此,關(guān)于“Yii中CGridView關(guān)聯(lián)表搜索的排序方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
分享題目:Yii中CGridView關(guān)聯(lián)表搜索的排序方法-創(chuàng)新互聯(lián)
分享路徑:http://m.rwnh.cn/article12/djiedc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、Google、全網(wǎng)營銷推廣、手機(jī)網(wǎng)站建設(shè)、面包屑導(dǎo)航、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容