wilikeit 2016-11-21 21:02:41 2442次浏览 0条回复 2 1 0

◆模型

<?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()
    {
        $data=Good::find();
        $pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => 2]);
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        return $this->render('t',['model'=>$model,'pages'=>$pages]);
    }
}

◆视图

<?php
use yii\widgets\LinkPager;
?>
<table class="table table-hover" style="width:600px;">
<thead>
<tr>
<th>id</th>
<th>name</th>
</tr>
</thead>
<tbody>
<?php foreach($model as $key=>$val): ?>
    <tr>
    <td><?=$val->id?></td>    <!--相当于  <?php echo $val['id'];?>-->
    <td><?=$val->name?></td>
    </tr>
<?php endforeach; ?>
</tbody>
</table>
<?=LinkPager::widget([
    'pagination' => $pages,
    'nextPageLabel' => '下一页',
    'prevPageLabel' => '上一页',
    'firstPageLabel' => '首页',
    'lastPageLabel' => '尾页',
    ]);
?>

效果如下:

lk.png

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