GridView/ListView and Custom columns

Hello,

I have tried quite a lot of things now, but it’s not working as I want it to do.

I want to use a CGridCiew or CListView, but I would like to add a column not found in the corresponding database table(s).

What would be the best way to do that?

I tried several things, but wasn’t succesfull. The closest was a ListView where I would have needed to adapt the vertical spacing.

Something like add field to corresponding Model, set its value per method and just use something like that




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

    'dataProvider'=>$dataProvider,

    'columns'=>array(

        'name',

        array('name'=>'bla',               

               'value'=> '$data->customField')

        )

    )

);



in the view.

For CGridView, you could create a subclass of CGridColumn and override the render functions if you need to, depending on what you are trying to display with the custom column. Lets say you call the subclass "CustomColumn". Use it in CGridView like so




'columns'=>array(

      array(

           'class'=>'CustomColumn',

       	'header'=>'custom header',

           'value'=>'custom value'

      ),

)



That should point you in the right direction. Let me know if you have any questions.

What exactly do you want to populate this additional column with? It could be as simple as just adding a column and specifying a function for calculating its content…

You could use raw type for columns. For example:


array(

    'name' => 'Column name',

    'type' => 'raw',

    'htmlOptions' => array(

        'style' => 'width: 100px; text-align: center;',

    ),

    'value' => 'CHtml::link( "Do it!", Yii::app()->createUrl("site/doit",array("id"=>$data->id)))',

),



or


array(

    'name' => 'Column name',

    'type' => 'raw',

    'value' => 'Just static text value',

),

Yes sorry for that question, I guess it was so simple.

Here is a thread which I found (too late): Link

But I am asking myself, if that isnt a bit expensive in terms of performance.

Thanks all for your help. I will look into your suggestions.

Is there a way to make a custom column sortable?

Here is the total solution for Customize grid view:) enjoy :rolleyes:




<div class="container m-tb-20">

 <div class="row">


<?php if(Yii::app()->user->hasFlash('CodeAdded')):?>

      <div class="info">

            

        <div id="CodeAdded"   class="alert alert-success"><?php echo Yii::app()->user->getFlash('CodeAdded'); ?></div>       

        </div>  

      <?php endif; ?>

     

 <div class="col-sm-12 col-xs-12"><h1 class="admin-heading">Manage Discount Codes:</h1></div>

 <div class="col-lg-12 col-sm-12 col-xs-12 m-b-20">

    <a href="<?php echo Yii::app()->request->baseurl."index.php/MasterDiscountCode/create" ?>" class="btn btn-lg btn-green pull-right">Add Discount Code </a>

 </div>

 

 <div class="col-lg-12 col-sm-12 col-sm-12 col-xs-12">

   <div class="table-responsive">     

       

       





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

	'id'=>'master-discount-code-grid',

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

        'template' => '{items}{pager}',

        'pager'=>array(

                        'header'=>'',

                        'htmlOptions'=>array('class'=>'pagination pull-right'),

                     ),

        'itemsCssClass' => 'table table-bordered',

        //'filter'=>$model,

	'columns'=>array(

//		'discount_id',

//		'discount_name',

//		'discount_code',

//		'discount_validity_date',

//		'discount_created_date',

//		'discount_update_date',

            

                array(

                        'name'=>'discount_name',

                        'value'=>'$data->discount_name',

                        'filter' => false,

                        'sortable'=>false,

                        //'header'=>'Email/Username'

                     ),

		

	

	

		

		array(

                        'name'=>'discount_code',

                        'value'=>'$data->discount_code',

                         //'header'=>'State',

                        'filter' => false,

                        'sortable'=>false,

                    ),

		

                 array(

                        'name'=>'discount_value',

                        'value'=>'$data->discount_value',

                         //'header'=>'State',

                        'filter' => false,

                        'sortable'=>false,

                      ),

            

                    array(

                        'name'=>'discount_validity_date',

                        'value'=>'$data->discount_validity_date',

                         //'header'=>'State',

                        'filter' => false,

                        'sortable'=>false,

                    ),

		   

		

		

		array(  'name'=>'discount_is_enabled',

                        'filter' => false,

                        'sortable'=>false,

                        'header'=>'Status',

                        'type'=>'raw',

                        'value'=> function ($data){ 

			        if($data->discount_is_enabled==1)

			    

			     return "<a title='Active' href='javascript:void(0)' onclick='change_status(0,".$data->discount_id.")'><img src='".Yii::app()->request->baseUrl."/images/active.png' ></a> ";

					else

					 return "<a title='In-active' href='javascript:void(0)' onclick='change_status(1,".$data->discount_id.")'><img src='".Yii::app()->request->baseUrl."/images/inactive.png' ></a> ";

			   }

		

		

		    ),

            

            

		array(

		  'header'=>'Actions',	

                                'class' => 'CButtonColumn',

                                'template' => ' {update}{delete}     ',

                                'buttons' => array(

                                    

//                                    'view' => array(

//                                                            'label' => 'View',

//                                                            'url' =>'$this->grid->controller->createUrl("view", array("id"=>$data->primaryKey,))',

//                                                            'imageUrl' => false,

//                                                            'options' => array('class' => 'btn btn-green '),

//                                                         ),

                                    

                                    'update' => array(

									   

                                                                'label' => 'Edit',

                                                                'url' => '$this->grid->controller->createUrl("update", array("id"=>$data->primaryKey,))',

                                                                'imageUrl' => false,

                                                                'options' => array('class' => 'btn btn-warning','id'=>'edit'),

                                                             ),

                                    

                                    'delete' => array(

									   

                                                                'label' => 'Delete',

                                                                'url' => '$this->grid->controller->createUrl("delete", array("id"=>$data->primaryKey, ))',


                                                                'imageUrl' => false,


                                                                'options' => array('class' => 'btn btn-danger','id'=>'delete'),

                                                             ),

                                    

                                    

                                ),

                            

		),

	),

)); ?>




 </div>

</div>