Yii Framework Forum: dynamic "deleteconfirmation" in the CButtonColumn ? - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

dynamic "deleteconfirmation" in the CButtonColumn ? Rate Topic: -----

#1 User is offline   karim gioca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 83
  • Joined: 21-September 10

Posted 03 December 2010 - 03:34 AM

Hi everybody,

can someone tell me how to display the $data in the button delete of the CGridview? i tryed to do that but i found that this is impossible because the "deleteconfirmation" zone is evalueted as a JS script not a PHP script. I explain my problem better :
this is my gridview :

$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'esbilancio-grid',
	'dataProvider'=>$dataProvider,
	'emptyText' => 'Nessun contenuto',
	'summaryText'=>'Totale {count} risultati',
	'columns'=>array(
		'anno_nome',
		array(
			'class'=>'CButtonColumn',
			'template'=>'{update}{delete}',
			'deleteButtonLabel' => 'Cancella',
			'deleteConfirmation' => 'Attenzione: L\'eliminazione di anno [color="#FF0000"]$data->anno_name[/color] porterà i file abbinati a quell\'anno nella categoria generica "Archivio bilanci". L\'operazione non è reversibile. Desideri continuare?',
			'updateButtonLabel' => 'Modifica',
		),
	),
)); 



The problem that the "'deleteConfirmation'" is not evaluated as a php script so it doesn't display the dynamic content.

How can i get the content of the $data in this case ?

if there is a better solution to display a confirm message containing the dynamic content i'll be hppy to know it :)
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 03 December 2010 - 04:56 AM

As you stated, this string will not be evaluated by php.

The solution is to create your own delete button and add to the button column.

You have to edit button and template property.

In options of the button you can refer to $data.
0

#3 User is offline   karim gioca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 83
  • Joined: 21-September 10

Posted 06 December 2010 - 03:19 AM

View Postzaccaria, on 03 December 2010 - 04:56 AM, said:

You have to edit button and template property.

In options of the button you can refer to $data.



i have tryed with editing a *my own button but it appear the same problem :
the configuration for additional buttons. Each array element specifies a single button which has the following format:
'buttonID' => array(
    'label'=>'...',     // text label of the button
    'url'=>'...',       // a PHP expression for generating the URL of the button
    'imageUrl'=>'...',  // image URL of the button. If not set or false, a text link is used
    'options'=>array(...), // HTML options for the button tag
    [color="#FF0000"]'click'=>'...',     // a JS function to be invoked when the button is clicked[/color]
    'visible'=>'...',   // a PHP expression for determining whether the button is visible
)
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. 


This is what manchened in documentation and in red (click) is where i'm supposed to put the "confirm(massage)" in JS, and in this attribute (click that i mean) all what is putted there is evaluated as a js not php.

my purpose is to display the $data in this confirm message.

I think it's not possible with the "buttons" :(

is there and other class that make me do this ?

i dont want to use Clinkcolumn because i want that the delete request is done by ajax way (i mean refresh the gridview and not the hole page)
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,336
  • Joined: 12-October 09
  • Location:Croatia

Posted 06 December 2010 - 04:15 AM

You can use jQuery to get the value of the first column to be displayed in the confirmation text...

this jQuery code will get the value of the first column: $(this).parent().parent().children(':first-child').text()

and you can use it like:

...
'deleteConfirmation'=>"js:'Attenzione: L\'eliminazione di anno '+$(this).parent().parent().children(':first-child').text()+' porterà i file abbinati a quellanno nella categoria generica \"Archivio bilanci\". L\'operazione non è reversibile. Desideri continuare?'",
...


NOTE: the "js:" at the beginning... and the proper use of double and single quotes...
Find more about me.... btw. Do you know your WAN IP?
1

#5 User is offline   karim gioca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 83
  • Joined: 21-September 10

Posted 06 December 2010 - 05:01 AM

View Postmdomba, on 06 December 2010 - 04:15 AM, said:

