[Yii2笔记]061实例,数据分页和排序显示 [ 技术分享 ]
说明
学习Yii Framework 2(易2框架)的过程是漫长的,也是充满乐趣的,以下是我学习Yii2框架时对官网英文资料的代码实现,提供了较完整的代码,供你参考。不妥之处,请多多指正!
原文网址:
http://www.yiiframework.com/doc-2.0/guide-output-pagination.html
http://www.yiiframework.com/doc-2.0/guide-output-sorting.html
本文主题:实例,数据分页和排序显示
D:\phpwork\news\controllers\ArticlesController.php
public function actionPagination() {
$data=TbArticles::valPagination();
return $this->render('pagination',$data);
}
D:\phpwork\news\views\articles\pagination.php
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<div class="articles-index">
<h1><?= Html::encode($this->title) ?></h1>
<table>
<tr>
<td><?=$sort->link('title')?></td>
<td><?=$sort->link('createdAt')?></td>
<td>更新时间</td>
</tr>
<?php
foreach($articles as $article){
echo "<tr>
<td>".$article->title."</td>
<td width='150'>".date('Y-n-j',$article->createdAt)."</td>
<td width='150'>".date('Y-n-j',$article->lastEdited)."</td>
</tr>";
}
?>
</table>
<div></div>
<?=LinkPager::widget(['pagination'=>$pagination])?>
<div></div>
<?php
echo "<br>".$pagination->createUrl(5);
echo "<br>".$pagination->createUrl(6);
echo "<br>".$sort->createUrl('title');
?>
</div>
D:\phpwork\news\models\TbArticles.php
public static function valPagination() {
$query=static::find()->where(['isDeleted'=>0]);
$count=$query->count();
$pagination=new Pagination([
'totalCount'=>$count,
//defaultPageSize
'defaultPageSize'=>3,
//'pageSize'=>3,//此句与上句同效,但在url中会多一个查询参数:per-page=3
]);
//创建排序字段
$sort=new Sort([
'attributes'=>[
//单个属性
'createdAt',
//复合属性
'title'=>[
'asc'=>['title'=>SORT_ASC,'createdAt'=>SORT_DESC],
'desc'=>['title'=>SORT_DESC,'createdAt'=>SORT_DESC],
'default'=>SORT_ASC,//首次点击此列排序连接时的排序方式
'label'=>'标题',
],
],
//defaultOrder,此参数与上面attributes中的default用处不同,注意区别!!
//'defaultOrder' => ['createdAt' => SORT_DESC],//初始页面中的默认排序方式
]);
//只修改单个属性createdAt的标签,其他则使用默认值
$sort->attributes['createdAt']['label']='创建时间';
$articles=$query->select(['title','createdAt','id','lastEdited'])
->offset($pagination->offset)
->limit($pagination->limit)
->orderBy($sort->orders)
->all();
return ['articles'=>$articles,'pagination'=>$pagination,'sort'=>$sort];
}
测试结果:
http://localhost:8085/articles/pagination
/*
Articles
标题 创建时间 更新时间
巴黎数千人再次示威 警方用催泪弹驱赶场面混乱 2017-3-29 2017-3-29
复星CEO宣布辞职 梁信军辞职全体员工信 2017-3-29 2017-3-29
国家气候中心预测今年汛期我国气候状况总体偏差 2017-3-29 2017-3-29
« 1 2 3 4 5 6 7 8 »
/articles/pagination?page=6
/articles/pagination?page=7
/articles/pagination?page=3&sort=title
*/
数据库
/*
-- ----------------------------
-- Table structure for articles
-- ----------------------------
DROP TABLE IF EXISTS `articles`;
CREATE TABLE `articles` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`authorId` int(10) unsigned DEFAULT NULL,
`createdAt` int(11) unsigned DEFAULT NULL,
`lastEdited` int(11) unsigned DEFAULT NULL,
`title` varchar(250) DEFAULT NULL,
`description` text,
`content` mediumtext,
`userId` int(10) unsigned DEFAULT '4',
`updaterId` int(10) unsigned DEFAULT '4',
`format` tinyint(1) unsigned DEFAULT '0',
`isDeleted` tinyint(1) unsigned DEFAULT '0',
`categoryId` int(10) unsigned DEFAULT '9',
PRIMARY KEY (`id`),
KEY `authorIdForeign` (`authorId`),
CONSTRAINT `authorIdForeign` FOREIGN KEY (`authorId`) REFERENCES `authors` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of articles
-- ----------------------------
INSERT INTO `articles` VALUES ('1', '5', '1490760176', '1490760176', '巴黎数千人再次示威 警方用催泪弹驱赶场面混乱', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('2', '5', '1490760201', '1490760201', '复星CEO宣布辞职 梁信军辞职全体员工信', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('3', '5', '1490779126', '1490779126', '国家气候中心预测今年汛期我国气候状况总体偏差', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('4', '5', '1490779126', '1493176240', '世界最大金币被偷 重100公斤价值450万美元', '', '', '4', '2', '1', '0', '11');
INSERT INTO `articles` VALUES ('7', '1', '1490844742', '1490857688', '苏格兰议会支持举行第二次独立公投', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('8', '4', '1490779126', '1490857679', '侠女护士怒斥医托走红:面对病人被骗 我不能不出手', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('9', '3', '1490779126', '1490857679', '一名中国女性花200万美元买下特朗普儿时故居', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('10', '5', '1490781703', '1490781703', '国台办:走遍世界恐怕也没叫“台湾国”的地方', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('11', '4', '1490839125', '1490857694', '地方国资监管加码将设负面清单 严防国资流失', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('12', '5', '1490940690', '1490940690', '城管执法将使用执法记录仪 将于5月1日起施行', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('13', '5', '1490940704', '1490940704', '一天内全国6城出手限购 楼市调控风暴正在持续', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('14', '5', '1490940715', '1490940715', '46城将实施生活垃圾强制分类 居民正确投放给奖励', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('15', '5', '1490940722', '1490940722', '聂树斌母亲谈未来生活:换个心情好好过几年', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('16', '5', '1490940732', '1490940732', '侠客岛:不仅是国产航母!中国海军最近好多大招', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('17', '5', '1490940752', '1490940752', '除反腐剧里的“藏钱”,贪官买房还干啥?', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('18', '5', '1490940769', '1490940769', '养老金实现十三连涨 为何今年平均涨幅是5.5%?', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('19', '2', '1490940801', '1492594484', '北京清明假期高速路出行量将创历史新高', '', '', '4', '4', '1', '0', '11');
INSERT INTO `articles` VALUES ('20', '5', '1490940813', '1490940813', '北京市属三甲医院:处方需经审核方能取药', '', '', '4', '4', '1', '0', '9');
INSERT INTO `articles` VALUES ('21', '5', '1490940825', '1493277416', '日本人英语为什么这么差?老师都用日式英语教学', '', '日本人英语为什么这么差?老师都\r\n用日式英语教学', '4', '2', '1', '1', '9');
INSERT INTO `articles` VALUES ('22', '5', '1490940833', '1492155439', '共享单车平台已设年龄门槛 小学生为何仍能开锁', '', '> 123\r\n\r\n### quote here\r\n```\r\n共享单车平台已设年龄门槛 小学生为何仍能开锁\r\n共享单车平台已设年龄门槛 小学生为何仍能开锁\r\n```\r\n> 共享单车平台已设年龄门槛 小学生为何仍能开锁 \r\n> 共享单车平台已设年龄门槛 小学生为何仍能开锁 \r\n\r\n`code text here`\r\n~~~php \r\n$var=\'ok\'\r\nif($var){\r\n echo \"OK\"!\r\n}\r\n~~~ \r\n~~~ \r\n\r\n~~~ \r\n`ddsafas`', '4', '4', '0', '0', '9');
INSERT INTO `articles` VALUES ('23', '5', '1490940885', '1492077327', '心脏支架的内幕:耗材成医疗腐败重灾区', '', '心脏支架的内幕:耗材成医疗腐败重灾区心脏支架的内幕:耗材成医疗腐败重灾区心脏支架的内幕:耗材成医疗腐败重灾区心脏支架的内幕:耗材成医疗腐败重灾区', '4', '4', '0', '0', '9');
INSERT INTO `articles` VALUES ('24', '5', '1490940903', '1492069622', '国土部:年底前所有地方房源接入国家平台', 'Description', '国土部:年底前所有地方房源接入国家平台国土部:年底前所有地方房源接入\r\n国家平台国土部:年底前所有地方房源接入国家平台国土部:年底前所有地方\r\n房源接入国家平台国\r\n土部:年底前所有地方房源接入国家平台国土部:年底前所有地方房源\r\n接入国家平台c', '4', '4', '0', '0', '9');
INSERT INTO `articles` VALUES ('25', '1', '1493176390', '1493176390', '我国第二艘航空母舰下水 范长龙出席下水仪式并致辞', '', '', '2', '2', '0', '0', '16');
INSERT INTO `articles` VALUES ('26', '1', '1493273652', '1493273652', '中国首艘国产航母在大连正式下水 完成优雅转身', '', '中国首艘国产航母在大连正式下水 完成优雅转身', '9', '9', '0', '0', '11');
INSERT INTO `articles` VALUES ('27', '1', '1493277523', '1493277527', '1234', '', '23423424', '9', '9', '0', '1', '16');
INSERT INTO `articles` VALUES ('28', '1', '1493277680', '1493277747', '1234', '', '', '9', '9', '0', '1', '16');
*/
(全文完)
共 0 条回复
没有找到数据。
阿江
注册时间:2015-10-18
最后登录:2024-03-03
在线时长:186小时21分
最后登录:2024-03-03
在线时长:186小时21分
- 粉丝94
- 金钱16816
- 威望160
- 积分20276