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

时间:2013-11-18 18:07:36

标签: php yii frameworks

我在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”时打开警报,但我无法在'click'中插入php代码。那么有可能这样做吗?

1 个答案:

答案 0 :(得分:1)

来自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'选项的PHP表达式和/或'visible'选项中,变量$ 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");}',
                            ),
                        ),
        ),