...
'deleteConfirmation'=>"js:'Attenzione: L\'eliminazione di anno '+$(this).parent().parent().children(':first-child').text()+' porterà i file abbinati a quellanno nella categoria generica \"Archivio bilanci\". L\'operazione non è reversibile. Desideri continuare?'",
...


NOTE: the "js:" at the beginning... and the proper use of double and sigle quotes...


Thanx it works !! :)
but excuse me i didn't understand exactly this code
$(this).parent().parent().children(':first-child').text()


it returns the parent of the parent and then ... and how can he get the "text()" function get the dynamic content in each row ?
0

#6 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,336
  • Joined: 12-October 09
  • Location:Croatia

Posted 06 December 2010 - 05:23 AM

It is a bit complex, I know :)

Quote

$(this).parent().parent().children(':first-child').text()

$(this) - When a user "click" on the delete icon you get here the jQuery object of the clicked link

$(this).parent() - is the table column that contains the delete button

$(this).parent().parent() - is the row in which the delete button has been clicked

$(this).parent().parent().children() - gets all columns of the row where the delete icon has been clicked

$(this).parent().parent().children(':first-child') - gets the first column of the row where the delete icon has been clicked

$(this).parent().parent().children(':first-child').text() - gets the value of the first column

Note that text() is a jQuery function nor PHP... it gets the value of a jQuery object... in this case the content of the first column in a table row
Find more about me.... btw. Do you know your WAN IP?
3

#7 User is offline   karim gioca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 83
  • Joined: 21-September 10

Posted 06 December 2010 - 06:02 AM

View Postmdomba, on 06 December 2010 - 05:23 AM, said:

It is a bit complex, I know :)

....

Note that text() is a jQuery function nor PHP... it gets the value of a jQuery object... in this case the content of the first column in a table row


Thanx so much :)
It's a greate solution and i appreciate your help :). I have archived this solution as one of my ways of cheats :P
0

#8 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,193
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 15 December 2010 - 07:36 AM

I will only add that in this post you will find example, how to read contents of other column than first and use in in delete confirmation message box.
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#9 User is offline   logos010 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 12-November 10
  • Location:VietNam

Posted 23 December 2010 - 06:19 AM

I hvae same problem, but in my case, i use to update ajax function,
i tried to use

Quote

$(this).parent().parent().children(':first-child').text()

but it same not work in my code! I explain my problem better :
array(
                'class' => 'CButtonColumn',
                'header' => 'Actions',
                'template' => '{update}',
                'buttons' => array(
                    'update' => array(
                        'url' => '"#"',
                        'label' => 'Edit',
                        'click' => "alert(js:$(this).parent().parent().children(':nth-child(2)').text())"
                    ),
                ),

It just get error, i don't know how to solve this problem, can anyone help me? :( :(
0

#10 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,336
  • Joined: 12-October 09
  • Location:Croatia

Posted 23 December 2010 - 06:35 AM

NOTE: "js:" when used... should be used only used as a start of the string not inside the string...

'click' needs a callback function to be invoked... check this thread for an example - http://www.yiiframew...p?/topic/14184-
Find more about me.... btw. Do you know your WAN IP?
0

#11 User is offline   rei 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 318
  • Joined: 10-November 10

Posted 27 December 2010 - 03:25 PM

Frankly speaking, I'm really confused about this ???

Quote

$(this).parent().parent().children(':first-child').text()


I would like to know how I can use this in 'selectionChanged'. I have something like this:

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'distributor-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'selectionChanged'=>"function(id){
			window.opener.document.subcontractor.distributor_code.value = $(this).parent().parent().children(':nth-child(2)').text();
		}",	
	'selectableRows'=>1,
	'columns'=>array(
		array(
			'class'=>'CCheckBoxColumn',
		),
		'distributor_code',
		'name',
		'city',
	),
)); ?>



Could someone help?

Thank you.
0

#12 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,336
  • Joined: 12-October 09
  • Location:Croatia

Posted 27 December 2010 - 03:32 PM

@rei

