如何在Yii中为ENUM columnt实现CGridView表的过滤?

时间:2015-07-17 10:03:26

标签: mysql yii enums cgridview yii-cactiverecord

在Yii应用程序中,我将数据显示为表格。我使用CGridView来处理(基于搜索的)数据过滤和排序。它有效。

现在我在ENUM数据库表中添加了statususer。在网格中,我可以对此进行排序和过滤,但只能通过表中的值进行排序和过滤。嗯,这很有道理。但是用户实际上并不知道它是如何在数据库中保存的,并且与标签一起工作(并希望对其进行排序和过滤)。

有没有办法通过Yii中ENUM数据库表列的自定义标签提供排序和过滤(使用CActiveRecord模型和CGridView生成的数据网格)?

数据库表

CREATE TABLE `users` (
    `userid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `email` varchar(45) NOT NULL,
    ...
    `status` enum('processed', 'waiting') NOT NULL,
    PRIMARY KEY (`userid`),
    UNIQUE KEY `email_UNIQUE` (`email`)
);

User模型

/**
 * ...
 * @property string $userid
 * @property string $email
 * ... 
 * @property string $status
 */
class User extends CActiveRecord
{

    public function tableName()
    {
        return 'users';
    }

    public function getFirstAndLastName(){
        return CHtml::encode($this->firstname." ".$this->lastname);
    }

    public function rules()
    {
        return array(
            ...
            array('userid, email, status', 'safe', 'on'=>'search'),
        );
    }

    public function attributeLabels()
    {
        return array(
            'userid' => 'Userid',
            'email' => 'Email',
            ...
            'status' => Yii::t('app', 'status'),
        );
    }

    public function relations()
    {
        return array(
            ...
        );
    }

    public function search()
    {
        $criteria=new CDbCriteria;

        $criteria->compare('userid',$this->userid,true);
        $criteria->compare('email',$this->email,true);
        ...
        $criteria->compare('status',$this->status,true);

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

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    protected function beforeSave()
    {
        return parent::beforeSave(); // TODO: Change the autogenerated stub
    }

    public function getProcessingStatus()
    {
        return Yii::t('app', $this->processing_status);
    }

}

UserController

class UserController extends Controller
{
    ...

    /**
     * Manages all models.
     */
    public function actionAdmin()
    {
//        Yii::app()->cache->flush();
        $model=new User('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['User']))
            $model->attributes=$_GET['User'];

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

    ...

}

查看

...

<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'user-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        'email',
        'status',
        array(
            'class' => 'CButtonColumn',
            'template' => '{view}{update}',
            'buttons' => array(
                'view' => array(
                )
            ),
        ),
    ),
));

2 个答案:

答案 0 :(得分:1)

您可以通过将CDataColumn属性设置为数组,在filter中指定自定义过滤器。它将导致下拉列表过滤器而不是文本字段过滤器。

示例:

<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'user-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        'email',
        array(
            'name' => 'status',
            'filter' => array(
                'processed' => 'Processed',
                'waiting' => 'Waiting',
            ),
        ),
        array(
            'class' => 'CButtonColumn',
            'template' => '{view}{update}',
            'buttons' => array(
                'view' => array(
                )
            ),
        ),
    ),
));

另请查看docs

答案 1 :(得分:1)

型号:

class User extends CActiveRecord {

  const STATUS_PROCESSED = 'processed';
  const STATUS_WAITING = 'waiting';

  public static function getStatusList(){
    return array(
      self::STATUS_PROCESSED => 'Processed',
      self::STATUS_WAITING => 'Waiting',
    );
  }

  public function getStatusValue(){
    $list = self::getStatusList();
    return array_key_exists( $this->status, $list ) ? $list[ $this->status ] : 'Undefined';
  }
}

查看:

$this->widget('zii.widgets.grid.CGridView', array(
  // other params
  'columns' => array(
    // other columns
    array(
      'name' => 'status',
      'value' => '$data->getStatusValue()',
      'filter' => User::getStatusList(),
    ),
  )
));

全部