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?

Joe
  • 2,551
  • 6
  • 38
  • 60

3 Answers3

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'),            
        ),        
    ),
));
cetver
  • 11,279
  • 5
  • 36
  • 56
  • You cannot define `'data-city-id' => $data->idCity` - it will show you error `Undefined variable: data` – Joe Mar 25 '13 at 12:56
  • 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. – Joe Mar 25 '13 at 13:59
  • `$model->projects` is array. Inside `gridButtons` check if empty `$model->projects` and is not you can access it `$model->projects[0]->id` – cetver Mar 25 '13 at 14:14
  • I tried that to, but it says `htmlspecialchars() expects parameter 1 to be string, array given ` – Joe Mar 25 '13 at 14:29
0

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

'onclick' => "js:removeCity(this, {$data->idCity}, {$model->idProject}); return false;",
Michael Härtl
  • 8,428
  • 5
  • 35
  • 62
  • It will result in syntax error - you cannot do sth like `${data->idCity}` (`->` will be unexpected). – Joe Mar 26 '13 at 08:00
  • @Joe: That's not true. Please read the section "Complex (curly) syntax" on this page: http://www.php.net/manual/en/language.types.string.php. EDIT: Fixed the typo: the `$` must be inside the `{}`. – Michael Härtl Mar 26 '13 at 08:06
  • I agree you won't get syntax error when move `$` in between `{}` but then - you will get `Undefined variable: data` error. I checked it :) – Joe Mar 26 '13 at 08:26
  • 1
    Oh, right. The variables are only availabel for some of the properties, mainly the URL related. – Michael Härtl Mar 26 '13 at 08:47
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; }',

            )
        ),
    )
),
));
zaman
  • 145
  • 7