Make Searchable And Sortable Other Model Field In Gridview

I have 3 models (SALE, STOCK, PRODUCT) where SALE is connected to STOCK while STOCK is connected to PRODUCT. I have used relation to show data in TbGridview (You can check view file below). I am not able to enable search and sort this field in TbGridview widget. How can I enable both sortable and searchable this field.

Sale Model




class Sale extends CActiveRecord

{

........

public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

 		return array(

 				'stock_relation' => array(self::BELONGS_TO, 'Stock', array('id'=>'master_id')),

 		);

	}


public function search()

{

$criteria=new CDbCriteria;

$criteria->compare('sale_date',$this->sale_date,true);

$criteria->compare('client_id',$this->client_id,true);

$criteria->addCondition('edt is null');

return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

				'sort'=>array(

						'attributes'=>array(

								'sale_date', 'client_id','discount', //here how can i can field which is related to stock model

						),

				),

				'pagination'=>array(

						'pageSize'=>Constants::PAGE_RECORDS_LIMIT,

				),

		));


}

........

}



Sale View





{

........

Yii::import('ext.chosen.Chosen');

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

		'type'=>'striped bordered condensed',

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

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

		'filter'=>$model,

		//'afterAjaxUpdate' => 'reinstallDatePicker',

		'afterAjaxUpdate' => 'applyChosen',

		'columns' => array(

				array(

						'name' => 'sale_date',

						'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', 

								array(

									'model'=>$model,

									'attribute'=>'sale_date',

									'language' => 'en',

									'htmlOptions' => array(

											'id' => 'datepicker_for_search',

											'size' => '10',

									),

									'defaultOptions' => array(  // (#3)

											'showOn' => 'focus',

											'dateFormat' => 'yy-mm-dd',

											'showOtherMonths' => true,

											'selectOtherMonths' => false,

											'changeMonth' => true,

											'changeYear' => true,

											'showButtonPanel' => false,

									)

								),//*/

								true),

				),

				array('name'=>'client_id', 'value'=>'Clients::model()->findByPk($data->client_id)->name',

				'filter' =>//

						Chosen::activeDropDownList($model, "client_id",  CHtml::listData(Clients::model()->findall("edt IS NULL ORDER BY name"), 'id', 'name'),  array('class'=>'chosen'))),

				'discount',

				array(

						'name'=>'productid', 'header'=>'Product',

						'value' => 'Products::model()->findByPk($data->stock_relation->product_id)->name',

						'filter'=>CHtml::listData(Products::model()->findall("edt IS NULL ORDER BY name"), 'id', 'name')

				),

				array(

						'name'=>'sale_quantity', 'header'=>'Sold Qty',

						'value' => '$data->stock_relation->stock_out',

				),

				array(

						'name'=>'sale_price', 'header'=>'Sale Price',

						'value' => '$data->stock_relation->sale_price',

				),

				

			array(

						'class'=>'bootstrap.widgets.TbButtonColumn',

						'htmlOptions'=>array('style'=>'width: 50px'),

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

						'buttons'=>array(

								'delete' => array(

										'visible'=>'false'

								),

						),

				),

		),

));


Yii::app()->clientScript->registerScript('apply-Chosen', "function applyChosen(id, data) { 

		$('select.chosen').prepend('<option value=\"\"></option>').data('placeholder','Select option').chosen(); 

		//$('select.chosen').prepend(\"<option value=''></option>\").chosen();

}");

........

}




Check out my extension: RelatedSearchBehavior .

Learn from the demo.