简体   繁体   中英

Yii CGridview hide rows

我的yii CGridview中有三行,并且有两种类型的用户登录。我必须隐藏三行中的一行,具体取决于用户的类型。请帮助。

Use the Conditional statements like this:

public function newsearch()
{
    $id= Yii::app()->user->id;
    if($id = Your conditon){
        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        Your Criteria to display
    } else {
        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        Your Criteria to display
    }
    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

And Call this dataprovider in your GridView data provider.

in your model add public static method for example:

class Post extends CActiveRecord {

    public function tableName() {

        return 'posts';
    }

    public function rules() {

        return array();
    }

    public function attributeLabels() {

        return array();
    }

...

    public static function rulesUser() {
        if ( Yii::app()->user->id = 1 ) {
            return True;
        } else {
            return False;
    }  
}

add in your gridview for row:

$this->widget('zii.widgets.grid.CGridView', 
    array(
'id' => 'posts-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'emptyText' => '',
'columns' => array(
        'id',
        'title',
        'post',
        'date_create',
                    array(
                    'name' => 'status',
                    'visible' => Posts::rulesUser(),
                     )
        array(
        'class' => 'CButtonColumn',
        ),
    ),
    )
);

or add in model

public $visible = True;

in you search add if or switch

public function search()
{
$criteria = new CDbCriteria;
     ...
    if (any if){
$this->visible = 1;
    }
return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
}

in your gridview

...

array(              
'name'     => 'value',
'visible'=>$model->visible,
)

...

I have solved the task by conditioning the CDbCriteria as follows,

if(!Yii::app()->session["admin"])
{
   $criteria->condition='t.unique_id!="i-8874c6e3"';
} 

Thanks all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM