yii2的GridView和ActiveDataProvider具体使用
此例子为带有搜索功能的列表
控制器代码:
public function actionIndex($success = "")
{
$searchModel = new Search();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams,1);
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
视图文件view
说明:filterModel => $searchModel 是视图文件中用来搜索用的
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'tableOptions'=>[
'class'=>'opgrid table table-hover table-list table-striped jtable'
],
'columns' => [
['label' => '编号','enableSorting'=>false, 'headerOptions'=> ['style'=>'width:40px;'],'value' => function($model){
return $model->user_id;
}],
array('label' => '用户名', 'attribute' => 'username', 'format' => 'html', 'value' => function($model){
return Html::a($model->user->username, ['user/update','id' => $model->user_id, 't' =>$this->context->id]);
}),
array('label' => '姓名','attribute' => 'fullname', 'value' => function($model){
return $model->user->fullname;
}),
array('attribute' => 'Email', 'value' => function($model){
return $model->user->email;
}),
array('attribute' => '注册时间', 'value' => function($model){
return date('Y-m-d H:i:s', $model->user->created_at);
}),
[
'class' => 'yii\grid\ActionColumn',
'header'=>'操作',
'template' => '{delete},{resetpwd}',
'buttons' =>[
'resetpwd' => function ($url, $model, $key) {
return Html::a('重置密码', ['/user/resetpwd','id' => $model->user_id,'t' =>$this->context->id],['display'=>'none']);
},
'unbinding' => function ($url, $model, $key) {
$html = '';
if ($model->user->wechat_id > 0){
$html = Html::a('解除绑定', ['/teacher/unbinding','id' => $model->user_id]);
}
return $html;
},
]
],
],
]);
怎么添加用户名和姓名两个搜索框:
搜索用户名传过去的GET参数为:username:xxxxx
搜索姓名传过去的GET参数为fullname:xxxxx
上面的代码
'attribute' => 'username',
'attribute' => 'fullname',
所以上面的代码的attribute必须要和搜索的田间对应,也就是说,你要搜索什么,这里的attribute就要写什么
如果不想要username,只需要把 'attribute'=>'username' 改成 'label'=>'用户名' 就可以了
模型类的Search的配置
特别要书注意rulers这个方法,搜索的字段在这里一定要加上,验证规则最好写成safe
namespace common\models\search;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\LbTeacher;
use common\models\User;
/**
* LbTeacherSearch represents the model behind the search form about `common\models\LbTeacher`.
*/
class Search extends LbTeacher
{
public $username;
public $fullname;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id', 'fullname', 'username'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = LbTeacher::find()->leftJoin('user','user.id = teacher.user_id');
if(empty($params['sort'])){
$query->orderBy(['teacher.user_id'=>SORT_DESC]);
}else {
$query->orderBy($params['sort']);
}
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere(['like', 'user.username', $this->username])
->andFilterWhere(['like', 'user.fullname', $this->fullname]);
return $dataProvider;
}
}
如有需要帮助,请留言