Displaying model data in CGridView's delete button's confirmation

Hi there,

I have two problems extending CButtonColumn and would appreciate any help as I found no solution in documentation. I’ve separated both problems into two topic as they are quite different and one post holding both of them become to long - no one would like to read it! :] Second problem has been described in this post.

[b]

[/b]I want to make delete button in CGridView behave exactly as delete menu item on view / update views generated by Gii for CRUD. But how to achieve this?

It is written in documentation that: “In the PHP expression for the ‘url’ option and/or ‘visible’ option, the variable $row refers to the current row number (zero-based), and $data refers to the data model for the row”. Why the same isn’t available for any other CButtonColumn property? For me, the easiest way would be to use code:


array

(

    	'class'=>'CButtonColumn',

    	'deleteConfirmation'=>'Do you really want to delete element with ID = !'.$data->ID."?"

),

Unfortunately this fails with "Undefined variable: data".

Then I tried:


'class'=>'CButtonColumn',

'template'=>'{delete}',

'buttons'=>array

(

	'delete' => array

	(

    	'label'=>'[-]',

    	'url'=>'"#"',

    	'linkOptions'=>array

    	(

        	'submit'=>array('delete', 'id'=>1),

        	'confirm'=>'Delete?'

    	)


	)

),

But this also is not working - default confirmation is show, probably because linkOptions is not an element of button configuration array!

And, again, if I change 1 at the end of submit to:


'submit'=>array('delete', 'id'=>$data->ID),

 'confirm'=>'Delete element ID = '.$data->ID.'?'

I got the same error, saying "Undefined variable: data".

I’m running out of ideas! :confused:

I too had this problem a while ago… the expression is not used for all properties probably because it would affect the speed of CGridView if it would be evaluated for all attributes…

to solve this you need to use jQuery and read the value from a column on the same row()

Check this thread and my solution to it - http://www.yiiframework.com/forum/index.php?/topic/13804-

There has been a similar post not long ago, it seems that both ‘buttons’ and ‘deleteConfirmation’ are not evaluated but simply pasted.

That means that is not possible to use CButtonColumn for do that. You can use CDataColumn and passing lot of buttons like data, that will be evaluated and will work correctly.

@mdomba: Thanks again for a wonderful and extremely fast solution. Of course, it works like a charm!

Since post, you pasted here contains solution not in English (Italian? Croatian?) I’ll paste here English one example for future reference:


array

(

    	'class'=>'CButtonColumn',

    	'deleteConfirmation'=>"js:'Record with ID '+$(this).parent().parent().children(':first-child').text()+' will be deleted! Continue?'",

),

I will also add for others that we can also use jQuery’s ntn-child function for retrieve contents of other column:


array

(

    	'class'=>'CButtonColumn',

    	'deleteConfirmation'=>"js:'Do you really want to delete record with ID '+$(this).parent().parent().children(':nth-child(2)').text()+'?'",

),

@zaccaria: Seems, mdomba proved it is possible with CButtonColumn, but thanks for your reply! :]