Ok here’s what i have so far(and it work!!! thanks yiqing95)
In my view i added the following:
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);
});
return false;
});
");
and here’s my CGridView
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'commissions-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'order_id',
//'salesrep_id',
'customer_id',
'commission_total',
array(
'name'=>'status',
'filter'=>array('paid'=>'Paid', 'unpaid'=>'Un-paid'),
),
array(
'class'=>'CButtonColumn',
'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("rep"=>$data->salesrep_id, "id"=>$data->id))',
'options'=>array('class'=>'pay'),
'visible'=>'$data->status == "unpaid"',
),
),
),
),
)); ?>
And in my controller i have my pay action:
public function actionPay($id){
if(Yii::app()->request->isPostRequest)
{
$commissions = $this->loadModel($id);
if($commissions->status !== 'paid'){
$commissions->status = 'paid';
if($commissions->save()){
echo 'Updated';
}else{
echo 'Error while paying';
}
}
}else{
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
}
The only bit i’m trying to figure out now is how to refresh/redirect after the ajax request.
I tried both the redirect and refresh methods in my pay action but nothing happened. Does this need to be done via JS ?
Thanks
Oliver