CGrideView and Custom data

Hi dear,

What is possibility to display custom data in CGrideview?

I have to display a row next to header containing data that I want to display.

Is there any way of doing so?

I don’t think CGridview has a built-in ability to do it but it probably can be achieved by extending CGridView and overriding renderTableBody() method in child class.

CGridView has well though through customization features. You can customize your own columns, related models, filters and actions column.

Read:

http://www.yiiframework.com/doc/api/1.1/CGridView

http://www.yiiframework.com/doc/api/1.1/CDataColumn

Please specify what custom data you mean - from other model? custom filter…?

Here’s example to demonstrate customization capabilities of component CGridView:




<?php

$r = AdminHelper::getAuthRoles(false, true);


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

	'id'=>'assignment-model-grid',

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

	'filter'=>$model,


	'columns'=>array(


		array(

			'name' => 'inventory.name',

			'filter' => CHtml::textField('InventoryModel[name]', $model->inventoryName),

		),


		array(

			'name' => 'inventory.inventory_number',

			'filter' => CHtml::textField('InventoryModel[inventory_number]', $model->inventoryNumber),

		),


		array(

			'name' => 'usermodel.firstname',

			'filter' => CHtml::textField('UserModel[firstname]', $model->userFirstname),

		),


		array(

			'name' => 'usermodel.lastname',

			'filter' => CHtml::textField('UserModel[lastname]', $model->userLastname),

		),


		array(

			'name'	=> Yii::t('main','User'),

			'value' => '$data->usermodel->firstname." ".$data->usermodel->lastname',

			'filter' => CHtml::textField('fullname', $model->fullname),

		),

		

		array(

			'name'	=> 'role',

			'value' => 'AdminHelper::getAuthRoleDesc($data->role, false, true)',

			'filter' => CHtml::dropDownList('UserModel[role]', $model->role, $r,  array('prompt' => Yii::t('main','No filter')) ),

		),


		array(

			'name' => 'date_issued',

			'value' => '$data->date_issued > 0 ? date("d/m/Y", $data->date_issued) : "-" ',

			'filter' => CHtml::textField('AssignmentModel[date_issued]'),

		),

		

		array(

			'name' => 'date_returned',

			'value' => '$data->date_returned > 0 ? date("d/m/Y", $data->date_returned) : "-" ',

		),

		

		'note',


		array(

			'name' => 'price_issued',

			'value' => 'number_format($data->price_issued,2)." ".$data->inventory->currency_code',

		),


		array(

			'class'=>'CButtonColumn',

			'template' => '<div align="left">{view} {update} {return} {delete}</div>',

			'buttons' => array(

				'return' => array(

					'label' => Yii::t('main','Return inventory'),

					'url' => 'Yii::app()->createUrl("assignment/return", array("id"=>$data->id))',

					'imageUrl' =>  Yii::app()->request->baseUrl."/css/undo.png",

					'visible' => '$data->date_returned == 0',

				),

				'update' => array(

					'visible' => '$data->date_returned == 0',

				)

			),

			'updateButtonLabel'=>Yii::t('main','Update issued inventory'),


			'deleteButtonLabel'=>Yii::t('main','Return inventory'),

			'deleteButtonUrl'=> 'Yii::app()->createUrl("assignment/return", array("id" => $data->id))',

			'deleteButtonImageUrl'=> Yii::app()->request->baseUrl."/css/certificate_ok.png",

		),

	),

));




Cheers

lubos

if you want to display custom data in cgridview you can use CArrayDataProvider instead using CActiveDataProvider…

Thanks you all for support.