add custom textbox filter in CGridView

Hi i want to add textbox filter in CGridView as i show data from other model in column,

this is my code for show data in column from other model which work perfectly. but textbox filter on top for filter data not shown,


array(

		'header'=>'First Name',

		'value'=>'ModelName::model()->findbyPk($data->model_pk_id)->first_name',   // this works fine

		'filter'=>'<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />'),      // this had to do

),

what i have to do to show textbox filter?

do i have to use multiple dataprovider or else?

if multiple dataprovider then how?

Thankx…

Hi,

if your relations are declared on the model, you can try something like my example:




'filter' => CHtml::listData(Languages::model()->findAll(), 'language_name', 'language_name'),



@ragua,

yes i had seen that relation declaration in model, but is there any other way to filter it?

You must also have in the search()




...

$criteria->with = array( 'language', ... ,);

$criteria->compare( 'language.language_name', $this->language_search, true );

...



For filtering, sorting and searching related models, you may want to refer to this wiki:

http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview

When we use multi model and give relation as BELONGS_TO it shows data in CGridView but filter on top disappear ( disable ),

is there a way to enable textbox filter??

Thankx…

Yes, I got it. That’s why I gave you the link above. Have you read it? Go on, it’s very easy to do, and if there’s a problem, share your codes and we’ll be happy to help.

@bennouna, very thnkx for reply,

I had code like below in CGridView ,


array(

		'name'=>'photographer_id',

		'header'=>'Photographer',

		'value'=>'$data->photographer->first_name',

		'filter' => CHtml::listData(PhotographerDetail::model()->findAll(), 'photographer_id', 'first_name'),

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


		),

as you can see currently i had use listData … as it shows dropdown of name array which working fine and filter well, and i perfect data in as value.

but i want textbox as filter, so do you had idea how to …

i also had tried CHtml::activeTextField but not working.

Thankx…

Hello Kiran.

I don’t know how to say it in another way. Please check the wiki here in this link, it’s the perfect way to do what you want to do :)

You’ll end up with a code like this:


$this->widget('zii.widgets.grid.CGridView', array(

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

    'filter'=>$model,

    'columns'=>array(

        …

        array(

            'name' => 'photographer_search', // please check the wiki

            'value' => '$data->photographer->first_name'

        ),

        array(

            'class'=>'CButtonColumn',

        ),

    ),

));

And you’ll have a textbox to filter your photographer first names.

Let me know if you can’t understand something in the wiki.

thnkx bennouna,

now its working, i had minor mistake in define name for column in view file.

+1 for your answer :)

Thankx…

Welcome and glad I could help

My Relation function

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(


		'menu' =&gt; array(self::BELONGS_TO, 'RestoMenu', 'menuId'),


		'restoItemtoingrdients' =&gt; array(self::HAS_MANY, 'RestoItemtoingrdient', 'itemId'),


	);


}

my search function

public function search()

{


	// @todo Please modify the following code to remove attributes that should not be searched.





	&#036;criteria=new CDbCriteria;





	&#036;criteria-&gt;compare('id',&#036;this-&gt;id);


	&#036;criteria-&gt;compare('name',&#036;this-&gt;name,true);


	&#036;criteria-&gt;compare('price',&#036;this-&gt;price,true);


	&#036;criteria-&gt;compare('menuId',&#036;this-&gt;menuId);


	&#036;criteria-&gt;compare('type',&#036;this-&gt;type,true);


	&#036;criteria-&gt;compare('new_item',&#036;this-&gt;new_item);


	&#036;criteria-&gt;compare('unit',&#036;this-&gt;unit,true);


	&#036;criteria-&gt;compare('quantity',&#036;this-&gt;quantity);


	&#036;criteria-&gt;compare('description',&#036;this-&gt;description,true);





	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


	));


}

my gridview is

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(

'id'=&gt;'resto-item-grid',


'dataProvider'=&gt;&#036;model-&gt;search(),


'filter'=&gt;&#036;model,


'columns'=&gt;array(


	'id',


	'name',


	'price',


	'menu.name',


	'type',


	'new_item',


	/*


	'unit',


	'quantity',


	'description',


	*/


	array(


		'class'=&gt;'CButtonColumn',


	),


),

)); ?>

i want to add texbox filter for search with menu name but it just displays the texbox after changes

below are my changes:

search function:

public function search()

{


	// @todo Please modify the following code to remove attributes that should not be searched.





	&#036;criteria=new CDbCriteria;


	


	&#036;criteria-&gt;with = array( 'menu' );


	&#036;criteria-&gt;compare( 'menu.name', &#036;this-&gt;name, true );


	


	&#036;criteria-&gt;compare('id',&#036;this-&gt;id);


	&#036;criteria-&gt;compare('name',&#036;this-&gt;name,true);


	&#036;criteria-&gt;compare('price',&#036;this-&gt;price,true);


	&#036;criteria-&gt;compare('menuId',&#036;this-&gt;menuId);


	&#036;criteria-&gt;compare('type',&#036;this-&gt;type,true);


	&#036;criteria-&gt;compare('new_item',&#036;this-&gt;new_item);


	&#036;criteria-&gt;compare('unit',&#036;this-&gt;unit,true);


	&#036;criteria-&gt;compare('quantity',&#036;this-&gt;quantity);


	&#036;criteria-&gt;compare('description',&#036;this-&gt;description,true);





	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


		'sort'=&gt;array(


			'attributes'=&gt;array(


				'name'=&gt;array(


					'asc'=&gt;'menu.name',


					'desc'=&gt;'menu.name DESC',


				),


				'*',


			),


		),


	));


}

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(

'id'=&gt;'resto-item-grid',


'dataProvider'=&gt;&#036;model-&gt;search(),


'filter'=&gt;&#036;model,


'columns'=&gt;array(


	'id',


	'name',


	'price',


	'menu.name',


	array( 'name'=&gt;'name', 'value'=&gt;'&#036;data-&gt;menu-&gt;name' ),


	'type',


	'new_item',


	/*


	'unit',


	'quantity',


	'description',


	*/


	array(


		'class'=&gt;'CButtonColumn',


	),


),

)); ?>

