Yii Framework Forum: CGridView and Jeditable widget - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CGridView and Jeditable widget Rate Topic: ***** 1 Votes

#1 User is offline   chtibob69 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 13-February 11

Posted 19 April 2011 - 08:03 AM

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
0

#2 User is offline   chtibob69 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 13-February 11

Posted 27 April 2011 - 02:47 PM

no one ?
so my app will not have this features...
:-[
0

#3 User is offline   jllanes 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 07-December 10

Posted 23 May 2011 - 10:39 PM

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'
0

#4 User is offline   chtibob69 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 13-February 11

Posted 24 May 2011 - 10:10 AM

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'
0

#5 User is offline   abud 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 01-February 09

Posted 17 November 2011 - 08:48 PM

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
0

#6 User is offline   abud 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 01-February 09

Posted 17 November 2011 - 09:42 PM

View Postabud, on 17 November 2011 - 08:48 PM, said:

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???
0

#7 User is offline   Ser 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 11-January 12
  • Location:Italy

Posted 11 January 2012 - 05:56 AM

View Postabud, on 17 November 2011 - 09:42 PM, said:

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users