CGridView edit data in grid row

is any example how to edit data directly in grid ? put textboxs in cells and edit data in textbox

I believe that for that you will require to incorporate a in-place editor on your rows (a good one is http://www.appelsiini.net/projects/jeditable)

Hi kusanagi

Have you resolve your problem

I’m looking to edit data in the grid, but without success for the moment…

If yes, could you please tell me how to do ?

thanks

Ok a little bit of a hack way to do this.

For CGridView

‘columns’=>array(

array('name'=>'name',


'type'=>'raw',


),

your model needs

public function getname()

{

return CActiveForm::textField($this,"rank",array("name"=>"form_name[rank][" .$this->id . "]"));

}

The best way NO but it works. Until someone much smarter then me explains the right way to do this without outside javascript.

I consider this a good idea.

I changed the code a little.

The model:


public function getInputField($fieldName) {

  return CActiveForm::textField($this,$fieldName,array("name"=>"form_name[".$fieldName."][" .$this->Id . "]"));

    }

The grid:


'columns'=>array(

   array('name'=>"theFieldName",

         'type'=>'raw',

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

But how can I save the values in the input fields?

Sorry, I am a beginner, please help me … :rolleyes:

I have the same problem…

How Can I save the value in the input fields??

Because I want to complete the Fields and the submit it

Thanks you

It’s a long time ago, I can hardly remember how I got the solution.

For this example the model is named:


mytable

The controller:


 ...       

$model=new mytable('search');

$model->unsetAttributes();

if(isset($_GET['mytable'])) {

   $model->attributes = $_GET['mytable'];

}

$FormData = $model->search();


// Set the new values that come from $_POST in the model:

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

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

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

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

         if ( $item->validate() ) $item->save();

         else throw new CHttpException(404,'Cannot save.');

       }

   }

}


$this->render('mytableGridView',array(

    'model'=>$model,

    'data'=>$FormData,

));



The view:


  ...

  $data->pagination = array('pageSize'=> 10);

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

    'dataProvider'=>$data,

    'filter'=>$model,

    'columns'=>array( ...



Hope, that can help you. Please let me know if it worked.

Thanks you for your help, but, I can’t do it.

I have this error: Non-static method CActiveForm::textField() should not be called statically, assuming $this from incompatible context… any idea?

Did you write the function using the method CActiveForm::textField() inside the model class for your data table?

Do you use Yii version 1.1 ?

Hi, thanks for you help.

Yes, I did it. I’m using Yii 1.1


class Facturadebe extends CActiveRecord

{


public function rules()

	{

		return array(

			array('fecha, monto, nroFactura, id_emp', 'required'),

			array('nroFactura, id_emp, pagado', 'numerical', 'integerOnly'=>true),

			array('monto', 'numerical'),			

			array('id, fecha, monto, nroFactura, id_emp, pagado', 'safe', 'on'=>'search'),

		);

	}


(...)


public function getInputField($fieldName) {

            return CActiveForm::textField($this,$fieldName,array("name"=>"form_name[".$fieldName."][" .$this->Id . "]"));

        }


}

and in my View:


$this->widget('booster.widgets.TbGridView', array(

            'id' => 'dispositivo-grid-list',

            'dataProvider' => $data,

            'columns' => array( 

                        (...................)


               array(

                    'name' => 'monto',

                    'header'=>'Monto',

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

                    

                ),


            ),            




Thanks you again for your help…!!

Hi,

I found a little difference to your example in my code: The "@" to avoid the error message.

May be, that’s all you need.


$return = @CActiveForm::textField($this,$fieldName, ...

Sorry, I am not able to remember, why I found this solution. It is not a clean solution but it seems to work.

Please try it and let me know, if you have success.

Thanks you so much!!!

I’m near…

https://drive.google.com/file/d/0B6ShgLFZKpgnU05ydnVSd3ZPaWs/view?usp=sharing

Thanks you for your help…

Indeed, you only have to add one line to each colum definition array in the gridView to avoid the HTML tags get displayed.


   array(

        'type'=>'raw'

        ...



it’s true.!!! Excellent.!! Thanks you…

but… I have the other problem (I’m so sorry), into the Controller, never read the $_POST[‘mytable’]. Can you show me how you define the GridView in the View?

Thanks you again


$model=new mytable('search');

$model->unsetAttributes();

if(isset($_GET['mytable'])) {

   $model->attributes = $_GET['mytable'];

}

$FormData = $model->search();


// Set the new values that come from $_POST in the model:

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

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

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

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

         if ( $item->validate() ) $item->save();

         else throw new CHttpException(404,'Cannot save.');

       }

   }

}


$this->render('mytableGridView',array(

    'model'=>$model,

    'data'=>$FormData,

));

In the view you have to wrap a form around the gridView.


<form name="editableGridForm" 

      id="editableGridForm" 

      method="post"

      action="index.php?r=yourController/yourAction"> <?php


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

      .... 

   ));


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

</form>

Please mention the action attribute of the form element.

Thanks you…

My model is:




class Facturadebe extends CActiveRecord{

(............)


public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'fecha' => 'Fecha',

			'monto' => 'Monto',

			'nroFactura' => 'Nro Factura',

			'id_emp' => 'Id Emp',

			'pagado' => 'Pagado',

		);

	}




(..............)


}




and My Controller:


public function actionAdmin()

	{

		$FacturasDebe = Facturadebe::model()->findAllByAttributes(array('pagado'=>'0'));

                $model=new Facturadebe('search');

                $model->unsetAttributes();

                if(isset($_GET['Facturadebe'])) {

                   $model->attributes = $_GET['Facturadebe'];

                   die();

                }

                

                $data = $model->search();

                

                date_default_timezone_set('America/Buenos_Aires');     

                

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

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

                        die("ENTRO");

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

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

                          if ( $item->validate() ) $item->save();

                          else throw new CHttpException(404,'Cannot save.');

                        }

                    }

                 }              

                 

                $this->render('admin',array(

			//'DataProviderFacturas'=>$DataProviderFacturas,

                    'model' => $model,

                    'data'=>$data,

		));

	}




and the view:


<form name="editableGridForm" 

      id="editableGridForm" 

      method="post"

      action="admin"> <?php

      

 $this->widget('booster.widgets.TbGridView', array(

            'id' => 'miTabla',

            'dataProvider' => $data,

            'responsiveTable' => true,

            'columns' => array(                 

                array(

                    'name' => 'fecha',

                    'header'=>'Fecha',

                    'value' => 'Yii::app()->dateFormatter->format("dd/MM/yyyy",strtotime($data->fecha))',

                ),  

                array(

                    'name' => 'nroFactura',

                    'header'=>'N° Factura'

                ),

                

                array(

                    'name' => 'monto',

                    'header'=>'Monto',

                    'type'=>'raw',

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

                    

                ),

                array(

                    'class' => 'booster.widgets.TbButtonColumn',

                   // 'htmlOptions' => array('width' => '40'), //ancho de la columna

                    'template' => '{pagar}', // botones a mostrar

                    'buttons'=>array(

                        "pagar"=>array(

                            'label'=>'Pagada',

                            'icon'=>'glyphicon glyphicon-ok',  

                            'url'=> 'Yii::app()->createUrl("Facturadebe/facturaPagada", array("id"=> ' . '$data->id)) ',

                            )),

                ),

            ),


        ));

    

     

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

</form>

thanks you sir for your help…