Yii Gridview Change Column Value Using Ajax

Hi, i’m facing a troublesome scenario for quite sometime without any clear solution to it. The problem is that i have a gridview which shows products information and return on investment for 5d, 6m, and 12m and another column called total profitable %. This total profitable % column is calculated for each months separately and is stored in separate columns in the database.

Now the thing is , how to make this column called total profitable % show the info for the roi month that the user is sorting on currently. So if the user sorts on 5d ROI column the total profitable % will recalculate to 5 days total profitable %. If the user sorts on 6m ROI column then the total profitable % will again recalculate to 6m , and so on. I’ve been trying to find a way to achieve this by setting the “visible” property to false for the total profitable % column for all the month except for the one that the user is sorting the ROI column on, but its not working at all.

Here is the gridview in question.





$this->widget('bootstrap.widgets.TbGridView', array(

    'id' => 'product-news-grid-'. $id,

    'itemsCssClass' => 'table table-striped',

    'htmlOptions' => array(

        'class' => 'news-datagrid',

    ),

    'dataProvider' => $dataProvider->searchProductNewsSymbol($symbol, $headlines, $publish_date), 

    'filter' => $dataProvider,

    'enableHistory' => false,

    'ajaxUpdate' => 'product-news-grid-'. $id,

    'ajaxUrl' => Yii::app()->createUrl('/productDetails/AjaxUpdateProductNews'),

    'pager' => array(

        'header' => '',

        'cssFile' => false,

        'maxButtonCount' => 5,

        'selectedPageCssClass' => 'active',

        'hiddenPageCssClass' => 'disabled',

        'firstPageCssClass' => 'previous',

        'lastPageCssClass' => 'next',

        'firstPageLabel' => '<<',

        'lastPageLabel' => '>>',

        'prevPageLabel' => '<',

        'nextPageLabel' => '>',

    ),

    'summaryCssClass' => 'label label-warning',

    'columns' => array(

        array(

            'name' => 'name',

            'header' => 'Name',

            'value' => function($data) {

                return '<div class="product-name"> <a target="_blank" href="'. $data->id .'" > '. $data->name .'</a></div>';

            },

            'type' => 'raw',

        ),

        array(

            'name' => 'headlines',

            'header' => 'Headlines',

            'value' => function($data) {

                return '<div class="product-news"> <a target="_blank" href="'. $data->link .'" > '. $data->headlines .'</a></div>';

            },

            'type' => 'raw',

        ),

        array(

            'name' => 'publish_date',

            'header' => 'Date',

            'value' => function($data) {

                return '<span class="news-pub-date">'. $data->publish_date .'</span>';

            },

            'type' => 'raw',

        ),

        array(

            'name' => 'fived_roi',

            'header' => '5d ROI',

            'value' => function($data) {

                return '<span class="news-pub-date">'. $data->fived_roi .'</span>';

            },

            'type' => 'raw',

        ),

         array(

            'name' => 'sixm_roi',

            'header' => '6m ROI',

            'value' => function($data) {

                return '<span class="news-pub-date">'. $data->sixm_roi .'</span>';

            },

            'type' => 'raw',

        ),

          array(

            'name' => 'twlm_roi',

            'header' => '12m ROI',

            'value' => function($data) {

                return '<span class="news-pub-date">'. $data->twlm_roi .'</span>';

            },

            'type' => 'raw',

        ),

         array(

            'name' => 'fived_profit',

            'header' => 'Total Profit %',

            'value' => function($data) {

                return '<span class="news-pub-date">'. $data->fived_profit .'%</span>';

            },

            'type' => 'raw',

        ),

         array(

            'name' => 'sixm_profit',

            'header' => 'Total Profit %',

            'value' => function($data) {

                return '<span class="news-pub-date">'. $data->sixm_profit .'%</span>';

            },

            'type' => 'raw',

            'visible' => false

        ),

         array(

            'name' => 'twlm_profit',

            'header' => 'Total Profit %',

            'value' => function($data) {

                return '<span class="news-pub-date">'. $data->twlm_profit .'%</span>';

            },

            'type' => 'raw',

            'visible' => true

        ),




    )

));

            

?>






I tried to make this work by attaching a click handler to the table column headers , and rendering a different view which has anyone of the Total profitable % column visible. But its not working, cause the sorting doesn’t work and the whole grid falls apart after rendering a different one.

So can anyone point me in the right direction on how to achieve the desired result. I would appreciate any help.

Thanks,

Maxx