本文介绍了带有ajax加载到数据库的Cgridview列中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些问题.所以基本上我试图在 CGridView 列中创建下拉列表来管理状态.我想将 onchange 加载到数据库.这是网格列:

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

这里是控制器动作代码

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

    }
    }

那有什么问题.我得到的是最后一个 $data->id 而不是下拉列表更改的元素的 id.其他一切正常.

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.

推荐答案

第一:如果您在模型类中使用 getter,您可以避免所有这些丑陋的转义代码:

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

现在添加一个网格列

array(
    'name'  => 'Status',
    'type'  => 'raw',
    'value' => '$data->statusDropdown',
),

现在剩下的就是添加一些 Javascript.如果您注册一个片段来统治所有按钮,那么与其向每个按钮都添加一个脚本,效率会更高.您必须收听所有下拉列表的 change 事件.因此,您可以使用 gridview 直接在页面上注册一个内联代码段,如下所示:

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

我添加了一个 body 选择器以确保如果您的 GridView 通过 AJAX 更新,该事件仍然会触发.您可能还想在 ajaxPost() 调用中添加一个成功处理程序.

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.

这篇关于带有ajax加载到数据库的Cgridview列中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:11
查看更多