First: You can avoid all this ugly escaped code if you use a getter in your model class:
public function getStatusDropdown()
{
$stats = array(
1 => 'Complete',
2 => 'Paid',
3 => 'Not paid',
);
return CHtml::dropDownlist('status',$this->status,$stats, array(
'class' => 'status',
'data-id' => $this->id,
));
}
Now add a grid column like
array(
'name' => 'Status',
'type' => 'raw',
'value' => '$data->statusDropdown',
),
What's left now is to add some Javascript. Instead of adding a script to each and every button it's much more efficient if you register one snippet to rule them all. You have to listen to the change
event of all dropdowns. So you could register a inline snippet right on the page with your gridview like this:
$url = $this->createUrl('user/orders/status');
Yii::app()->clientScript->registerScript('initStatus',
"$('select.status').on('change','body',function() {
el = $(this);
$.ajaxPost('$url', {status: el.val(), id: el.data('id')}
});",
CClientScript::POS_READY
);
I've added a body
selector to make sure, the event still fires if your GridView is updated through AJAX. You may also want to add a success handler to your ajaxPost()
call.
Note, that the above may contain typos, so don't just copy and paste but try to understand how it works. It should get you on the right track.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…