首页 > 代码库 > yii2的GridView和ActiveDataProvider具体使用
yii2的GridView和ActiveDataProvider具体使用
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.运行效果如下图:
yii2的GridView和ActiveDataProvider具体使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。