这篇文章介绍的内容是关于yii2实现分页,带搜索的分页功能示例,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
事例会用到三个models。文章类别表和文章表用gii生成下即可,最后一个是搜索验证模型。其中,只讲下一个联表和搜索验证。其他不用操作。
1.文章表关联
<?php
//...other code
//关联
public function getcate(){
return $this->hasone(articlecate::classname(),['id' => 'cid']);
}
?>
2.搜索模型
common/models/search/创建articlesearch.php
<?php
namespace 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 code
use 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,
]);
}
?>
其次在视图中我们输出的模板为当前页并通过分页对象链接到该页:
<?php
use yii\widgets\linkpager;
use yii\helpers\html;
use yii\helpers\url;
//other code
foreach ($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,
//'title' => sort_asc,
]
],
]);
return $this->render('index', [
'model' => $query,
'dataprovider' => $provider
]);
?>
视图:
<?php
use yii\grid\gridview;
echo gridview::widget([
'dataprovider' => $dataprovider,
//每列都有搜索框 控制器传过来$searchmodel = new articlesearch();
//'filtermodel' => $searchmodel,
'layout'=> '{items}<p class="text-right tooltip-demo">{pager}</p>',
'pager'=>[
//'options'=>['class'=>'hidden']//关闭自带分页
'firstpagelabel'=>"first",
'prevpagelabel'=>'prev',
'nextpagelabel'=>'next',
'lastpagelabel'=>'last',
],
'columns' => [
//['class' => 'yii\grid\serialcolumn'],//序列号从1开始
// 数据提供者中所含数据所定义的简单的列
// 使用的是模型的列的数据
'id',
'username',
['label'=>'文章类别', /*'attribute' => 'cid',产生一个a标签,点击可排序*/ 'value' => 'cate.cname' ],
['label'=>'发布日期','format' => ['date', 'php:y-m-d'],'value' => 'created_at'],
// 更复杂的列数据
['label'=>'封面图','format'=>'raw','value'=>function($m){
return html::img($m->cover,['class' => 'img-circle','width' => 30]);
}],
[
'class' => 'yii\grid\datacolumn', //由于是默认类型,可以省略
'value' => function ($data) {
return $data->name;
// 如果是数组数据则为 $data['name'] ,例如,使用
sqldataprovider 的情形。
},
],
[
'class' => 'yii\grid\actioncolumn',
'header' => '操作',
'template' => '{delete} {update}',//只需要展示删除和更新
/*'headeroptions' => ['width' => '80'],*/
'buttons' => [
'delete' => function($url, $model, $key){
return html::a('<i class="glyphicon glyphicon-trash"></i> 删除',
['artdel', 'id' => $key],
['class' => 'btn btn-default btn-xs',
'data' => ['confirm' => '你确定要删除文章吗?',]
]);
},
'update' => function($url, $model, $key){
return html::a('<i class="fa fa-file"></i> 更新',
['artedit', 'id' => $key],
['class' => 'btn btn-default btn-xs']);
},
],
],
],
]);
?>
三、搜索带分页功能
创建搜索模型(前面己做)
控制传入数据
视图显示控制器代码:
<?php
public function actionindex()
{
$searchmodel = new articlesearch();
$dataprovider = $searchmodel->search(yii::$app->request->queryparams);
return $this->render('index', [
'searchmodel' => $searchmodel,
'dataprovider' => $dataprovider,
]);
}
?>
视图:
<?php $form = activeform::begin([
'action' => ['index'],
'method' => 'get',
'id' => 'cateadd-form',
'options' => ['class' => 'form-horizontal'],
]); ?>
<?= $form->field($searchmodel, 'title',[
'options'=>['class'=>''],
'inputoptions' => ['placeholder' => '文章搜索','class' => 'input-sm form-control'],
])->label(false) ?>
<?= html::submitbutton('go!', ['class' => 'btn btn-sm btn-primary']) ?>
<?php activeform::end(); ?>
<?= gridview::widget([
'dataprovider' => $dataprovider,
'layout'=> '{items}<p class="text-right tooltip-demo">{pager}</p>',
'pager'=>[
//'options'=>['class'=>'hidden']//关闭自带分页
'firstpagelabel'=>"first",
'prevpagelabel'=>'prev',
'nextpagelabel'=>'next',
'lastpagelabel'=>'last',
],
//这部分和上面的分页是一样的
相关推荐:
yii实现model添加默认值的方法
yii实现的多级联动下拉菜单
yii2实现表单上传文件功能
以上就是yii2实现分页,带搜索的分页功能示例的详细内容。