wilikeit 2016-11-17 13:13:25 2784次浏览 0条回复 2 1 0

本人初学,不会设置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>

效果如下: 2016-11-17_130844.png

参考:http://www.cnblogs.com/gyrgyr/p/5249392.html

觉得很赞
    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册