this is not working

following error occurs:

Error 500: <h1>CDbException</h1>

<p>CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘name’ in where clause is ambiguous. The SQL statement executed was: SELECT COUNT(DISTINCT t.id) FROM tbl_resto_item t LEFT OUTER JOIN tbl_resto_menu menu ON (t.menuId=menu.id) WHERE ((menu.name LIKE :ycp0) AND (name LIKE :ycp1)) (C:\wamp\www\yii\framework\db\CDbCommand.php:543)</p><pre>#0 C:\wamp\www\yii\framework\db\CDbCommand.php(433): CDbCommand->queryInternal(‘fetchColumn’, 0, Array)

#1 C:\wamp\www\yii\framework\db\ar\CActiveFinder.php(764): CDbCommand->queryScalar()

#2 C:\wamp\www\yii\framework\db\ar\CActiveFinder.php(151): CJoinElement->count(Object(CDbCriteria))

#3 C:\wamp\www\yii\framework\db\ar\CActiveRecord.php(1610): CActiveFinder->count(Object(CDbCriteria))

#4 C:\wamp\www\yii\framework\web\CActiveDataProvider.php(228): CActiveRecord->count(Object(CDbCriteria))

#5 C:\wamp\www\yii\framework\web\CDataProvider.php(224): CActiveDataProvider->calculateTotalItemCount()

#6 C:\wamp\www\yii\framework\web\CActiveDataProvider.php(178): CDataProvider->getTotalItemCount()

#7 C:\wamp\www\yii\framework\web\CDataProvider.php(168): CActiveDataProvider->fetchData()

#8 C:\wamp\www\yii\framework\zii\widgets\CBaseListView.php(125): CDataProvider->getData()

#9 C:\wamp\www\yii\framework\zii\widgets\grid\CGridView.php(339): CBaseListView->init()

#10 C:\wamp\www\yii\framework\web\CBaseController.php(147): CGridView->init()

#11 C:\wamp\www\yii\framework\web\CBaseController.php(180): CBaseController->createWidget(‘zii.widgets.gri…’, Array)

#12 C:\wamp\www\yii\testdrive\protected\views\restoItem\admin.php(64): CBaseController->widget(‘zii.widgets.gri…’, Array)

#13 C:\wamp\www\yii\framework\web\CBaseController.php(126): require(‘C:\wamp\www\yii…’)

#14 C:\wamp\www\yii\framework\web\CBaseController.php(95): CBaseController->renderInternal(‘C:\wamp\www\yii…’, Array, true)

#15 C:\wamp\www\yii\framework\web\CController.php(869): CBaseController->renderFile(‘C:\wamp\www\yii…’, Array, true)

#16 C:\wamp\www\yii\framework\web\CController.php(782): CController->renderPartial(‘admin’, Array, true)

#17 C:\wamp\www\yii\testdrive\protected\controllers\RestoItemController.php(143): CController->render(‘admin’, Array)

#18 C:\wamp\www\yii\framework\web\actions\CInlineAction.php(49): RestoItemController->actionAdmin()

#19 C:\wamp\www\yii\framework\web\CController.php(308): CInlineAction->runWithParams(Array)

#20 C:\wamp\www\yii\framework\web\filters\CFilterChain.php(133): CController->runAction(Object(CInlineAction))

#21 C:\wamp\www\yii\framework\web\filters\CFilter.php(40): CFilterChain->run()

#22 C:\wamp\www\yii\framework\web\CController.php(1145): CFilter->filter(Object(CFilterChain))

#23 C:\wamp\www\yii\framework\web\filters\CInlineFilter.php(58): CController->filterAccessControl(Object(CFilterChain))

#24 C:\wamp\www\yii\framework\web\filters\CFilterChain.php(130): CInlineFilter->filter(Object(CFilterChain))

#25 C:\wamp\www\yii\framework\web\CController.php(291): CFilterChain->run()

#26 C:\wamp\www\yii\framework\web\CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)

#27 C:\wamp\www\yii\framework\web\CWebApplication.php(282): CController->run(‘admin’)

#28 C:\wamp\www\yii\framework\web\CWebApplication.php(141): CWebApplication->runController(‘restoitem/admin’)

#29 C:\wamp\www\yii\framework\base\CApplication.php(184): CWebApplication->processRequest()

#30 C:\wamp\www\yii\testdrive\index.php(13): CApplication->run()

#31 {main}</pre>