2011-11-21 34 views
1

背景Yii的CGridView和多重复选框每行

我有一个包含多个复选框列的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->状况服务$ DATA-> educationService用于设置初始检查复选框的状态,基于从数据数据库。

问题

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

回答

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

控制器代码来检索选定的标识

$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

可能是更好的选择是CCheckBoxColumn

http://www.yiiframework.com/doc/api/1.1/CCheckBoxColumn

+0

V2P ,我尝试使用CCheckBoxColumn并无法控制列标题。当selectableRows> = 2时,列标题被强制为复选框。我需要列标题来指示复选框表示的服务类型。 –