• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

yii2实现分页和带搜索的分页功能

php 搞代码 4年前 (2022-01-22) 12次浏览 已收录 0个评论

本篇文章主要介绍了yii2实现分页,带搜索的分页功能示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

一、模型配置

事例会用到三个models。文章类别表和文章表用gii生成下即可,最后一个是搜索验证模型。其中,只讲下一个联表和搜索验证。其他不用操作。

1.文章表关联

<?php//...other code//关联public function getCate(){    return $this->hasOne(ArticleCate::className(),['id' => 'cid']);  }?>

2.搜索模型

common/models/search/创建ArticleSearch.php

<?phpnamespace common\models\search;use Yii;use yii\base\Model;use yii\data\ActiveDataProvider;use common\models\Article;class ArticleSearch extends Article{  //public $cname;//文章类别名    /**   * @inheritdoc   */  public function rules()  {    return [      [['cid','created_at', 'updated_at'], 'integer'],      [['id', 'desc','title','cover','content'], 'safe'],    ];  }  /**   * @inheritdoc   */  public function scenarios()  {    // bypass scenarios() implementation in the parent class    return Model::scenarios();  }  //搜索  public function search($params)  {    $query = Article::find();    // $query->joinWith(['cate']);//关联文章类别表    // $query->joinWith(['author' => function($query) { $query->from(['author' => 'users']); }]);    $dataProvider = new ActiveDataProvider([      'query' => $query,      'pagination' => [        'pageSize' => 2,      ],    ]);    // 从参数的数据中加载过滤条件,并验证    $this->load($params);    if (!$this->validate()) {      // uncomment the following line if you do not want to any records when validation fails      // $query->where('0=1');      return $dataProvider;    }    // 增加过滤条件来调整查询对象    $query->andFilterWhere([      // 'cname' => $this->cate.cname,      'title' => $this->title,    ]);    $query->andFilterWhere(['like', 'title', $this->title]);    //$query->andFilterWhere(['like', 'cate.cname', $this->cname]) ;    return $dataProvider;  }}

二、分页使用

方式一

首先在控制器的动作中,创建分页对象并且为其填充数据:

<?php//other codeuse yii\data\Pagination;public function actionArticlelist()  {    //分页读取类别数据    $model = Article::find()->with('cate');    $pagination = new Pagination([      'defaultPageSize' => 3,      'totalCount' => $model->count(),    ]);    $model = $model->orderBy('id ASC')      ->offset($pagination->offset)      ->limit($pagination->limit)      ->all();    return $this->render('index', [      'model' => $model,      'pagination' => $pagination,    ]);  }?>

其次在视图中我们输出的模板为当前页并通过分页对象链接到该页:

<?phpuse yii\widgets\LinkPager;use yii\helpers\Html;use yii\helpers\Url;//other codeforeach ($models as $model) {  // 在这里显示 $model}// 显示分页echo LinkPager::widget([  'pagination' => $pagination,  'firstPageLabel'=>"First",  'prevPageLabel'=>'Prev',  'nextPageLabel'=>'Next',  'lastPageLabel'=>'Last',]);?>

方式二

控制器:

<?php    $query = Article::find()->with('cate');    $provider = new ActiveDataProvider([      'query' => $query,      'pagination' => [        'pageSize' => 3,      ],      'sort' => [        'defaultOrder' => [          //'created_at' => SORT_DESC,       <i style="color:transparent">@本文来源gaodai$ma#com搞$代*码6网</i><b>搞代gaodaima码</b>   //'title' => SORT_ASC,        ]      ],    ]);    return $this->render('index', [      'model' => $query,      'dataProvider' => $provider    ]);?>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:yii2实现分页和带搜索的分页功能

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址