CGridview apply sorting on user-defined field

Hi,

I am using CGridview for display my model’s data. There are 2 columns as “Subject1” and “Subject2” which display mark of those 2 subjects. In third column I want to display Total of those 2 columns. My code is for third column is :

[b]array(

  'header'=>'Total',


  'value'=>'(($data->sub1 + $data->sub2) > 0) ? $data->sub1 + $data->sub2 : 0',

),[/b]

This code will display Total of Subject1 and Subject2. But "Total" column is not Sortable. Subject1 and Subject2 both columns are sortable.

How can I make "Total" column sortable?

Thanks in Advance!!

I did not check that, but I think similar to http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

you need to give it a name:




array(

'name'=>'total_column',

'header'=>'Total',

'value'=>'(($data->sub1 + $data->sub2) > 0) ? $data->sub1 + $data->sub2 : 0',

),



and provide sorting information for that name in your dataprovider:




new CActiveDataProvider( 'Post', array(

    'criteria'=>$criteria,

    'sort'=>array(

        'attributes'=>array(

            'total_column'=>array(

                'asc'=>'t.subtotal1 + t.subtotal2',

                'desc'=>'( t.subtotal1 + t.subtotal2 ) DESC',

            ),

            '*',

        ),

    ),

));



Thank you!! It works fine for me.