Yii Framework Forum: Make Searchable And Sortable Other Model Field In Gridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Make Searchable And Sortable Other Model Field In Gridview Rate Topic: -----

#1 User is offline   Javed Iqbal 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 25-October 11

Posted 21 March 2014 - 02:42 AM

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();
}");
........
}


Javed Iqbal

Web Developer
syntecX Solution
skype:javedboqo
0

#2 User is offline   le_top 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 325
  • Joined: 08-June 10
  • Location:France

Posted 02 August 2014 - 06:14 PM

Check out my extension: RelatedSearchBehavior .
Learn from the demo.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users