Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

yii - Dropdownlist in Cgridview column with ajax load to database

I got some issue. So basically I'm trying to make dropdownlist in CGridView column to manage statuses. I want to load onchange to database. Here is grid column:

array(
        'name'=>'status',
        'type'=>'raw',
        'value'=>'CHtml::dropDownlist('status','',array('1'=>'Complete',
                        '2'=>'Paid',
        '3'=>'Not paid'),array(
                                    'class'=>'status',
                                    'options'=>array($data->status=>array('selected'=>'selected')),
                                    'ajax'=>array(
                                        'type' => 'POST',
                                        'url'=>Yii::app()->createUrl('user/orders/status'),                        
                                        'data'=>array('status'=>'js:this.value','order'=>$data->id),
        )
        ));',
        ),

Here is controller action code

public function actionStatus()
    {
    if (isset($_POST['order'])){
        $model=$this->loadModel($_POST['order']);
        $model->status=$_POST['status'];        
    $model->save();
      
    }    
    }

So what is the problem. I'm getting last $data->id and not id for element which dropdown changed. All other works fine.

Maybe it's easy but I can't find any solution.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...