Joe
Joe

Reputation: 2591

Yii CGridView button - call parametrized javascript function

I need to create a CGridView with one button and make the button call javascript function like this:

$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;',
                    ),                          
                )
            ),
        )
    ),
    ));

Ofcourse it's not working, because the generated html is:

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

Is there a way to do it so there will be proper id in the javascript function call?

Upvotes: 1

Views: 7902

Answers (3)

zaman
zaman

Reputation: 145

I facing this problem and also solve this.May be your problem is going to 'option' array close after 'click' parameter where js function have staying.But 'option' array close before 'click' parameter below this way.Please see this,may be this solve problem are help to you.

$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; }',

            )
        ),
    )
),
));

Upvotes: 0

Michael H&#228;rtl
Michael H&#228;rtl

Reputation: 8587

You can use double quotes to activate variable replacement in your string:

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

Upvotes: 0

cetver
cetver

Reputation: 11829

//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'),            
        ),        
    ),
));

Upvotes: 2

Related Questions