Can you explain what you want to do?

For the explanation of the "complex" jQuery read the comment #6 on this thread... you will see that this line works only for a click on a button in the CButtonColumn... as you are using selectionChanged... you don't know where the user has clicked as he can click anywhere on the row...
Find more about me.... btw. Do you know your WAN IP?
1

#13 User is offline   rei 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 318
  • Joined: 10-November 10

Posted 27 December 2010 - 10:22 PM

Hi mdomba,

Quote

Can you explain what you want to do?


I'm so perplexed that I forgot to explain about that :).

Basically, I want to pass the content of the second column (in the selected row) to textfield in another window.

So, when user checks the checkbox, the 'distributor id' column content (of the selected row) will be passed to distributor id textfield in another window.
0

#14 User is offline   rei 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 318
  • Joined: 10-November 10

Posted 27 December 2010 - 10:28 PM

Anyway, thanks for your hint:

Quote

as you are using selectionChanged... you don't know where the user has clicked as he can click anywhere on the row...


So I change it to:

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'distributor-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'selectableRows'=>1,
	'columns'=>array(
		array(
			'class'=>'CCheckBoxColumn',
			'checkBoxHtmlOptions'=>array('onclick'=>"window.opener.document.subcontractor.distributor_code.value = $(this).parent().parent().children(':nth-child(2)').text();")
		),
		'distributor_code',
		'name',
		'city',
	),
)); ?>


Now it works. Thanks again!

This post has been edited by rei: 28 December 2010 - 02:44 AM

0

#15 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,336
  • Joined: 12-October 09
  • Location:Croatia

Posted 28 December 2010 - 01:52 AM

Glad you solved it... but note that to select a row... you can click anywhere on the row... not only on the checkbox...
Find more about me.... btw. Do you know your WAN IP?
0

#16 User is offline   rei 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 318
  • Joined: 10-November 10

Posted 28 December 2010 - 03:14 AM

Quote

note that to select a row... you can click anywhere on the row... not only on the checkbox.


Could that be achieved? I mean we click anywhere on row and get value from the second column of that row?
0

#17 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,336
  • Joined: 12-October 09
  • Location:Croatia

Posted 28 December 2010 - 03:55 AM

Of course it can be done...

My way of doing it is by using pure jQuery... you register a script like this...

Yii::app()->clientScript->registerScript('dc', "
$('#document-grid table tbody tr').click(function()
{
	$('#dc').val($(this).children(':nth-child(2)').text());
});
");


where "document-grid" is the ID of the CGridView and "dc" is the ID of the textfield

Example textfield :
echo CHtml::textField('distributor_code',$distributor_code,array('id'=>'dc'));


This way in the click event function $(this) is always the current row... and we just need the 2nd child of that row...
Find more about me.... btw. Do you know your WAN IP?
2

#18 User is offline   rei 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 318
  • Joined: 10-November 10

Posted 28 December 2010 - 08:09 AM

Wow! Looks very cool. Thanks a million for giving a detailed example, mdomba.

Edit: It works when page loads for the first time. Anyway, I notice that this event is not triggered after I do some filtering (search function) in CGridView. Any idea?

This post has been edited by rei: 28 December 2010 - 08:50 AM

0

#19 User is offline   rei 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 318
  • Joined: 10-November 10

Posted 28 December 2010 - 10:23 AM

Is it possible to have conflict with the 'search' script?

Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$.fn.yiiGridView.update('distributor-grid', {
		data: $(this).serialize()
	});
	
	return false;
});
");


Since that script works fine before I do filtering...
0

#20 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,336
  • Joined: 12-October 09
  • Location:Croatia

Posted 28 December 2010 - 10:55 AM

It's becasue after an ajax call (sort/pagination/filtering) the gridview/listview gets reloaded...

I cannot test it right now... Try to use live or delegate instead of ".click"

example:
$('.search-button').live('click', function(){
        $('.search-form').toggle();
        return false;
});

Find more about me.... btw. Do you know your WAN IP?
3

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users