首页 > 代码库 > Activedataprovider 类分页的使用

Activedataprovider 类分页的使用

下面以管理员列表为例说明Activedataprovider分页的具体使用

1.控制器中

public function actionIndex()
{
$model=new Admin();
$dataProvider = new ActiveDataProvider([
‘query‘ => Admin::find()->orderBy(‘id‘),
‘pagination‘ => [‘pageSize‘ => 5],//此处为分页设置,其中5为每页显示5条
]);
return $this->render(‘index‘,[
‘model‘=>$model,
‘dataProvider‘=>$dataProvider
]);
}

2.view视图文件中

index.php中

<?=
GridView::widget([
‘dataProvider‘ => $dataProvider,
‘columns‘ => [
[
‘attribute‘ => ‘name‘,//字段名
‘content‘ => function($dataProvider){
return $dataProvider[‘name‘];
},
],
‘description‘,//字段名
[
‘attribute‘ => ‘created_at‘,
‘format‘ => [‘date‘, ‘php:Y-m-d H:i:s‘],
],

[
‘class‘ => ‘yii\grid\ActionColumn‘,
‘header‘ => ‘操作‘,
‘template‘ => ‘{view}{update}{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‘,
]);
},
],
],
],
/*‘layout‘ => "{items}\n{pager}",*/ //此处注释显示条数信息
]);
?>

分页和列表显示效果如下图:
技术分享

Activedataprovider 类分页的使用