zoukankan      html  css  js  c++  java
  • 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'=>'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' => 'yiigridActionColumn',
                        '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 commonmodelssearch;
    
    use Yii;
    use yiiaseModel;
    use yiidataActiveDataProvider;
    use commonmodelsLbTeacher;
    use commonmodelsUser;
    /**
     * LbTeacherSearch represents the model behind the search form about `commonmodelsLbTeacher`.
     */
    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;
        }
    
    }

    如有需要帮助,请留言

  • 相关阅读:
    深度学习之 TensorFlow(一):基础库包的安装
    爬取网易云音乐评论并使用词云展示
    MySQL学习笔记(一):查询
    【linux】查看linux系统版本信息(Oracle Linux、Centos Linux、Redhat Linux、Debian、Ubuntu)
    【php】PHP中Session ID的实现原理
    VMware安装VMwareTolls
    退耦、旁路电容
    SPI笔记
    旧板与IO板之间的连接
    S3C2440启动方式
  • 原文地址:https://www.cnblogs.com/fpcing/p/9012884.html
Copyright © 2011-2022 走看看