如何使用CGridView为用户显示内容?

时间:2016-09-08 14:52:21

标签: yii yii-extensions

我有一个页面,其中包含我网站上的类别列表,我想使用Cgridview显示它们,因为它们显示在CListView中。我使用的是Yii 1.1。我看到CGridView只在你想要管理内容时使用,但如果你想在索引页面上显示它,是否可以使用它?

<?php 

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'category-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'title',
        'status',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

1 个答案:

答案 0 :(得分:0)

您可以将此代码用于cgridview

Categories Model

文件路径:protected / model / categories.php

<强> Categories.php

<?php

/**
 * This is the model class for table "categories".
 *
 * The followings are the available columns in table 'categories':
 * @property integer $id
 * @property string $title
 * @property string $status
  */
class Categories extends CActiveRecord
{
  /**
   * @return string the associated database table name
   */
  public function tableName()
  {
    return 'area';
  }

  /**
   * @return array validation rules for model attributes.
   */
  public function rules()
  {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
      array('title, status', 'required'),
      array('status', 'numerical', 'integerOnly'=>true),
      array('title', 'length', 'max'=>255),
      // The following rule is used by search().
      // @todo Please remove those attributes that should not be searched.
      array('id, title, status', 'safe', 'on'=>'search'),
    );
  }

  /**
   * @return array relational rules.
   */
  public function relations()
  {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
    );
  }

  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels()
  {
    return array(
      'id' => 'ID',
      'title' => 'Title',
      'status' => 'Status'
    );
  }

  /**
   * Retrieves a list of models based on the current search/filter conditions.
   *
   * Typical usecase:
   * - Initialize the model fields with values from filter form.
   * - Execute this method to get CActiveDataProvider instance which will filter
   * models according to data in model fields.
   * - Pass data provider to CGridView, CListView or any similar widget.
   *
   * @return CActiveDataProvider the data provider that can return the models
   * based on the search/filter conditions.
   */
  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('title',$this->title,true);
    $criteria->compare('status',$this->status);
    return new CActiveDataProvider($this, array(
      'criteria'=>$criteria,
    ));
  }

  /**
   * Returns the static model of the specified AR class.
   * Please note that you should have this exact method in all your CActiveRecord descendants!
   * @param string $className active record class name.
   * @return Area the static model class
   */
  public static function model($className=__CLASS__)
  {
    return parent::model($className);
  }
}

Categories Controller

文件路径:protected / model / CategoriesController.php

<强> CategoriesController.php

<?php

class CategoriesController extends Controller
{
  /**
   * Specifies the access control rules.
   * This method is used by the 'accessControl' filter.
   * @return array access control rules
   * @access Public
   */
    public function accessRules()
    {

        return array(
             array('allow',
                    'actions'   => array('index'),
                    'users'     => array('*'),
             ),
             array('deny',
                   'users'     => array('*'),
             ),
        );
    }
  public function actionIndex()
  {
    $model      = new Categories();
    $model->unsetAttributes();

    if(isset($_REQUEST['Categories']))
      $model->attributes = $_REQUEST['Categories'];

    $this->render('index',array('model'=>$model));
  }
}

Categories View File

文件路径:protected / view / index.php

<强>的index.php

<?php 

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'category-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'title',
        'status',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

您可以在index.php文件中添加您的设计。

refere this link for design