1

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?

3 Answers 3

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'),            
        ),        
    ),
));
Sign up to request clarification or add additional context in comments.

4 Comments

You cannot define 'data-city-id' => $data->idCity - it will show you error Undefined variable: data
Good work. It is pretty close I guess. I have the problem with $model->idProject. I guess it's because Cities and Projects tables are related with many-to-many relationship. So I'm getting Property "Cities.idProject" is not defined.. I tried to access the table using ORM: $model->projects->idProject but I got Trying to get property of non-object error. That is weird - if there was no project assigned to the city - it wouldn't appear.
$model->projects is array. Inside gridButtons check if empty $model->projects and is not you can access it $model->projects[0]->id
I tried that to, but it says htmlspecialchars() expects parameter 1 to be string, array given
0

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

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

4 Comments

It will result in syntax error - you cannot do sth like ${data->idCity} (-> will be unexpected).
@Joe: That's not true. Please read the section "Complex (curly) syntax" on this page: php.net/manual/en/language.types.string.php. EDIT: Fixed the typo: the $ must be inside the {}.
I agree you won't get syntax error when move $ in between {} but then - you will get Undefined variable: data error. I checked it :)
Oh, right. The variables are only availabel for some of the properties, mainly the URL related.
0

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

            )
        ),
    )
),
));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.