一:Yii2這是控制器中的分頁,能夠定義分頁的條數:php
controller中的用法:html
public function actionList() { $model = new User(); $dataProvider = new ActiveDataProvider([ 'query' => $model->find(); 'pagination => [ 'pagesize' => '10', ] ]); return $this->render('list', [ 'model' => $model, 'dataProvider' => $dataProvider, ]); }
<?= GridView::widget([ 'dataProvider' => $dataProvider, //'filterModel' => $searchModel, 'layout'=>'<div id="DataTables_Table_0_wrapper" class="dataTables_wrapper"> {items} <div class="row-fluid"> <div class="span12"><div class="dataTables_info" id="DataTables_Table_0_info">{summary}</div></div> <div class="span12 center"> <div class="dataTables_paginate paging_bootstrap pagination"> {pager} </div> </div> </div> </div> <div class="row"> ', 'pager'=>[ 'prevPageLabel'=>'← 上一頁', 'nextPageLabel'=>'下一頁 →', ], 'columns' => [ ['class' => 'yii\grid\SerialColumn'], ['class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($model, $key, $index, $column) { return ['value' => $model->id]; }], ['attribute'=>'pics','value'=>function($model){ $path = '/static/images/brand-head.jpg'; if(!empty($model->pics_arr)){ $path = $model->pics_arr[0]['path']; } return "<img src='$path' width=75 height=75>"; },'format'=>'html'], 'name', ['label'=>'品牌', 'attribute' => 'name_zh', 'value' => 'brand.name_zh'], ['label'=>'類目', 'attribute' => 'catename', 'value' => 'category.name'], [ 'attribute' => 'status', 'label' => '狀態', 'value'=>function($model){ return $model->status_text; }, 'filter' => \common\models\ProductBase::get_status(), ], ['attribute'=>'storage_time','format'=>['date', 'php:Y-m-d']], ['label'=>'更多操做', 'format' => 'raw', 'value' => function($model){ return "<a class='btn btn-success' href='".Url::to(['product-base/view','id'=>$model->id])."' alt='查看'> <i class='icon-zoom-in'></i> </a> <a class='btn btn-info' href='".Url::to(['product-base/update','id'=>$model->id])."' alt='修改'> <i class='icon-edit'></i> </a> <a class='btn btn-danger' href='".Url::to(['product-base/delete','id'=>$model->id])."' alt='刪除'> <i class='icon-trash'></i> </a> "; }], [ 'label' => '更多操做', 'format'=>'raw', 'value' => function($model){ return Html::a('店鋪商品', ['product/index','id' => $model->id], ['title' => '店鋪商品','class'=>'btn btn-info btn-setting']); } ], ], ]); ?>
二:這應該是一種原生的寫法數據庫
文件頭先導入Pagination類: use yii\data\Pagination; 具體的action處,這麼用: public function actionIndex() { $query = News::find() ->where(['news_type' => 1]) ->orderBy('published_on DESC, id DESC'); $countQuery = clone $query; $pages = new Pagination([ 'totalCount' => $countQuery->count(), 'pageSize' => 2, ]); $models = $query->offset($pages->offset) ->limit($pages->limit) ->all(); return $this->render('index', [ 'models' => $models, 'pages' => $pages, ]); }
頁頭處導入LinkPager類:bootstrap
use yii\widgets\LinkPager; 具體要顯示的時候這麼用 foreach ($models as $model) { // display $model here } // display pagination echo LinkPager::widget([ 'pagination' => $pages, ]);
$defaultPageSize
屬性。<div class="text-center">...</div>
便可轉自:http://www.yiichina.com/tutorial/481app