I tried to make direct edit possible in CGridView in a simple way as described in this posting.
I wrapped a form around the CGridView.
The form input fields are displayed in the grid view. I created a submit Button. But Yii does not save the changed values because they are not submitted in the $_POST Array. I don’t find out , why they are not submitted.
The input fields are created with this function in the model:
public function getInputField($fieldName) {
return CActiveForm::textField($this,$fieldName,array("name"=>"Noten[".$this->Id."][".$fieldName."]"));
}
The Controller code:
public function actionEditableGrid() {
$model=new Noten('search');
$notenData = $model->search();
if (isset($_POST['Noten']) ) {
foreach( $notenData->data as $i=>$item ) {
if(isset($_POST['Noten'][$i])) {
$item->attributes=$_POST['Noten'][$i];
if ( $item->validate() and $item->save() )
$this->redirect(array('admin'));
}
}
}
...
}
I checked out, that the save function of the model is called by my code, I am redirected to the admin page. But it seems that the submitted values in $_POST[‘Noten’][$i] are not the new values. I don’t understand, why the old values are submitted.
The input fields are created with a function in the model. HtmlOptions can also be commited:
public function getInputField($fieldName,$row,$options=array()) {
return CActiveForm::textField($this,$fieldName,
array_merge(array("name"=>"Noten[".$row."][".$fieldName."]"),$options)
);
}
}
The Controller code. Now the redirection command is on the right place.
public function actionEditableGrid() {
$model=new Noten('search');
$notenData = $model->search();
if (isset($_POST['Noten']) ) {
foreach( $notenData->data as $i=>$item ) {
if(isset($_POST['Noten'][$i])) {
$item->attributes=$_POST['Noten'][$i];
if ( $item->validate() ) $item->save() )
}
}
$this->redirect(array('admin'));
}
...
}