小巫师 2017-06-09 09:06:03 10648次浏览 4条评论 5 3 0

1、在view视图层列表展示页面
头部放置:

<?php
use yii\widgets\LinkPager;
?>

尾部放置:

<?= LinkPager::widget(['pagination' => $pages]); ?>

2、在控制器层

<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use frontend\models\Text;
use yii\data\Pagination;


class NewsController extends Controller
{
  public $enableCsrfValidation = false;
  public function actionIndex()
  {
    $count = News::find()->count();
    $page = new Pagination(['totalCount' => $count,'pageSize'=>'5']);
    $data = News::find()
      ->innerJoinWith('newstype')
      ->offset($page->offset)
      ->limit($page->limit)
      ->asArray()
      ->all();
    // print_r($data);die;
    return $this->render('index',['data'=>$data,'page'=>$page]);
  }

3、在model层建立text.php

<?php

namespace frontend\models;

use yii;
use yii\db\ActiveRecord;
class Text extends ActiveRecord{
    public static function tableName()
    {
        return '{{text}}';
    }
}
觉得很赞
  • 评论于 2017-07-27 13:24 举报

    麻烦你自己走通了再发出来。很普通的错误。

    3 条回复
    评论于 2017-08-01 12:01 回复

    都是走通了 才发的 你如果不懂别看就行了

    评论于 2017-08-01 12:02 回复

    如果不能理解这些基本 那你学的太死了

    评论于 2018-07-09 13:39 回复

    return $this->render('index',['data'=>$data,'page'=>$page]); 'page'修改为'pages' 都是暴躁老哥

  • 评论于 2017-07-28 16:43 举报
    * Controller action:
     *
    
    • public function actionIndex()
    • {
    • $query = Article::find()->where(['status' => 1]);
    • $countQuery = clone $query;
    • $pages = new Pagination(['totalCount' => $countQuery->count()]);
    • $models = $query->offset($pages->offset)
    • ->limit($pages->limit)
    • ->all();
      *
    • return $this->render('index', [
    • 'models' => $models,
    • 'pages' => $pages,
    • ]);
    • }
    •  *
      
    • View:
      *
    • 
      
    • foreach ($models as $model) {
    • // display $model here
    • }
      *
    • // display pagination
    • echo LinkPager::widget([
    • 'pagination' => $pages,
    • ]);
      
      

    这个官方的注释,比你这个清晰一点.

  • 评论于 2017-08-13 09:15 举报

    感觉还可以

    1 条回复
    评论于 2017-08-15 09:07 回复

    谢谢

  • 评论于 2017-08-23 10:28 举报

    bucuo==========================不错

您需要登录后才可以评论。登录 | 立即注册