yii'-GridView

来源:互联网 发布:淘宝回收手机吗 编辑:程序博客网 时间:2024/05/16 14:12
 <?= GridView::widget([        'dataProvider' => $dataProvider,        'filterModel' => $searchModel, //table第二行生成查询表单        'columns' => [            ['class' => 'yii\grid\SerialColumn'],//            'id',            [               'attribute'=>'id',               'contentOptions'=>['width'=>'30px']  //设置html属性            ],            'name',//            'author_id', //不想显示id,想显示作者name            [//                'attribute'=>'author_id',                //关联字段搜索author_id(其他表中的字段,但是要根据name搜索):                //1 想按照author.name搜索,就自定义一个冗余字段在searchModel中添加属性"authorName",这里也不在用author_id                'attribute'=>'authorName',                /*2 在serchModel中新添加字段如下:                public function attributes()                {                    return array_merge(parent::attributes,['authName']);                }                 3 在serchModel中的rules规则中添加字段authName,此时表单搜索框已经出来了,但是还不能搜索                 4 在searchMode最下方添加关联查询如下:                    $query->join('INNER JOIN','Adminer','post.author_id = Adminer.id');                    $query->andFilterWhere(['like','Adminer.nickname',$this->authorName]);                    [注]:这里要注意,如果两个表字段相同,要指定"表名.属性",否则查询就会报错                 5 让authorName可以排序                    $dataProvider->sort->attributes['authorName'] =                    [                        'asc'=>['Adminer.nickname'=>SORT_ASC], //可以升序                        'desc'=>['Adminer.nickname'=>SORT_DESC],//可以降续                    ]                */                'value'=>'author.name' //关联的作者表author            ],//            'content', //截取几个字段显示            [                'attribute'=>'content',                'value'=>function($model,$key,$index,$column){                    //$model:当前行对象;$key:当前行的键值;$index:当前行的索引;$column:数据列对象                    $tmpStr = strip_tags($model->content);                    $tmp_len = mb_strlen($tmpStr,'utf-8');                    return mb_substr($tmpStr,0,20,'ytf-8').($tmp_len > 20 ? '...': '');                }            ],            [                'attribute' => 'status' , //下拉菜单                'value'=>'status0.name',                'filter'=>[1=>'草稿箱',2=>'已发布'], //key=>val 就会形成下拉菜单筛选                'contentOptions'=>function($model){ //添加html属性以及class                    return $model->status == 1 ? ['class'=>'bg-danger'] : [];                }            ],            'content:ntext',//            'created_at',            [                'attribute'=>'created_at',                'format'=>['date','php:Y-m-d H:i:s']            ],            'updated_at',//            ['class' => 'yii\grid\ActionColumn'], //负责动作按钮的,这是一种简写:只展示"查看","修改","删除"            [                'class' => 'yii\grid\ActionColumn',                'template'=>'{view}{update}{delete}{customer}',  //添加自己扩展的动作                'button'=>[                    'customer'=>function($url,$model,$key){                        //$url:按钮对应的url;$model:当前模对象的模型;$key:模型的键                        $options = [                            'title'=>Yii::t('yii','审核'), //Yii::t(): 多语言翻译                            'aria-label'=>Yii::t('yii','审核'),                            'data-confitm'=>Yii::t('yii','你确定执行此操作嘛?'),                            'data-method'=>'post',                            'data-pjax'=>0,                            //如果action是别的控制器的,就指定controller                            ];                        return Html::a('<span class="glyphicon glyphicon-check"></span>',$url,$options);                    }                ]            ]        ]    ]); ?>
原创粉丝点击