如何更改CRUD的默认页面大小? YII框架默认显示10个结果,如何更改显示限制?
controllers/UsersController.php
class UsersController extends Controller{
public $layout='//layouts/column2';
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
);
}
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
public function actionDelete($id)
{
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
public function actionIndex()
{
$model=new Users('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Users']))
$model->attributes=$_GET['Users'];
$this->render('admin',array(
'model'=>$model,
));
}
public function loadModel($id)
{
$model=Users::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='users-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
view/users/admin.php
/* @var $this UsersController */
/* @var $model Users */
$this->breadcrumbs=array(
'Users'=>array('index'),
'Manage',
);
$this->menu=array(
array('label'=>'List Users', 'url'=>array('index')),
array('label'=>'Create Users', 'url'=>array('create')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$('#users-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Manage Users</h1>
<p>
You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
'pass',
'config',
array(
'class'=>'CButtonColumn',
),
),
));
修改:
如何只选择我们录制的前5个?
答案 0 :(得分:0)
在您的模型中,更改search()
方法,返回CActiveDataProvider:
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination' => array(
'pageSize' => 40,
),
));
答案 1 :(得分:0)
在Users
模型中,检查search()
方法
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('pass',$this->pass,true);
.......
.......
.......
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
您可以在此处设置/配置pagination
属性。 pagination
是CActiveDataProvider
类的属性,它可以返回CPagination类的对象。
您可以设置页面大小
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array('pageSize' => 20),
));
或
$pageSize=20;
$pagination = new CPagination($pageSize);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>$pagination
));
答案 2 :(得分:0)
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['activity_id' => SORT_DESC]],
'pagination' => [
'pageSize' => 10,
],
]);