1

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

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

有没有办法通过自定义标签为ENUMYii 中的数据库表列提供排序和过滤(使用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(
                )
            ),
        ),
    ),
));
4

2 回答 2

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(
                )
            ),
        ),
    ),
));

另请查看文档

于 2015-07-17T10:53:36.133 回答
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(),
    ),
  )
));

就这样

于 2015-07-17T10:58:31.303 回答