我試圖在CGridView查看或顯示按鈕(CButtonColumn),在我/views/file/admin.php:Yii - 如何基於用戶級別隱藏/查看CGridview中的按鈕?
....
//getLevel()==1 means ADMIN, getLevel==2 means common users
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{view}{update}{delete}', 'visible'=> (Yii::app()->user->getLevel()==1),
'deleteConfirmation'=>"js: 'Are you want to delete '+$(this).parent().parent().children(':first-child').text()+ '?'",
//I tried to modify with this code below, but there's nothing happens, 'view' button not
//display when I access as common user (getLevel()==2)
'buttons'=>array(
'view' => array(
'visible'=> Yii::app()->user->getLevel()==2,
),
)
),
....
WebUser.php
<?php
class WebUser extends CWebUser{
protected $_model;
protected function loadUser()
{
if ($this->_model === null) {
$this->_model = User::model()->findByPk($this->id);
}
return $this->_model;
}
function getLevel()
{
$user=$this->loadUser();
if($user)
return $user->id_level;
return 100;
}
}
?>
我想這樣做的代碼,但「查看」按鈕不顯示,並且當我加載頁面時沒有錯誤。任何人都可以幫我解決這個問題嗎?非常感謝。
嘗試將此語句放在逗號'Yii :: app() - > user-> getLevel()== 2'中。然後看看它是否工作 –
我已經嘗試過它,但它引發錯誤'call_user_func_array()期望參數1是一個有效的回調,沒有數組或字符串給出' – gultomicro
好吧你確定getLevel()返回正確的結果嗎? –