企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# GridView ~~~ use yii\grid\GridView; echo yii\grid\GridView::widget([ 'dataProvider' => $dataProvider, ]); ~~~ 常用配置 ~~~ <?php use yii\grid\GridView; echo GridView::widget([ 'dataProvider' => $dataProvider, //表格列值搜索功能,注意一定要配合attribute才会显示 //$searchModel = new ArticleSearch(); 'filterModel' => $searchModel, //重新定义分页样式 'layout'=> '{items}<div class="text-right tooltip-demo">{pager}</div>', 'pager'=>[ //'options'=>['class'=>'hidden']//关闭分页 'firstPageLabel'=>"First", 'prevPageLabel'=>'Prev', 'nextPageLabel'=>'Next', 'lastPageLabel'=>'Last', ], 'columns' => [ ['class' => 'yii\grid\SerialColumn'],//序列号从1自增长 // 数据提供者中所含数据所定义的简单的列 // 使用的是模型的列的数据 'id', 'username', [ 'label' => '车辆ID', // 自定义显示名称 'attribute' => 'id', // 模型字段名称,产生一个a标签,点击可排序*/ //'format' => 'html', // 内容显示模式 //'value' => 'id', // 内容 //'value' => 'cate.cname', //关联表 'value'=>function($model){ // 自定义处理方法 return $model->id; }, //在搜索条件(过滤条件)中使用下拉框来搜索 'filter' => Html::activeDropDownList( $searchModel, 'yeartype', $array, // 搜索条件,一维数组 ['prompt'=>'全部',"class" => "form-control"] ), 'headerOptions' => ['width' => '80'], // 宽度 ], // 操作按钮自定义 [ //动作列yii\grid\ActionColumn //用于显示一些动作按钮,如每一行的更新、删除操作。 'class' => 'yii\grid\ActionColumn', 'header' => '操作', 'template' => '{view} {delete} {update}', // 展示操作 'headerOptions' => ['width' => '240'], 'buttons' => [ // 自定义属性 'diy' => function ($url, $model, $key) { // 按钮属性 $options = [ 'title' => Yii::t('yii', 'View'), 'aria-label' => Yii::t('yii', 'View'), 'data-pjax' => '0', 'model'=>$model->id, 'id'=> $key, ]; // 生成按钮 return Html::a('<span class="glyphicon glyphicon-refresh"></span>', $url, $options); }, 'delete' => function($url, $model, $key){ return Html::a('<i class="fa fa-ban"></i> 删除', ['del', 'id' => $key], [ 'class' => 'btn btn-default btn-xs', 'data' => ['confirm' => '你确定要删除文章吗?',] ] ); }, ], ], ], ]); ?> [ 'attribute' => 'is_delete', 'label' => '是否删除', 'value'=>function ($model){ return $model->is_delete==1?'删除':'未删除'; }, 'filter' => Html::activeDropDownList($searchModel,'is_delete',['1'=>'删除','0'=>'未删除'], ['prompt'=>'全部',"class" => "form-control"]), 'headerOptions' => ['width' => '100'], ], ~~~ #### **操作按钮** ~~~ [ 'class' => 'yii\grid\ActionColumn', 'header' => '操作', 'template' => '{view} {delete} {update} {diy}', // 展示操作 //'headerOptions' => ['width' => '240'], 'buttons' => [ // 自定义按钮 'diy' => function ($url, $model, $key) { // 按钮属性 $options = [ 'title' => Yii::t('yii', 'View'), 'aria-label' => Yii::t('yii', 'View'), 'data-pjax' => '0', 'model'=>$model->id, 'id'=> $key, ]; // 生成按钮 return Html::a('<span class="glyphicon glyphicon-refresh"></span>', $url, $options); }, ] ], ~~~ #### **批量删除** ~~~ // 视图内 - 设置表单ID ,增加ID复选: <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'options' => ['id' => 'grid'], 'footer' => '<button href="#" class="btn btn-default btn-xs btn-delete" url="'. Url::toRoute('admin/delete') .'">删除</button>', 'columns' => [ //['class' => 'yii\grid\SerialColumn'], [ "class" => "yii\grid\CheckboxColumn", "name" => "id", ], ] ?> // 视图内按钮 <?= Html::a("批量删除", "javascript:void(0);", ["class" => "btn btn-success gridview"]) ?> // 视图内注册JS $this->registerJs(' $(".gridview").on("click", function () { var keys = $("#grid").yiiGridView("getSelectedRows"); console.log(keys); $.post("delall?id="+keys); }); '); // 控制器内定义删除方法 public function actionDelall($id) { $model =new CarBrand(); if($model->deleteAll("id in($id)")){ // return $this->render('index',['id'=>3,'mark'=>2]); return $this->redirect(['/carbrand/index']); }else{ echo "err"; } } ~~~ #### 1. 处理时间 ~~~ [ 'label'=>'更新日期', 'format' => ['date', 'php:Y-m-d'], 'value' => 'updated_at' ], //or [ //'attribute' => 'created_at', 'label'=>'更新时间', 'value'=>function($model){ return date('Y-m-d H:i:s',$model->created_at); }, 'headerOptions' => ['width' => '170'], ], ~~~ #### 2. 处理图片 ~~~ [ 'label'=>'封面图', 'format'=>'raw', 'value'=>function($m){ return Html::img($m->cover, ['class' => 'img-circle', 'width' => 30] ); } ], ~~~ #### 3. 数据列有链接 ~~~ [ 'attribute' => 'title', 'value' => function ($model, $key, $index, $column) { return Html::a($model->title, ['article/view', 'id' => $key]); }, 'format' => 'raw', ], ~~~ #### 4. 数据列显示枚举值(男/女) ~~~ [ 'attribute' => 'sex', 'value'=>function ($model,$key,$index,$column){ return $model->sex==1?'男':'女'; }, //在搜索条件(过滤条件)中使用下拉框来搜索 'filter' => ['1'=>'男','0'=>'女'], //or 'filter' => Html::activeDropDownList($searchModel, 'sex',['1'=>'男','0'=>'女'], ['prompt'=>'全部'] ) ], [ 'label'=>'产品状态', 'attribute' => 'pro_name', 'value' => function ($model) { $state = [ '0' => '未发货', '1' => '已发货', '9' => '退货,已处理', ]; return $state[$model->pro_name]; }, 'headerOptions' => ['width' => '120'] ] ~~~