CButtonColumn with custome header link

Hi all,

I have a CButtonColumn column in my CGridView. I’m trying to add a custom button/link(that makes an ajax call) as the header.

The button works fine when it’s not in the header, but when i have it in the header for some reason 2 ajax calls are made instead of one(i get two alert pop-ups, one that i created and one with an error).

My custom action called by my button checks to make sure if the request is a POST request or not(will through an exception if not) then updates some content.

Here’s my CGridView:




		array(

			'class'=>'CButtonColumn',

			'header'=>"$model->payallbutton",

			'htmlOptions'=>array('width'=>'80px'),

			'template'=>'{view}{update}{delete}{pay}',

			'buttons'=>array(

				'pay'=>array(

					'label'=>'Pay',

					'imageUrl'=>Yii::app()->request->baseUrl.'/images/pay.png',

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

					'options'=>array('class'=>'pay'),

					'visible'=>'$data->status == "unpaid"',

				),

			),

		),



And here’s my controller.




	public function actionPayAll($id){

		if(Yii::app()->request->isPostRequest){

			$commissions = Commissions::model()->findAllByAttributes(array('salesrep_id'=>$id));

			$data = '';

			foreach($commissions AS $commission){

				if($commission->status !== 'paid'){

					$commission->status = 'paid';

					if(!$commission->save()){

						throw new CHttpException(400,'Error Saving Data');

					}

				}

			}

			echo 'All commissions Paid';

		}else{

			throw new CHttpException(400,'Invalid request on PayAll. Please do not repeat this request again.');

		}

	}



The error i get is:


Error 400: <h1>CHttpException</h1>

<p>Invalid request on PayAll. Please do not repeat this request again. (/var/www/html/yii/salesrepview/protected/controllers/CommissionsController.php:154)</p><pre>#0 [internal function]: CommissionsController->actionPayAll('2')

#1 /var/www/html/yii/framework/web/actions/CAction.php(104): ReflectionMethod->invokeArgs(Object(CommissionsController), Array)

#2 /var/www/html/yii/framework/web/actions/CInlineAction.php(48): CAction->runWithParamsInternal(Object(CommissionsController), Object(ReflectionMethod), Array)

#3 /var/www/html/yii/framework/web/CController.php(300): CInlineAction->runWithParams(Array)

#4 /var/www/html/yii/framework/web/filters/CFilterChain.php(134): CController->runAction(Object(CInlineAction))

#5 /var/www/html/yii/framework/web/filters/CFilter.php(41): CFilterChain->run()

#6 /var/www/html/yii/framework/web/CController.php(1144): CFilter->filter(Object(CFilterChain))

#7 /var/www/html/yii/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))

#8 /var/www/html/yii/framework/web/filters/CFilterChain.php(131): CInlineFilter->filter(Object(CFilterChain))

#9 /var/www/html/yii/framework/web/CController.php(283): CFilterChain->run()

#10 /var/www/html/yii/framework/web/CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)

#11 /var/www/html/yii/framework/web/CWebApplication.php(277): CController->run('payall')

#12 /var/www/html/yii/framework/web/CWebApplication.php(136): CWebApplication->runController('commissions/pay...')

#13 /var/www/html/yii/framework/base/CApplication.php(158): CWebApplication->processRequest()

#14 /var/www/html/yii/salesrepview/index.php(13): CApplication->run()

#15 {main}</pre>

Any Ideas why this might be happening?

Thanks

Oliver

Seems, that everything is OK! :] Shouldn’t you check:


if(Yii::app()->request->isAjaxRequest)

instead of:


if(Yii::app()->request->isPostRequest)

I’m using the first version in all my code and this works fine.

Seems that AJAX call are not of POST type and your application reacts fine. Receives non-POST request, so throws an exception.

@Trejder Thanks for the reply…I did not know that there was an ‘isAjaxRequest’ function (still very new)

I will change my code accordingly.

Thanks

Oliver

@Trejder i tried isAjaxRequest but still getting the error…both requests are AJAX. And i believe it’s some sort of sort ajax call. Is there any way i can turn off sorting for a CButtonColumn?

Thanks

Oliver

You did not post the code that creates the ajax call for the header…

This is code i added in my controller for the ajax Pay and PayAll calls




Yii::app()->clientScript->registerScript('pay', "

jQuery('#commissions-grid a.pay').live('click',function() {

        if(!confirm('Are you sure you want to mark this commission as PAID?')) return false;

        

        var url = $(this).attr('href');

        //  do your post request here

        $.post(url,function(res){

             alert(res);

             $.fn.yiiGridView.update('commissions-grid');

         });

        return false;

});

jQuery('a.payall').live('click',function() {

        if(!confirm('Are you sure you want to mark this commission as PAID?')) return false;

        var item = $('#commissions-grid input:checkbox:checked');

        var checked = [];

        for(var i=0; i<item.length; i++) {

			if(item[i].checked){

				checked.push(item[i].value);

			}

		}

        //console.log(checked.length);

        var url = $(this).attr('href');

        //  do your post request here

        if(checked.length >= 1){

			$.post(url,'ids=' + checked,function(res){

				 alert(res);

			 });

			 $.fn.yiiGridView.update('commissions-grid');

		}else{

			alert('No Commissions selected');

		}

        return false;

});

");



Any Thoughts?

Thanks

Oliver

You did not post your code that is gerated in the header ($model->payallbutton)… but I guess it generates a link…

CGidView attaches an event handler to all <a href> links in the headers to process sorting… so I think that is why you get here two calls…