CGridView add sortable column

Hi,

I have two columns in my datagrid that I’m adding together and displaying in a third column. I can do this but the datagrid won’t let me sort the calculated column. Here’s my code:




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

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		'risk_id',

		'description',

		/*'objective',*/

		'likelihood',

		'rank',

         array(

                'header'=>'Score',

                'value'=>'$data->rank + $data->likelihood',

                'name'=>'$data->rank + $data->likelihood',

                'sortable'=>true,

                ),



I’m sure that I’m doing something wrong with the ‘Name’ property of the DataColumn but I can’t work out where the problem is.

CSort handles those sorting options, CSort checks if the name is in the attribute list of the AR object (meaning the name exists as a column in the underlying table), If it is it will allow sorting otherwise it won’t. The only option for you is to add an attribute entitled ‘score’ that will be used as the result of the code ($data->rank + $data->likelihood) you have above. You will need to override several methods in the AR object to include that attribute dynamically.

Thanks Vince. I was wondering if that was the case. It might be easier to just store the score in the DB.