dynamic "deleteconfirmation" in the CButtonColumn ?

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 ?

It is a bit complex, I know :)

$(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

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

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.

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

i tried to use

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? :( :(

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.yiiframework.com/forum/index.php?/topic/14184-

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

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.

@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…

Hi mdomba,

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.

Anyway, thanks for your hint:

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!

Glad you solved it… but 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?

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…

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?

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…

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;

});

Works perfectly with ‘live’. Thank you.

Instead of multiple calls to parent(), you can also make a single call to parents(), passing ‘tr’ as the selector, so instead of this, which would take the text from the 4th column of the table (the :eq selector uses 0-based numbering):


'deleteConfirmation'=>"js:'Are you sure you want to delete the user ' +

	$(this).parent().parent().children(':eq(3)').text() +

	'? This operation cannot be undone.\\n\\nPress \"OK\" to delete, or \"Cancel\" to abort without deleting.\\n\\n'",

You could use this:


'deleteConfirmation'=>"js:'Are you sure you want to delete the user ' +

	$(this).parents('tr').children(':eq(3)').text() +

	'? This operation cannot be undone.\\n\\nPress \"OK\" to delete, or \"Cancel\" to abort without deleting.\\n\\n'",

Dan

Will it also be problem, if all AJAX calls of CGridView (sorting, pagination) will be changed to normal POST calls?

I mean - is it truly only a problem of reloading CGridView after AJAX call or maybe this little trick interference somehow, how CGridView works at all?

"problem" is only on ajax calls… becasue the ajax call loads a completely new grid and replaces the current one…

if you use POST or GET (without ajax)… then a completely new page is loaded… new jQuery events are rendered/attached and all is working

but if you use ajax… the new page/sort/whatever is returned from the ajax call… and with jQuery the current page grid is replaced by the returned grid… so everything on the current page remains as is… only the grid is replaced… no new events are binded or new jQuery code is rendered… there is only the old one…

if you use the classic jQuery.click(…) method… it does work only on the original grid… that’s why it does not work after that original grid is replaced by a new one…

but as per jQuery documentation… live() and delegate() works even for future elements… so if you use live()… it will work even after the grid has being replaced by a new one…

I hope it’s more clear now…

if not… there is no other way then get your hands a bit dirty with jQuery… without Yii… just HTML and jQuery… and experiment a bit… read some books on it…