控制LinkPager每页显示的条数 [ 新手入门 ]
本人初学,不会设置LinkPager的一些参数,代码不是很严谨,望各位大神不吝赐教
◆模型
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Good extends ActiveRecord
{
}
◆控制器
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\Good;
use yii\data\Pagination;
class HiController extends Controller
{
public function actionEntry()
{
$session = Yii::$app->session;
$session->open();
$request=Yii::$app->request;
if(!$session->get('pageSize')){
$session->set('pageSize',2);
}
if($request->get()){
$pageSize=$request->get('per-page');
$session->set('pageSize',$pageSize);
}
$pageSize=$session->get('pageSize');
$data=Good::find();
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => $pageSize]);
$model = $data->offset($pages->offset)->limit($pages->limit)->all();
return $this->render('t',['model'=>$model,'pages'=>$pages,'pageSize'=>$pageSize]);
}
}
◆视图
<style>
.linkpager{
float:left;
}
.lf{
margin-left:7px;
}
.pane {
border-radius: 5px;
outline:none;
border:solid 1px #dddddd;
width:52px;
height:34px;
text-align:center;
}
.ok{
height:34px;
margin-left:3px;
}
</style>
<?php
use yii\bootstrap\ActiveForm;
use yii\widgets\LinkPager;
?>
<table class="table table-hover" style="width:300px;">
<thead>
<tr>
<th>id</th>
<th>name</th>
</tr>
</thead>
<tbody>
<?php foreach($model as $key=>$val): ?>
<tr>
<td><?=$val->id?></td>
<td><?=$val->name?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?=LinkPager::widget([
'pagination' => $pages,
'options'=>['class'=>'pagination linkpager'],
]);
?>
<form action="" method="get" class="pagination">
<span class="lf">跳到</span><input class="pane" type="text" name="page">页
<span class="lf">每页显示</span>
<select class="pane" name="per-page">
<option selected="selected"><?=$pageSize?></option>
<?php for($i=1;$i<=100;$i++):?>
<option><?=$i?></option>
<?php endfor;?>
</select>条
<button class="btn btn-default ok">确定</button>
</form>
效果如下:
共 0 条回复
没有找到数据。
wilikeit
注册时间:2016-11-01
最后登录:2017-01-01
在线时长:9小时44分
最后登录:2017-01-01
在线时长:9小时44分
- 粉丝2
- 金钱80
- 威望0
- 积分170