2011-11-21 32 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時,列標題被強制爲複選框。我需要列標題來指示覆選框表示的服務類型。 –