Yii CGridview复选框ajax数据库更新

时间:2013-04-02 09:58:00

标签: php ajax yii cgridview

我在yii cgridview中列出了三个项目,每行都有一个复选框,点击当前行项目的状态应该在数据库中更新。请提供一些实现此目的的想法。

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以使用cgridview columns数组的ajax属性。请找到以下示例。

    array(
        'header'=>'Status',
        'type'=>'raw',
        'value'=>'CHtml::CheckBox("$data->active",$data->active,array(
         "ajax" => array(
                    "type"=>"POST", 
                    "url"=> ApplicationConfig::getURL("user", "twitterFeed", "changeStatus",array("feed_id"=>$data->feed_id)),
                    "dataType"=>"text",
                    "data" => array("ap_programType_id" => $data->feed_id),
                    "success" => "js:function(html){
                           if(html==\"1\"){
                               $(\"#active$data->feed_id\").attr(\"checked\",\"checked\");
                               window.location.reload()
                           }
                               else if(html==\"0\"){
                               $(\"#active$data->feed_id\").attr(\"checked\",false);
                               window.location.reload()
                           }

                    }",
                    "error"=>"function (xhr, ajaxOptions, thrownError){
                            alert(xhr.statusText);
                            alert(thrownError);}",
            ),
        "style"=>"width:50px;","feed_id"=>$data->feed_id,"id"=>"active".$data->feed_id))',
        'htmlOptions'=>array("width"=>"50px"),
    ),