下面介绍一下 Activedataprovider 类分页的使用
use yii\data\ActiveDataProvider; 这是必须的步骤,因为我们要使用 ActiveDataProvider 类
use common\models\User;
public function actionList()
{
$model = new User();
$dataProvider = new ActiveDataProvider([
'query' => $model->find(),
'pagination => [
'pagesize' => '10',
]
]);
return $this->render('list', ['model' => $model, 'dataProvider' => $dataProvider]);
}
view代码
list.php
use yii\grid\GridView; 这是必须的;
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'attribute',(attribute为字段的名称,开发时候根据自己的需要进行修改)
[
'attribute' => 'create_time',
'format' => ['data', 'Y-m-d H:i:s'],
'options' => ['width' => '200']
]],
['class' => 'yii\grid\ActionColumn', 'header' => '操作', 'headerOptions' => ['width' => '80']],
]);
下面为大家介绍第二种分页方法:
控制器 CommentController 里面的任意一个方法,在这里我的方法是 actionComment();
<?php
use yii\data\Pagination;
use app\models\Comment;
public function actionComment(){
$data = Comment::find()->andWhere(['id' => '10']);
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '2']);
$model = $data->offset($pages->offset)->limit($pages->limit)->all();
return $this->render('comment',[
'model' => $model,
'pages' => $pages,
]);
}
?>
好的,到这里,控制器部分基本就结束了。我们接续看 view 里面的代码:
Comment.php 文件代码如下所示
<?php
use yii\widgets\LinkPager;
?>
foreach($model as $key=>$val)
{
这里就是遍历数据了,省略......
}
<?= LinkPager::widget(['pagination' => $pages]); ?>
参考: http://blog.sina.com.cn/s/blog_88a65c1b0101j0md.html
参考: http://blog.sina.com.cn/s/blog_88a65c1b0101ixhw.html
搜索
标签
study
ab
amap
apache
apahe
awk
aws
bat
centos
CFS
chrome
cmd
cnpm
composer
consul
crontab
css
curl
cygwin
devops
di
docker
docker,docker-compose
ethereum
excel
fiddler
fluentd
framework
front-end
git
gitgui
github
glide
go
golang
gorm
grafana
gzip
ioc
item2
iterm2
javascript
jenkins
jsonp
kafka
laradock
laravel
larval
linux
liunux
log
mac
mac, wi-fi
macos
magento
mariaDB
minikube
mongoDB
msp
mysql
netbeans
nginx
nodejs
nohup
npm
nsq
php
php-fpm
php7
phpstorm
php扩展
Protobuf
python
redis
scp
server
shell
soap
socket
socket5
sql
sre
ssdb
ssh
ssl
study
sublime
swift
system
td-agent
uml
v2ray
vagrant
vagrnat
vim
vpn
vue
vue.js
webpack
webrtc
websocket
webtatic
windows
windows7
word
wps
xdebug
yarn
yii2
yum
zookeeper
世界国家
互联网
以太坊
分类
前端
小程序
打印机
排序算法
搞笑
权限
粤语
缓存
网络
虚拟机
视频
设计模式
项目管理
热门文章
友情链接