title: Yii Gridview Change Column Value Using Ajax
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Here is the gridview in question.</p>
  
  <pre><code>$this-&gt;widget('bootstrap.widgets.TbGridView', array(
      'id' =&gt; 'product-news-grid-'. $id,
      'itemsCssClass' =&gt; 'table table-striped',
      'htmlOptions' =&gt; array(
          'class' =&gt; 'news-datagrid',
      ),
      'dataProvider' =&gt; $dataProvider-&gt;searchProductNewsSymbol($name, $headlines, $publish_date), 
      'filter' =&gt; $dataProvider,
      'enableHistory' =&gt; false,
      'ajaxUpdate' =&gt; 'product-news-grid-'. $id,
      'ajaxUrl' =&gt; Yii::app()-&gt;createUrl('/productDetails/AjaxUpdateProductNews'),
      'pager' =&gt; array(
          'header' =&gt; '',
          'cssFile' =&gt; false,
          'maxButtonCount' =&gt; 5,
          'selectedPageCssClass' =&gt; 'active',
          'hiddenPageCssClass' =&gt; 'disabled',
          'firstPageCssClass' =&gt; 'previous',
          'lastPageCssClass' =&gt; 'next',
          'firstPageLabel' =&gt; '&lt;&lt;',
          'lastPageLabel' =&gt; '&gt;&gt;',
          'prevPageLabel' =&gt; '&lt;',
          'nextPageLabel' =&gt; '&gt;',
      ),
      'summaryCssClass' =&gt; 'label label-warning',
      'columns' =&gt; array(
          array(
              'name' =&gt; 'name',
              'header' =&gt; 'Name',
              'value' =&gt; function($data) {
                  return '&lt;div class="product-name"&gt; &lt;a target="_blank" href="'. $data-&gt;id .'" &gt; '. $data-&gt;name .'&lt;/a&gt;&lt;/div&gt;';
              },
              'type' =&gt; 'raw',
          ),
          array(
              'name' =&gt; 'headlines',
              'header' =&gt; 'Headlines',
              'value' =&gt; function($data) {
                  return '&lt;div class="product-news"&gt; &lt;a target="_blank" href="'. $data-&gt;link .'" &gt; '. $data-&gt;headlines .'&lt;/a&gt;&lt;/div&gt;';
              },
              'type' =&gt; 'raw',
          ),
          array(
              'name' =&gt; 'publish_date',
              'header' =&gt; 'Date',
              'value' =&gt; function($data) {
                  return '&lt;span class="news-pub-date"&gt;'. $data-&gt;publish_date .'&lt;/span&gt;';
              },
              'type' =&gt; 'raw',
          ),
          array(
              'name' =&gt; 'fived_roi',
              'header' =&gt; '5d ROI',
              'value' =&gt; function($data) {
                  return '&lt;span class="news-pub-date"&gt;'. $data-&gt;fived_roi .'&lt;/span&gt;';
              },
              'type' =&gt; 'raw',
          ),
           array(
              'name' =&gt; 'sixm_roi',
              'header' =&gt; '6m ROI',
              'value' =&gt; function($data) {
                  return '&lt;span class="news-pub-date"&gt;'. $data-&gt;sixm_roi .'&lt;/span&gt;';
              },
              'type' =&gt; 'raw',
          ),
            array(
              'name' =&gt; 'twlm_roi',
              'header' =&gt; '12m ROI',
              'value' =&gt; function($data) {
                  return '&lt;span class="news-pub-date"&gt;'. $data-&gt;twlm_roi .'&lt;/span&gt;';
              },
              'type' =&gt; 'raw',
          ),
           array(
              'name' =&gt; 'fived_profit',
              'header' =&gt; 'Total Profit %',
              'value' =&gt; function($data) {
                  return '&lt;span class="news-pub-date"&gt;'. $data-&gt;fived_profit .'%&lt;/span&gt;';
              },
              'type' =&gt; 'raw',
          ),
           array(
              'name' =&gt; 'sixm_profit',
              'header' =&gt; 'Total Profit %',
              'value' =&gt; function($data) {
                  return '&lt;span class="news-pub-date"&gt;'. $data-&gt;sixm_profit .'%&lt;/span&gt;';
              },
              'type' =&gt; 'raw',
              'visible' =&gt; false
          ),
           array(
              'name' =&gt; 'twlm_profit',
              'header' =&gt; 'Total Profit %',
              'value' =&gt; function($data) {
                  return '&lt;span class="news-pub-date"&gt;'. $data-&gt;twlm_profit .'%&lt;/span&gt;';
              },
              'type' =&gt; 'raw',
              'visible' =&gt; true
          ),
  
  
      )
  ));
  
  ?&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>So can anyone point me in the right direction on how to achieve the desired result. I would appreciate any help.</p>
  
  <p>Thanks,
  Maxx </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is the code for searchProductNewsSymbol() </p>
  
  <pre><code>public function searchProductNewsSymbol ( $name ,
                                              $headlines = NULL ,
                                              $publish_date = NULL ) {
  
          // @todo Please modify the following code to remove attributes that should not be searched.
  
          $criteria = new CDbCriteria ;
  
  
          $criteria-&gt;select = 'name, publish_timestamp, publish_date, headlines, link, last_updated';
          $criteria-&gt;group = 'headlines';
          $criteria-&gt;compare ( 'name' ,
                               $name ,
                               true ) ;
  
          if ( isset ( $headlines ) || $headlines != "" ) {
              $criteria-&gt;compare ( 'headlines' ,
                                   $headlines ,
                                   TRUE ,
                                   'AND' ) ;
          }
  
          if ( isset ( $publish_date ) || $publish_date != "" ) {
              $criteria-&gt;compare ( 'publish_date' ,
                                   $publish_date ,
                                   TRUE ,
                                   'AND' ) ;
          }
          return new CActiveDataProvider ( $this ,
                                           array (
                                   'criteria' =&gt; $criteria ,
                                   'sort'     =&gt; array (
                                                            'defaultOrder' =&gt; 'publish_date desc' ,
                                   ) ,
                  ) ) ;
      }
  </code></pre>