1.控制器中(以User模块的列表为例):
第一步:
use backend\models\User;use yii\data\ActiveDataProvider;
第二步:
public function actionIndex(){ $model=new User(); $dataProvider = new ActiveDataProvider([ ‘query‘ => User::find()->orderBy(‘id‘), ]); return $this->render(‘index‘, [ ‘model‘ => $model, ‘dataProvider‘ => $dataProvider, ]);}
2.view文件中:
第一步:
use yii\helpers\Html;use yii\grid\GridView;use backend\models\User;
第二步:
<?=GridView::widget([ ‘dataProvider‘ => $dataProvider, ‘columns‘ => [ ‘id‘, [ ‘attribute‘ => ‘username‘, ‘content‘ => function($dataProvider){ return $dataProvider[‘username‘]; }, ], ‘email:email‘, [ ‘attribute‘ => ‘created_at‘, ‘format‘ => [‘date‘, ‘php:Y-m-d H:i:s‘], ], [ ‘class‘ => ‘yii\grid\ActionColumn‘, ‘header‘ => ‘操作‘, ‘template‘ => ‘{view}{update}{password}{delete}‘, ‘headerOptions‘ => [‘width‘ => ‘128‘, ‘class‘ => ‘padding-left-5px‘,], ‘contentOptions‘ => [‘class‘ => ‘padding-left-5px‘], ‘buttons‘ => [ ‘password‘ => function ($url, $model, $key) { return Html::a(‘<span class="glyphicon glyphicon-edit"></span>‘, $url, [ ‘title‘ => ‘修改密码‘, ‘data-method‘ => ‘post‘, ‘data-pjax‘ => ‘0‘, ]); }, ], ], ],]); ?>3.运行效果如下图:
时间: 2024-10-14 08:13:58