Yii Framework Forum: Creating custom grid columns - Yii Framework Forum

Jump to content

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

Creating custom grid columns Chapter 7, Page 203

#1 User is offline   mikewalen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 27-August 11
  • Location:Hamilton, New Zealand

Posted 18 October 2011 - 10:13 PM

Hi All,

I'm a Yii newbie posting for the first time. I'm working my way through the cookbook and have learnt how to do so much. Many thanks to Alexander Makarov and the other contributors.

I'm currently implementing the 'Creating custom grid columns' feature (Chapter 7, Page 203), and it's working perfectly, but I'm wondering if someone can help me set up a filter for my FlagColumn.

I am currently using the code exactly as it appears in the cookbook on page 203, so I have a FlagColumn.php file which contains the following...

<?php
/*
 * A custom grid view column to change the value
 * of a BOOLEAN database field using AJAX
 */

class FlagColumn extends CGridColumn
{
	public $name;
	public $sortable=true;
	public $callbackUrl = array('flag');
	private $_flagClass = "flag_link";
	
	public function init()
	{		
		parent::init();
		
		$cs=Yii::app()->getClientScript();
		$gridId = $this->grid->getId();
		$script = <<<script
		jQuery(".{$this->_flagClass}").live("click", function(e){
			e.preventDefault();
			var link = this;
			$.ajax({
				dataType: "json",
				cache: false,
				url: link.href,
				success: function(data){
					$('#$gridId').yiiGridView.update('$gridId');
				}
			});
		});
SCRIPT;
		$cs->registerScript(__CLASS__.$gridId.'#flag_link', $script);
	}
				
	protected function renderDataCellContent($row, $data)
	{
		$value = cHtml::value($data, $this->name);
		
		$this->callbackUrl['pk'] = $data->primaryKey;
		$this->callbackUrl['name'] = urlencode($this->name);
		$this->callbackUrl['value'] = (int)empty($value);
		
		$link = CHtml::normalizeUrl($this->callbackUrl);
				
		echo CHtml::link(!empty($value) ? 'Yes' : 'No', $link, array(
			'class'=>$this->_flagClass,
		));
	}
	
	protected function renderHeaderCellContent()
	{
		if($this->grid->enableSorting && $this->sortable && $this->name!==null)
		{
			echo $this->grid->dataProvider->getSort()->link($this->name, $this->header);
		}
		else if($this->name!==null && $this->header===null)
		{
			if($this->grid->dataProvider instanceof CActiveDataProvider)
			{
				echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));
			}
			else
			{
				echo CHtml::encode($this->name);
			}
		}
		else
		{
			parent::renderHeaderCellContent();
		}
	}
}


So as I mentioned, this all works fine, but I need to add a filter to the top of the column, preferably a drop down list that will have the values '', '0' and '1' (to enable filtering of the boolean field in my DB).

I know I need to implement the renderFilterCellContent() function, but I can't get it to work. I can get the drop down list to render, with the 3 list items, but selecting any of the items doesn't filter the list. Here's the code I'm trying to use...

	protected function renderFilterCellContent()
	{
		echo CHtml::dropDownList($this->name, '', array(''=>'All','0'=>'No','1'=>'Yes'));
	}


My questions are:
1. How do I reference the selected value parameter from the grid, after the filter is applied to put in place of the '' (second parameter required for CHtml::dropDownList).
2. Is this the only code I would need to get the filter correctly working?

Any help most appreciated.
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,350
  • Joined: 17-January 09
  • Location:Russia

Posted 19 October 2011 - 11:23 AM

In order for the filter to work properly you need to check your search() method (this one is in the model's code) and add condition for the field you're trying to filter.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#3 User is offline   mikewalen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 27-August 11
  • Location:Hamilton, New Zealand

Posted 26 October 2011 - 03:05 PM

Many thanks for taking the time to reply Sam. I'll check out the model as you suggest and go from there :)
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