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:
-
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).
-
Is this the only code I would need to get the filter correctly working?
Any help most appreciated.