Yii2-GridView 中讓關(guān)聯(lián)字段帶搜索和排序功能示例
情境要求:
要在訂單(Order)視圖的gridview中顯示出客戶(Customer)姓名,并使其具有與其它字段相同的排序和搜索功能。
數(shù)據(jù)庫(kù)結(jié)構(gòu)
訂單表order含有字段customer_id 與 客戶表customer的id字段關(guān)聯(lián)
首先確保在Order Model中包含以下代碼:
public function getCustomer()
{
return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
}
用gii會(huì)自動(dòng)生成此代碼;
第一步:
在OrderSearch添加一個(gè)$customer_name變量
class OrderSearch extends Order
{
public $customer_name; //<=====就是加在這里
}
第二步:
修改OrderSearch中的search函數(shù)
public function search($params)
{
$query = Order::find();
$query->joinWith(['customer']);<=====加入這句
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes' => [
/* 其它字段不要?jiǎng)?*/
/* 下面這段是加入的 */
/*=============*/
'customer_name' => [
'asc' => ['customer.customer_name' => SORT_ASC],
'desc' => ['customer.customer_name' => SORT_DESC],
'label' => 'Customer Name'
],
/*=============*/
]
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'user_id' => $this->user_id,
'customer_id' => $this->customer_id,
'order_time' => $this->order_time,
'pay_time' => $this->pay_time,
]);
$query->andFilterWhere(['like', 'status', $this->status]);
$query->andFilterWhere(['like', 'customer.customer_name', $this->customer_name]) ;//<=====加入這句
return $dataProvider;
}
第三步:
修改order/index視圖的gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'customer_id',
'status',
['label'=>'客戶', 'attribute' => 'customer_name', 'value' => 'customer.customer_name' ],//<=====加入這句
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Yii2.0小部件GridView(兩表聯(lián)查/搜索/分頁(yè))功能的實(shí)現(xiàn)代碼
- yii2實(shí)現(xiàn)分頁(yè),帶搜索的分頁(yè)功能示例
- Yii2實(shí)現(xiàn)同時(shí)搜索多個(gè)字段的方法
- Yii2實(shí)現(xiàn)讓關(guān)聯(lián)字段支持搜索功能的方法
- Yii2 ActiveRecord多表關(guān)聯(lián)及多表關(guān)聯(lián)搜索的實(shí)現(xiàn)
- 淺析Yii2 GridView實(shí)現(xiàn)下拉搜索教程
- yii2實(shí)現(xiàn)根據(jù)時(shí)間搜索的方法
- yii2帶搜索功能的下拉框?qū)嵗斀?/a>
- Yii2框架整合Xunsearch搜索引擎的方法
- yii2組件之下拉框帶搜索功能的示例代碼(yii-select2)
- Yii2.0框架實(shí)現(xiàn)帶分頁(yè)的多條件搜索功能示例
相關(guān)文章
php實(shí)現(xiàn)的IMEI限制的短信驗(yàn)證碼發(fā)送類
本文給大家分享的是可以檢驗(yàn)手機(jī)號(hào)碼與IMEI的短信驗(yàn)證碼發(fā)送的php類,十分的實(shí)用,這里推薦給大家,有需要的小伙伴可以參考下。2015-05-05
php 替換文章中的圖片路徑,下載圖片到本地服務(wù)器的方法
下面小編就為大家分享一篇php 替換文章中的圖片路徑,下載圖片到本地服務(wù)器的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
遍歷指定目錄,并存儲(chǔ)目錄內(nèi)所有文件屬性信息的php代碼
本篇文章主要介紹了PHP遍歷指定目錄,并存儲(chǔ)目錄內(nèi)所有文件屬性信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10
ThinkPHP有變量的where條件分頁(yè)實(shí)例
這篇文章主要介紹了ThinkPHP有變量的where條件分頁(yè)方法,實(shí)例講述了ThinkPHP條件查詢與分頁(yè)的技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11
命令行執(zhí)行php腳本中的$argv和$argc配置方法
這篇文章主要介紹了命令行執(zhí)行php腳本 中$argv和$argc的方法,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-01-01
關(guān)于PHP內(nèi)置的字符串處理函數(shù)詳解
下面小編就為大家?guī)?lái)一篇關(guān)于PHP內(nèi)置的字符串處理函數(shù)詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02

