【问题标题】:how do I change the default pagesize?如何更改默认页面大小?
【发布时间】:2014-05-09 07:25:13
【问题描述】:

如何更改 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>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</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 个?

【问题讨论】:

  • the first 5 we recorded 是什么意思?

标签: php yii frameworks


【解决方案1】:

在您的 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 属性。 paginationCActiveDataProvider类的属性,可以返回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

     ));

【讨论】:

  • 如何只选择我们录制的前5个?
【解决方案2】:

在您的模型中,更改 search() 方法,它返回 CActiveDataProvider:

return new CActiveDataProvider($this, array(
     'criteria'=>$criteria,
     'pagination' => array(
         'pageSize' => 40,
     ),
));

【讨论】:

  • 以及如何只选择我们录制的前 5 个?你有一些创建仪表板的教程吗?我用哪个代替了标准?
【解决方案3】:
 $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort'=> ['defaultOrder' => ['activity_id' => SORT_DESC]],
        'pagination' => [
            'pageSize' => 10, 
        ],
    ]);

【讨论】:

  • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 2017-07-11
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 2017-01-23
相关资源
最近更新 更多