Yii:如果变量等于0图标作为链接,则图标打开警报


Yii: If variable is equal 0 icon as link, else icon open alert

我在CGridView:中有CButtonColumn

array(
            'class' => 'CButtonColumn',
                        'template'=>'{document}',
                        'buttons'=>array(
                            'document'=>array(
                                'imageUrl'=>'icon.gif',
                                'url' => '($data->status=="1") ? Yii::app()->createUrl("site/getDocument") : "" ',
                                'click' => 'js:function() { alert("There isn't file");}'
                            ),
                        ),
        ),

但我想打开将打开警报只有在$data->status=="1"时,但我不能在"点击"中插入php代码。那么有可能这样做吗?

来源http://www.yiiframework.com/doc/api/1.1/CButtonColumn:

'buttonID' => array(
    'label'=>'...',     // text label of the button
    'url'=>'...',       // a PHP expression for generating the URL of the button
    'imageUrl'=>'...',  // image URL of the button. If not set or false, a text link is used
    'options'=>array(...), // HTML options for the button tag
    'click'=>'...',     // a JS function to be invoked when the button is clicked
    'visible'=>'...',   // a PHP expression for determining whether the button is visible
)

"在'url'选项和/或'visible'选项的PHP表达式中,变量$row表示当前行号(从零开始),$data表示该行的数据模型。PHP表达式可以是任何有值的PHP代码。"您可以访问DOM上的元素以获取所需值。

你可以试试这个:

array(
      'class' => 'CButtonColumn',
            'template'=>'{document}',
            'buttons'=>array(
                        'document'=>array(
                        'imageUrl'=>'icon.gif',
                        'url' => '($data->status=="1") ? Yii::app()->createUrl("site/getDocument") : "" ',
                'click' => 'function() { if($(this).attr("href") !== "") alert("There isn't file");}',
                            ),
                        ),
        ),