Yii CGridView和每行多个复选框

时间:2011-11-21 14:22:00

标签: checkbox controller yii

背景

我有一个包含多个复选框列的CGridView。我使用如下代码创建了复选框列:

$columns[] = array(
    'header'=>'Health',
    'value' => 'CHtml::checkBox("hsid[]", $data->healthService, array("value"=>$data->wc_client_id,"id"=>"hsid_".$data->wc_client_id))',
    'type'=>'raw',
    'htmlOptions'=>array('style'=>'text-align:center'),
);

$columns[] = array(
    'header'=>'Education',
    'value' => 'CHtml::checkBox("esid[]", $data->educationService, array("value"=>$data->wc_client_id,"id"=>"esid_".$data->wc_client_id))',
    'type'=>'raw',
    'htmlOptions'=>array('style'=>'text-align:center'),
);

$ data-> healthService $ data-> educationService 用于根据数据库中的数据设置复选框的初始检查状态。

问题

如何连续捕获每个不同复选框的更改,并将这些更改发送回我的控制器?然后,控制器将根据复选框更改来更新数据库。

2 个答案:

答案 0 :(得分:2)

以下是我最终的工作方式:

查看代码

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'service-grid',
    'dataProvider'=>$clients->search(),
    'columns'=>array(
        'first_name',
        'last_name',
        array(
            'header'=>'Education',
            'class'=>'CDataColumn',
            'type'=>'raw',
            'htmlOptions'=>array('style'=>'text-align:center'),
            'value' => 'CHtml::checkBox("esid[]", $data->education, array("value"=>$data->wc_client_id,"id"=>"esid_".$data->wc_client_id))',
        ),
        array(
            'header'=>'Health',
            'class'=>'CDataColumn',
            'type'=>'raw',
            'htmlOptions'=>array('style'=>'text-align:center'),
            'value' => 'CHtml::checkBox("hsid[]", $data->health, array("value"=>$data->wc_client_id,"id"=>"hsid_".$data->wc_client_id))',
        )
    ),
));  

控制器代码检索所选ID

$healthClientId = array();
if(isset($_POST['hsid']) && is_array($_POST['hsid']))
{
  $healthClientId = $_POST['hsid'];
}

$educationClientId = array();
if(isset($_POST['esid']) && is_array($_POST['esid']))
{
  $educationClientId = $_POST['esid'];
}

答案 1 :(得分:1)

可能是一个更好的选择CCheckBoxColumn

请参阅http://www.yiiframework.com/doc/api/1.1/CCheckBoxColumn