Yii Framework Forum: CGridView 'delete' button - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CGridView 'delete' button Sends GET request instead of POST Rate Topic: -----

#1 User is offline   Astart 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 12-February 12
  • Location:Dublin

Posted 03 May 2012 - 06:11 AM

Hi, for some strange reason one out of several CGridViews that I use, sends GET request on deletion, instead of sending POST request. I am pretty sure that I am missing something obvious, but I already spent 4 hours trying to figure it out. Hope you guys could help me.

Here's the code:
Controller:
public function actionDeleteReport($loginID, $reportName)
	{
			if(Yii::app()->request->isPostRequest)
			{
				
				$model = UserReports::model()->findByAttributes(array('loginID'=>$loginID, 'reportName'=>$reportName))->delete();				
				
				// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
				/* if(!isset($_GET['ajax']))
					$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('dataManagement')); */
			}
			else
				throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}


CGid:
$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'reports',
    'dataProvider'=>$userReports->search(Yii::app()->user->id),
    'columns'=>array(
        'reportName',
        'reportType',
		'timeStamp',
        array(
        	'class'=>'CButtonColumn',
        	
			'viewButtonUrl'=>'Yii::app()->controller->createUrl("viewReport",array("loginID"=>$data->loginID,"reportName"=>$data->reportName))',

        	'deleteButtonUrl'=>'Yii::app()->controller->createUrl("deleteReport",array("loginID"=>$data->loginID,"reportName"=>$data->reportName))',
        	'deleteButtonImageUrl'=>Yii::app()->request->baseUrl.'../images/delete-green.png',
        	//'deleteConfirmation'=>"js:'Report will be deleted! Continue?'",
			//'afterDelete'=>'function(link,success,data){ if(success) $("#statusMsg").html(data); }',
        	//'updateButtonUrl'=>'Yii::app()->controller->createUrl("download",array("userID"=>$data->userID,"filename"=>$data->filename))',
			
			
        	
			'template'=>'{view}{rename}{download}{delete}',
        	'buttons'=>array(
        		'rename'=>array(
        			'label'=>'Rename file',
					'imageUrl'=>Yii::app()->request->baseUrl.'../images/rename.png',
					'options'=>array(
						'onclick'=>'js:alert("Renaming is currently disabled. Please stay tuned");',
					),
					//'url'=>'Yii::app()->controller->createUrl("renameReport",array("loginID"=>$data->loginID,"deckName"=>$data->deckName))',
        		),
        		 'download'=>array(
        			'label'=>'Download File',
        			'imageUrl'=>Yii::app()->request->baseUrl.'../images/download.png',
        			'url'=>'Yii::app()->controller->createUrl("downloadReport",array("loginID"=>$data->loginID,"reportName"=>$data->reportName))',
        		), 
        	), 
        ),
	),
));

0

#2 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 03 May 2012 - 08:11 AM

You say you are using multiple CGridViews and I am almost sure that this causes the problem. If you are not setting every id on your own Yii will autogenerate them for you which almost always causes problems if you are using multiple widgets. While you set the id of the gridView to "reports" you didn't specify ids for the buttonColumns and my guess is that another gridView is using the same id(s) now. try adding 'id'=>'reports-button-column' in your button column config and try using unique names for the buttons like 'reports-rename' and 'reports-download' because I think they are used as ids too etc.

Hope that helps,

Hannes

P.S.: You could check your generated JS code by searching for "yw" values like "yw0" (meaning yiiwidget0). These are autogenerated and you should get rid of all of them
0

#3 User is offline   Astart 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 12-February 12
  • Location:Dublin

Posted 03 May 2012 - 08:33 AM

I added "id" to the button columns, but it didn't help. Renaming buttons didn't help either(I didn't rename "delete" button though, as I'm not really keen on rewriting it). I also added unique id's to all widgets I had in this view page.

I am using multiple CGridViews throughout website and they all are working fine, but there is only 1 CGridView in this view/action.

ps I couldn't find any "yw" or "yw0". I use firebug and chrome inbuilt extension for inspection. Am I looking in a wrong place?

Here's the Jquery that was generated for delete button:
jQuery('#reports a.delete').live('click',function() {
	if(!confirm('Report will be deleted! Continue?')) return false;
	var th=this;
	var afterDelete=function(){};
	$.fn.yiiGridView.update('reports', {
		type:'POST',
		url:$(this).attr('href'),
		data:{ 'YII_CSRF_TOKEN':'6ab4040ca9534c635bf635cb6386449ace533b6c' },
		success:function(data) {
			$.fn.yiiGridView.update('reports');
			afterDelete(th,true,data);
		},
		error:function(XHR) {
			return afterDelete(th,false,XHR);
		}
	});
	return false;
}

0

#4 User is offline   Astart 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 12-February 12
  • Location:Dublin

Posted 03 May 2012 - 09:09 AM

I have found the problem old-school method, by deleting bits of code and watching if that makes any difference.
So apparently a small script that allows closing flash-windows was causing the problem.

Yii::app()->clientScript->registerScript(
   'myHideEffect',
   '$("#close_btn").click(function(){$(".flash-error").fadeOut("slow"); $(".flash-success").fadeOut("slow")}; $(".flash-notice").fadeOut("slow");)',
CClientScript::POS_READY
);


Should have changed script position from READY to HEAD.
0

#5 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 03 May 2012 - 09:25 AM

Ok, that is weird as I don't quite get why the position of another script is causing the widget to use a GET request instead of POST. However, glad you found a solution ;)
0

#6 User is offline   Astart 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 12-February 12
  • Location:Dublin

Posted 03 May 2012 - 09:29 AM

Well, after I changed that bit, it started working. Then out of curiosity I changed position back to READY and it still works O_O

I have no explanation other than Coding gods were having bits of laugh at me
0

#7 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 03 May 2012 - 09:38 AM

Don't want to drive you crazy but that still sounds like the duplicated id problem ;) If it is happening again you could take a look at the delete button JS code and get its id value. Then search for the same id in the rest of your JS code.
0

#8 User is offline   Astart 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 12-February 12
  • Location:Dublin

Posted 03 May 2012 - 09:43 AM

I'd love to agree with you, because I already had 'id' problems before. But this time it is definitely not the case. I even deleted id's from CGridView just to check - still works.
0

#9 User is offline   sangprabo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 03-July 12

Posted 17 March 2013 - 11:28 AM

Hi, I've also faced the similar problem. The solution that works for me is simply clean your assets folder. And refresh the page. Thank you.
0

#10 User is offline   Alichin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 03-August 10

Posted 06 November 2014 - 08:00 AM

I'm experiencing the same problem. I'm on the 1.1.15 release of Yii, and none of the solutions exposed in the previous post worked for me. After hours of effort I'm giving up and allowing delete by GET requests (very risky practice!).
0

#11 User is offline   AlexInt 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 95
  • Joined: 25-October 13

Posted 06 November 2014 - 08:06 AM

Here you got an example

'delete' => array(
    'url'=>'Yii::app()->createUrl("/controllerID/delete/", array("id"=>$data->id))',

0

Share this topic:


Page 1 of 1
  • 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