Filter for a custom column

Hello! I have an issue regarding a column whose values have been added in GridView through a model function.

I don’t have any relation with the tables from where I get the results.

I would like to have also for this column the filter option/search, but I didn’t find any solution yet.

My column in GridView:


array(

			'header'=>'Categories',

			'value' => '$data->categories($data["id"])',

			'htmlOptions' => array('width'=>'200'),

			'headerHtmlOptions' => array('width'=>'200'),

			),

Thank you,

Denisa

you have to provide setter for this attribute (I assume you already have getter), add this attribute to ‘safe’ on ‘search’ and add some code to ‘search’ function which prepares CActiveRecordDataProvider with search results.

It is not clear for me about the name for this attribute…if I add in the array name=>‘categories’ it will throw error that the property doesn’t exist. How should I add this setter and getter methods?




public function setCategories($value){

  $this->categories = $value;

}



Is this about? I didn’t do until now in Yii this things.

Thanks,

Well, there must be a relationship between the data in the database and the categories you display - either that or your application makes no sense :P Either way, would you mind posting your categories() method so we can see exactly what it’s doing?

This may also help you: http://www.yiiframework.com/wiki/117/using-standard-filters-in-cgridview-custom-fields/

Hi,




public function categories($id) {

    $criteria = new CDbCriteria();

$criteria->select = array('cat.name');

    $criteria->join = 'INNER JOIN {{t1}} AS l ON t.id = l.dealclub_id INNER JOIN {{t2}} AS lt on lt.list_id = l.id

						INNER JOIN {{t3}} AS cat ON cat.id = lt.category_id';

    $criteria->condition = 't.id = :id';

    $criteria->params = array(':id' => $id);

	$categories = Dealclub::model()->findAll($criteria);

	$cat = array();

	$categs = '';

	foreach($categories as $category)

		//$categs = $category['name'].',';

		$cat[] = $category['name'];


	$categs = implode(', ', $cat);

    return $categs;

  }



and this is in my view




array(

			'header'=>'Categories',

			'value' => '$data->categories($data["id"])',

			'htmlOptions' => array('width'=>'200'),

			'headerHtmlOptions' => array('width'=>'200'),

			),



It’s 2am here, and I’m about to head off without really studying your code. However, having had a brief glance, it looks like you’d be better of creating the necessary relationships between your models rather than manually performing the database operations as you have above. If you were to take the route of setting up those relations, you should be able to set the filter up as you normally would within the grid view.

I added the new field. Almost everything is working right, except one thing.

If I want to search something in the custom field I have just added, I noticed that with pagination it will not work right. If there are more results than on a page…for the first page the results are fine …the count number of results is fine…but when I go to page 2 the count results will be reset and also there will be listed all the records again, of course with pagination. I would like to be able to see the results for my search in the page 2 and so on…

Do you have any idea? I think is an issue related to Yii pagination…but I coudnt’t find any solution yet.

Thanks,

Post your code so I can have a look.