Yii CGridView按钮 - 调用参数化的javascript函数

时间:2013-03-25 12:41:34

标签: php javascript yii cgridview cbuttoncolumn

我需要用一个按钮创建一个CGridView并使按钮调用javascript函数如下:

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'projectCities',
    'summaryText' => '',
    'dataProvider' => $model->getCitiesProvider(),
    'columns' => array(
        'name',
        'directional',
        'customCount',
        array(
            'class'=>'CButtonColumn',
            'template'=>'{delete}',
            'buttons' => array(
                'delete' => array(
                    'url' => '',
                    'click' => '',
                    'options' => array(
                        'onclick' => 'removeCity(this, $data->idCity, 
                                      $model->idProject); return false;',
                    ),                          
                )
            ),
        )
    ),
    ));

当然它不起作用,因为生成的html是:

<a class="delete" title="Delete" onclick="removeCity(this, $data->idCity, $model->idProject); return false;">

有没有办法让javascript函数调用中有正确的id?

3 个答案:

答案 0 :(得分:2)

//Controller:
public function gridButtons($model)
{   
    return array(
        'class'=>'CButtonColumn',
        'template'=>'{delete}',
        'buttons' => array(
            'delete' => array(
                'url' => '',
                'click' => '',
                'options' => array(
                    'onclick' => sprintf(
                        'js:removeCity(this, %d, %d);return false;',
                        $model->idCity, $model->idProject
                    ),
                ),                          
            )
        ),
    )
}
//view
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'projectCities',
    'summaryText' => '',
    'dataProvider' => $model->getCitiesProvider(),
    'columns' => array(
        'name',
        'directional',
        'customCount',
        array(            
            'value' => array($this, 'gridButtons'),            
        ),        
    ),
));

答案 1 :(得分:0)

您可以使用双引号在字符串中激活变量替换:

'onclick' => "js:removeCity(this, {$data->idCity}, {$model->idProject}); return false;",

答案 2 :(得分:0)

我遇到了这个问题并且也解决了这个问题。可能你的问题是在'click'参数之后将'选项'数组关闭,其中js函数已经停留。但是'选项'数组在这之前的'click'参数之前关闭。请看这个,可能这个解决问题对你有帮助。

$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'projectCities',
'summaryText' => '',
'dataProvider' => $model->getCitiesProvider(),
'columns' => array(
    'name',
    'directional',
    'customCount',
    array(
        'class'=>'CButtonColumn',
        'template'=>'{delete}',
        'buttons' => array(
            'delete' => array(                      
       'url'=>'$data->id',
       'visible'=>'true',
       'options'=>array('class'=>'viewbtns'),
       'click'=>'js: function(){ viewProfile((this).attr("href"),"openDialog" ); return false; }',

            )
        ),
    )
),
));