PHPIN.NET

 找回密码
 立即注册
查看: 6843|回复: 0

[基础应用] Yii2 使用gii生成 gridview分页时候 显示第一页和最后一页 方法

[复制链接]

469

主题

31

回帖

5509

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5509
发表于 2015-12-24 16:27:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Yii2 使用gii生成 gridview分页时候 显示第一页和最后一页 方法


在Yii2 使用gii生成 gridview分页时候 model中是ActiveDataProvider ,默认是没有第一页,最后一页的, 要想显示第一页和最后一页,不需要加载其他东西,只需要在view中增加个pager属性

  1. <?php
  2. use yii\grid\GridView;
  3. echo GridView::widget([
  4.     'dataProvider' => $dataProvider,
  5.     //每列都有搜索框 控制器传过来$searchModel = new ArticleSearch();
  6.     //'filterModel' => $searchModel,
  7.     'layout'=> '{items}<div class="text-right tooltip-demo">{pager}</div>',
  8.      'pager'=>[
  9.                //'options'=>['class'=>'hidden']//关闭自带分页
  10.                'firstPageLabel'=>"First",
  11.                 'prevPageLabel'=>'Prev',
  12.                 'nextPageLabel'=>'Next',
  13.                  'lastPageLabel'=>'Last',
  14.       ],
  15.     'columns' => [
  16.         //['class' => 'yii\grid\SerialColumn'],//序列号从1开始
  17.         // 数据提供者中所含数据所定义的简单的列
  18.         // 使用的是模型的列的数据
  19.         'id',
  20.         'username',
  21.         ['label'=>'文章类别',  /*'attribute' => 'cid',产生一个a标签,点击可排序*/  'value' => 'cate.cname' ],
  22.         ['label'=>'发布日期','format' => ['date', 'php:Y-m-d'],'value' => 'created_at'],
  23.         // 更复杂的列数据
  24.         ['label'=>'封面图','format'=>'raw','value'=>function($m){
  25.          return Html::img($m->cover,['class' => 'img-circle','width' => 30]);
  26.         }],
  27.         [
  28.             'class' => 'yii\grid\DataColumn', //由于是默认类型,可以省略
  29.             'value' => function ($data) {
  30.                 return $data->name;
  31.                 // 如果是数组数据则为 $data['name'] ,例如,使用

  32. SqlDataProvider 的情形。
  33.             },
  34.         ],
  35.         [
  36.          'class' => 'yii\grid\ActionColumn',
  37.          'header' => '操作',
  38.          'template' => '{delete} {update}',//只需要展示删除和更新
  39.          /*'headerOptions' => ['width' => '80'],*/
  40.          'buttons' => [
  41.              'delete' => function($url, $model, $key){
  42.                       return Html::a('<i class="glyphicon glyphicon-trash"></i> 删除',
  43.                              ['artdel', 'id' => $key],
  44.                              ['class' => 'btn btn-default btn-xs',
  45.                               'data' => ['confirm' => '你确定要删除文章吗?',]
  46.                              ]);
  47.              },
  48.             'update' => function($url, $model, $key){
  49.                      return Html::a('<i class="fa fa-file"></i> 更新',
  50.                             ['artedit', 'id' => $key],
  51.                             ['class' => 'btn btn-default btn-xs']);
  52.              },
  53.             ],
  54.          ],
  55.     ],
  56. ]);
  57. ?>
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|PHPIN.NET ( 冀ICP备12000898号-14 )|网站地图

GMT+8, 2024-4-20 20:41

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表