CGridView and Jeditable widget

Hi

I need help to make a CGridView with an edit in place function

I try several widgets (such as CEditableColumn, or Jeditable, or jqgrid) but it never works

My last try was with Jeditable widget

I want to extend CGridColumn to override the renderDataCellContent function

Here is my code : CJeditableColumn.php




Yii::import('zii.widgets.grid.CGridColumn');


/**

 * CJEditableColumn represents a grid view column that is editable.

 */


class CJEditableColumn extends CGridColumn

{

	public $name;	

	public $value;

	

	/**

	 * Initializes the column.

	 */

	public function init()

	{

		parent::init();

		//Yii::log("init : ".$this->name." ".$this->value,"debug","z");

		if($this->name===null && $this->value===null)

			throw new CException(Yii::t('zii','Either "name" or "value" must be specified for CJEditableColumn.'));

	}

	

	/**

	 * Renders the data cell content.

	 * @param integer the row number (zero-based)

	 * @param mixed the data associated with the row

	 */

	protected function renderDataCellContent($row,$data)

	{

						

		echo $this->grid->owner->widget(

                'application.extensions.jeditable.DsJEditableWidget', 

                array(

                        'jeditable_type' => 'text',

                        'model'=>$data,

                        'attribute'=>$this->value,

                        'id'=>$data->tableSchema->name[$this->name],                		

                        'width'=>'200',

                )

        );

        

	}

}



and here my gridview




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

		    'dataProvider'=>$applicationsDP,	

		    'id'=>'gridViewApplication', 	

		    'columns'=>array(						

		 		array(

		 			'class'=>'application.extensions.CJeditableColumn',

		 			'name'=>'utilisateur',

		 			'value'=>'utilisateur',

		 		),

		 		'id',

		 		'utilisateur',				   		        

		    ),

		    //'cssFile' => Yii::app()->request->baseUrl . '/css/gridView.css',

		    'htmlOptions' => array('style'=>'overflow:auto;' ),

        	    'enablePagination'=>false, // j'arrive pas a faire marcher la pagination inside le tabview 

    		    'summaryText'=>'{count} z-info(s)',

    		    'emptyText'=>'No z-info.',        			    

		));



When I call the gridview, I get the error : DsJEditableWidget must specify "model" and "attribute" or "name" property values.

And if I try with




protected function renderDataCellContent($row,$data)

	{

						

		echo $this->grid->owner->widget(

                'application.extensions.jeditable.DsJEditableWidget', 

                array(

                        'jeditable_type' => 'text',

                        'model'=>$data,

                        'attribute'=>$this->value,

                        'name'=>$this->value,

                        'id'=>$data->tableSchema->name[$this->name],                		

                        'width'=>'200',

                )

        );



I get the error : Object of class DsJEditableWidget could not be converted to string

Any ideas ?

Thanks

no one ?

so my app will not have this features…

:-[

you must set the type output data with the function $this->grid->getFormatter()->format($value,type);

please try this

protected function renderDataCellContent($row,$data)

    {


            //set widget into a variable                                 


            $value =$this->grid->owner->widget(


            'application.extensions.jeditable.DsJEditableWidget', 


            array(


                    'jeditable_type' => 'text',


                    'model'=>$data,


                    'attribute'=>$this->value,


                    'name'=>$this->value,


                    'id'=>$data->tableSchema->name[$this->name],                            


                    'width'=>'200',


            )


              );


        //set the output type


        $this->grid->getFormatter()->format($value,'raw');//(value,type) you can also use 'html'

You’re the best

B) It works ! B)

thank you very much very much

Note : i don’k know how to update the title to ‘Solved’

hello,why i still get error for my code??

i copy the file CJeditableColumn.php

and stil get error that model and attributes must be set

i use yii 1.1.8 can u get me an idea???

thanks

after i try to fix i know that my gridcolumn not specefied value, thanks.

now why, i can edit first row but can’t edit in next row???

Because in the given implementation each row has the same id.

I’ve make some changes on the code to also use it with other jeditable types:




<?

Yii::import('zii.widgets.grid.CGridColumn');


/**

 * CJEditableColumn represents a grid view column that is editable.

 */


class CJEditableColumn extends CGridColumn

{

	public $name;   

	public $value;

	public $type='text';

	public $model;

	public $sortable;

	public $filter;

	

	/**

	 * Initializes the column.

	 */

	public function init()

	{

			parent::init();

			//Yii::log("init : ".$this->name." ".$this->value,"debug","z");

			if($this->name===null && $this->value===null)

					throw new CException(Yii::t('zii','Either "name" or "value" must be specified for CJEditableColumn.'));

	}

	

	/*

	 * Render column header 

	 */

	protected function renderHeaderCellContent()

	{

		if($this->grid->enableSorting && $this->sortable && $this->name!==null)

			echo $this->grid->dataProvider->getSort()->link($this->name,$this->header);

		else if($this->name!==null && $this->header===null)

		{

			if($this->grid->dataProvider instanceof CActiveDataProvider)

				echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));

			else

				echo CHtml::encode($this->name);

		}

		else

			parent::renderHeaderCellContent();

	}

	

	/*

	 * Render column filter 

	 */

	

	protected function renderFilterCellContent()

	{

		if(is_string($this->filter))

			echo $this->filter;

		else if($this->filter!==false && $this->grid->filter!==null && $this->name!==null && strpos($this->name,'.')===false)

		{

			if(is_array($this->filter))

				echo CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id'=>false,'prompt'=>''));

			else if($this->filter===null)

				echo CHtml::activeTextField($this->grid->filter, $this->name, array('id'=>false));

		}

		else

			parent::renderFilterCellContent();

	}


	/**

	 * Renders the data cell content.

	 * @param integer the row number (zero-based)

	 * @param mixed the data associated with the row

	 */


	protected function renderDataCellContent($row,$data)

	{

		// DsJEditableWidget parameters

		$parameters=array(

			'jeditable_type'=>$this->type,

			'name'=>$this->name,

			'id'=>$this->name.$data->id,

			'width'=>'200',

		);

		//If the value is an expression return the correct value

		if(substr_count($this->value,'$')>0){	

			$value = $this->evaluateExpression($this->value,array('row'=>$row,'data'=>$data));

			$parameters['value']=$value;

			//If is setting 'select' as jeditable type create the list

			if($this->type=='select'){

				$name= some_function_to return_model_name();

				$models=new $name;

				/*

				 * It's assuming that the parameters of the model you need is 'id' and

				 * his lowercase name

				 */

				$name=lcfirst($name);

				$data=CHtml::listData($models->findAll(),'id',$name);

				$parameters['data']=$data;

			}

		}else{

			if(isset($this->model))

				$parameters['model']=new $this->model;

			else

				$parameters['model']=$data;

			$parameters['attribute']=$this->value;

		}


		//set widget into a variable

		$editable =$this->grid->owner->widget('application.extensions.jeditable.DsJEditableWidget',$parameters);

		$this->grid->getFormatter()->format($editable,'raw');       

	}

}

?>



To make it works correctly it’s also need to set $parameters[‘callback’]=jsfunction to save the modified data but I don’t have implement it yet