direct edit in CGridView

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.

Code snippet of the view:


<form name="editableGridForm" method="post" action="index.php?r=noten/editableGrid;"> <?php


    $this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'noten-grid',

        'dataProvider'=>$notenData,

        'filter'=>$model,

        'columns'=>array(

           array('name'=>'Id','htmlOptions'=>array('width'=>'25px')),

              array('name'=>"Werk",

                'type'=>'raw',

                'htmlOptions'=>array('width'=>'150px'),

                'value'=>'$data->getInputField(\'Werk\')'),

            ...

            )

        )

    );

    echo CHtml::submitButton('Änderungen speichern');

    ?>

</form>

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. >:(

hi

by firebug, console tab, check value of $_POST that see what happens

There were some bugs in the code. Here is the corrected code. I also added some improvements.

It is necessary to pass the row number of the gridView, this is the variable $row, to the function in the model:


<form name="editableGridForm" method="post" action="index.php?r=noten/editableGrid;"> <?php


    $this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'noten-grid',

        'dataProvider'=>$notenData,

        'filter'=>$model,

        'columns'=>array(

           array('name'=>'Id','htmlOptions'=>array('width'=>'25px')),

              array('name'=>"Werk",

                'type'=>'raw',

                'htmlOptions'=>array('width'=>'150px'),

                'value'=>'$data->getInputField(\'Werk\',$row)'),

            ...

            )

        )

    );

    echo CHtml::submitButton('Änderungen speichern');

    ?>

</form>

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

  }

  ...

}



Now direct input in CGridView is working.

[color="#222222"][size=“2”]unfortunately this solution doesn’t save edited data of my grig, may i have some help?[/size][/color]

  • View name: admin
  • Model name: Timesheet
  • Controller name: TimesheetController

[color="#222222"][size="2"]Thanks in advance.[/size][/color]

View


<form name="editableGridForm" method="post" action="editableGrid"> <?php

                                $this->widget('zii.widgets.grid.CGridView', 

                                  array(

                                    'id'=>'timesheet-grid',

                                    //'htmlOptions' => array('class' => 'list-group'),

                                    'dataProvider'=>$model->search(),

                                        'columns'=>array(

                                            array('name'=>'IDCOMMESSA','htmlOptions'=>array('width'=>'15px')),

                                            array('name'=>"DESCRIZIONE",

                                                'type'=>'raw',

                                                'headerHtmlOptions' => array('width'=>'650px'),

                                                //'htmlOptions'=>array('size'=>'300px'),

                                                'value'=>'$data->getInputField(\'DESCRIZIONE\',$row)',

                                            ),

                                            array('name'=>'ORE','htmlOptions'=>array('width'=>'15px')),

                                        )

                                    )

                                );

                                echo CHtml::submitButton('salva',array('class' => 'btn btn-success'));

                                ?>

                            </form>

Model


public function getInputField($fieldName, $row, $options = array()) {

            return CActiveForm::textField($this, $fieldName, 

                    array_merge(array("name" => "Timesheet[" . $row . "][" . $fieldName . "]"), $options)

            );

        }

Controller


public function actionEditableGrid() {

        $model=new Timesheet('search');

        $notenData = $model->search();

        

        if (isset($_POST['Timesheet']) ) {

            foreach( $notenData->data as $i=>$item ) {

                if(isset($_POST['Timesheet'][$i])) {

                    $item->attributes=$_POST['Timesheet'][$i];

                    $item->save();  

                }

            }

            $this->redirect(array('admin'));

        }

    }

It’s a long time ago that I worked with this project, so I hardly can remember what I have done …

May be you have forgotten to define an access rule for the action editableGrid in the controller?


public function accessRules() {

   return array(

      array('allow', 

            'actions'=>array('create','update','delete','editableGrid'),

            'users'=>array('@'),

      ),

Hi Ferdinand you’re right!!! thanks a lot