Logic in gridview widget

Hi

I have the following gridview which is contained in a partial.




<?php

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

 'id'=>'gridProfileSegmentList',

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

 'htmlOptions'=>array('style'=>'margin-bottom: 10px;'),

 'dataProvider'=>$dataProviderExistingSegments,

 'columns' => array(

    	array(

      	'name' => 'Segment Order',

 				'type' => 'raw',

 				'value' => 'CHtml::encode($data->SegmentNo)'

     	),

    	array(

      	'name' => 'Segment Name',

 				'type' => 'raw',

 				'value' => 'CHtml::encode($data->SegmentName)'

     	),

    	array(

      	'name' => 'Number of Users',

 				'type' => 'raw',

 				'value' => 'CHtml::encode($data->NumUsers)'

     	),

    	array(

      	'name' => 'Segment Length (minutes)',

 				'type' => 'raw',

 				'value' => 'CHtml::encode($data->SegmentLen/60)'

     	),

     	array(

      	'name' => 'Action',

 				'type' => 'raw',

 				'value' => '

 				CHtml::link(

	 				"edit",

	 				array(

	 					//"profile/editprofile",

	 					//"pid"=>$data->ProfileID,

	 					//"segnum"=>$data->SegmentNo

	 				),

	 				array(

						"onclick"=>"showDialogAddEditSegment(

						{\"mode\":\"edit\",

						\"pid\":$data->ProfileID,

						\"segnum\":$data->SegmentNo,

						\"oldSegnum\":$data->SegmentNo,

						\"okBtnTitle\":\"edit segment\"}

						); return false;"

					)

				)

 				. " | " .

 				CHtml::link(

	 				"delete",

	 				array(

	 				),

	 				array(

						"onclick"=>"showDialogAddEditSegment(

						{\"mode\":\"delete\",

						\"pid\":$data->ProfileID,

						\"segnum\":$data->SegmentNo,

						\"okBtnTitle\":\"delete segment\"}

						); return false;"

					)

				)'

 			),

    )

	)

);



I want to display the same grid in two places in the system. In one place it should apppear exactly as above…in the other it should show everything as above except the last column which is headed ‘Action’ - this gives access to editing etc for items in the grid and I do not want those to be available in the second page. To do this is there a away to put some logic into the grid to show, or not show the last column depending on conditions or do I need to create another partial file with the same code minus the last column.

Thanks

tb

I’d create another partial

cool - thank you zaccaria

Just for your information, another way might be like this …




$columns = array(

	array(

		'name' => 'Segment Order',

		'type' => 'raw',

		'value' => 'CHtml::encode($data->SegmentNo)'

	),

	....

);

if ($some_condition)

{

	$columns[] = array(

		'name' => 'Action',

		'type' => 'raw',

		'value' => '

		....

	);

}

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

	'id'=>'gridProfileSegmentList',

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

	'htmlOptions'=>array('style'=>'margin-bottom: 10px;'),

	'dataProvider'=>$dataProviderExistingSegments,

	'columns' => $columns,

));



I think it’s a matter of taste which way you would take, but zaccaria’s way seems a little more stable in the long run. Maybe he doesn’t want a spaghetti code that is full of 'if-then-else’s.

thanks you softark - that is useful, i hadn’t thought of building the column array outside the grid :)