CGridView 'delete' button

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))',

        		), 

        	), 

        ),

	),

));

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

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;

}

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")<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/wink.gif' class='bbc_emoticon' alt=';)' />',

CClientScript::POS_READY

);

Should have changed script position from READY to HEAD.

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 ;)

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

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.

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.

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.

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!).

Here you got an example




'delete' => array(

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



Please follow the steps to check:

  1. Check the “id” property of each CGridView and make sure they are totally different

  2. Check if all the javascript files needed are loaded (jquery.ba-bbq.js, jquery.yiigridview.js, jquery.js).

If you want to how to debug and find the indeed reason, you can read a post for more.

Yii CGridView 400 Error – ‘delete’ Sends GET request

Hope it can help