conrtoller
$search = new UserAccount();
$provider = $search->search(Yii::$app->request->get());
return $this->render('search', [
'provider' => $provider,
'searchModel' => $search,
]);
model
public function search($param)
{
$query = self::find()->alias('user')->joinWith(['type','status']);
$provider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'defaultOrder' => [
'RegTime' => SORT_ASC,
]
]
]);
//不通过就不增加条件,直接返回
if(!($this->load($param) && $this->validate())){
return $provider;
}
$query->andFilterWhere(['user.Type' => $this->Type]);
$query->andFilterWhere(['user.Account' => $this->Account]);
$query->andFilterWhere(['like', 'user.PhoneAddress', $this->PhoneAddress]);
return $provider;
}
view
<?php
use yii\grid\GridView;
use yii\helpers\Html;
use app\models\UserAccount;
?>
<?= GridView::widget([
'dataProvider' => $provider,
'filterModel' => $searchModel,
'columns' => [
[
'class' => 'yii\grid\SerialColumn',
],
'Account',
'UIN',
[
'attribute' => 'Type',
'value' => function($data){
return $data->type->name;
},
'filter' => UserAccount::getDictionaryDatas(UserAccount::DICTIONARY_USER_TYPE),
],
'Status',
'RegTime',
'RegSrc',
'PhoneAddress',
]
]) ?>
需要注意的:
筛选的字段必须是safeAttribute,即在rules中有定义规则的,如果没有,则load()是无法加载数据的
如果type和status等这种固定值的筛选,想要创建下拉,则可以给column列中定义filter,对应一个数组键值对。