CGridview Ajax Button not storing correct values

refer this and this

Even this simplest form is not working


<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'profileGrid',

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		'Id',

		'ShortTitle',

		'SortOrder',

    array(

      'type'=>'raw',

      'value'=>'CHtml::ajaxLink(CHtml::image("' . Yii::app()->request->baseUrl . '/images/uparrow.png","",array("border"=>0)),Yii::app()->createUrl("/product/reposition"),array(

                "data"=>array(

                        "id"=>$data->Id,

                        "direction"=>"up",

                        "sortOrder" =>$data->SortOrder,

                        ),

                "update"=>"#profileGrid",

            ))." ".

            CHtml::ajaxLink(CHtml::image("' . Yii::app()->request->baseUrl . '/images/downarrow.png","",array("border"=>0)),Yii::app()->createUrl("/product/reposition"),array(

                "data"=>array(

                        "id"=>$data->Id,

                        "direction"=>"down",

                        "sortOrder" =>$data->SortOrder,

                ),

                "update"=>"#profileGrid",

                                

            ));',   

    ),      

Can you tell me why the Id in js script is not referring to the current row Id after the ajax call?

Id is the primary key. But I need to parse in the right Id to the controller to update the new sort order.

I will make this clearer…

First time load


Id | Sort Order | Title

38 | 1 | apple (click down button on this row, controller receive Id as 38, so I can swap the sortorder)

25 | 2 | banana

72 | 3 | citrus

After click, the Ajax refreshed (correctly shown on the grid)


Id | Sort Order | Title

25 | 1 | banana (click down button on this row, controller STILL receive Id as 38, it shouldn’t be 25?)

38 | 2 | apple

72 | 3 | citrus

When the controller still refers to the original Id(38) after the grid is reloaded, I cannot update the "banana(25)" Sort order…

The first time I click on a button on any line, the controller got the right Id. Subsequently after the grid reloaded, click on anywhere gives the same Id as the previous button click. It is as the data->Id is cached or the CDATA script is not updated to reflect the new sort position…

Did I do something wrong?

I think this CDATA is causing the problem as it is not being refreshed.

This is the html source on the first load




<script type="text/javascript">

/*<![CDATA[*/

jQuery(document).ready(function() {

jQuery('#profileGrid a.delete').live('click',function() {

	if(!confirm('Are you sure you want to delete this item?')) return false;

	$.fn.yiiGridView.update('profileGrid', {

		type:'POST',

		url:$(this).attr('href'),

		success:function() {

			$.fn.yiiGridView.update('profileGrid');

		}

	});

	return false;

});

jQuery('#profileGrid').yiiGridView({'ajaxUpdate':['profileGrid'],'ajaxVar':'ajax','pagerClass':'pager','loadingClass':'grid-view-loading','filterClass':'filters','tableClass':'items','selectableRows':1});

jQuery('#yt0').live('click',function(){jQuery.ajax({'data':{'id':'4','direction':'up','sortOrder':'0'},'url':'/myapp/admin.php/product/reposition','cache':false,'success':function(html){jQuery("#profileGrid").html(html)}});return false;});

jQuery('#yt1').live('click',function(){jQuery.ajax({'data':{'id':'4','direction':'down','sortOrder':'0'},'url':'/myapp/admin.php/product/reposition','cache':false,'success':function(html){jQuery("#profileGrid").html(html)}});return false;});

jQuery('#yt2').live('click',function(){jQuery.ajax({'data':{'id':'7','direction':'up','sortOrder':'0'},'url':'/myapp/admin.php/product/reposition','cache':false,'success':function(html){jQuery("#profileGrid").html(html)}});return false;});

jQuery('#yt3').live('click',function(){jQuery.ajax({'data':{'id':'7','direction':'down','sortOrder':'0'},'url':'/myapp/admin.php/product/reposition','cache':false,'success':function(html){jQuery("#profileGrid").html(html)}});return false;});

jQuery('#product-folder-grid a.delete').live('click',function() {

	if(!confirm('Are you sure you want to delete this item?')) return false;

	$.fn.yiiGridView.update('product-folder-grid', {

		type:'POST',

		url:$(this).attr('href'),

		success:function() {

			$.fn.yiiGridView.update('product-folder-grid');

		}

	});

	return false;

});

/*]]>*/

</script>



Subsequent reload do not change any of the #yt0, #yt1 rows…

How to regenerate to CDATA above to reflect the sorted rows after each ajax click?

Will CClientScript help to regenerate? if yes how do I write the registerScript codes?

Thanks.

can someone please confirm if this problem I am facing relates this issue being the jscript is not generated inside the div of the grid?

AjaxLinks with RenderPartial

CJuiDIalog and CGridView with ajaxLink return is set outiside the update div

AjaxLink call CJuiDialog which contains CGridView

Haven’t anyone tried doing a sort up/down button that move rows up and down (through ajax) in the grid before?

I tried even the simplest sample of publish/unpublish button in the posts above, but after the second click, every click thereafter still refers to the Clientscript outside the div of the grid.

I did tried the _renderPartial(‘view’,array(), false, TRUE) it generates clientscript in the div of the grid, but the results are not quite expected, Checking on firebug shows multiple loading of jquery.js resulting in the grid running a few times before it stop.

Other posts of this same issue

http://www.yiiframework.com/forum/index.php?/topic/10210-cgridview-ajax-button-not-storing-correct-values/

Need CGridView examples to move record up/down

How to add ajaxButton in the CGridView

If this is not already a problem like I make of it, I hope some expert here can work on a real working publish/unpublished and sort up/down buttons for the Cookbook…

If this is indeed an unsolvable issue without concrete workaround, please let me (or other newbies) know.

Sorry for all the trouble but this is my first experience with Yii and I am hitting this problem.

Take a look at this post