Permalink
Browse files

#2007 - Improvement of filter with relation in Cgridview

  • Loading branch information...
1 parent 1287d21 commit 3e4d7bbb95d6d4edf4a99ec365f41bf0370b416d @n3okill committed Jan 18, 2013
Showing with 48 additions and 19 deletions.
  1. +48 −19 framework/zii/widgets/grid/CDataColumn.php
@@ -1,4 +1,5 @@
<?php
+
/**
* CDataColumn class file.
*
@@ -7,7 +8,6 @@
* @copyright 2008-2013 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
-
Yii::import('zii.widgets.grid.CGridColumn');
/**
@@ -26,34 +26,39 @@
*/
class CDataColumn extends CGridColumn
{
+
/**
* @var string the attribute name of the data model. Used for column sorting, filtering and to render the corresponding
* attribute value in each data cell. If {@link value} is specified it will be used to rendered the data cell instead of the attribute value.
* @see value
* @see sortable
*/
public $name;
+
/**
* @var string a PHP expression that will be evaluated for every data cell and whose result will be rendered
* as the content of the data cells. In this expression, the variable
* <code>$row</code> the row number (zero-based); <code>$data</code> the data model for the row;
* and <code>$this</code> the column object.
*/
public $value;
+
/**
* @var string the type of the attribute value. This determines how the attribute value is formatted for display.
* Valid values include those recognizable by {@link CGridView::formatter}, such as: raw, text, ntext, html, date, time,
* datetime, boolean, number, email, image, url. For more details, please refer to {@link CFormatter}.
* Defaults to 'text' which means the attribute value will be HTML-encoded.
*/
- public $type='text';
+ public $type = 'text';
+
/**
* @var boolean whether the column is sortable. If so, the header cell will contain a link that may trigger the sorting.
* Defaults to true. Note that if {@link name} is not set, or if {@link name} is not allowed by {@link CSort},
* this property will be treated as false.
* @see name
*/
- public $sortable=true;
+ public $sortable = true;
+
/**
* @var mixed the HTML code representing a filter input (eg a text field, a dropdown list)
* that is used for this data column. This property is effective only when
@@ -72,9 +77,9 @@ class CDataColumn extends CGridColumn
public function init()
{
parent::init();
- if($this->name===null)
- $this->sortable=false;
- if($this->name===null && $this->value===null)
+ if($this->name === null)
+ $this->sortable = false;
+ if($this->name === null && $this->value === null)
throw new CException(Yii::t('zii','Either "name" or "value" must be specified for CDataColumn.'));
}
@@ -89,12 +94,35 @@ protected function renderFilterCellContent()
{
if(is_string($this->filter))
echo $this->filter;
- elseif($this->filter!==false && $this->grid->filter!==null && $this->name!==null && strpos($this->name,'.')===false)
+ elseif($this->filter !== false && $this->grid->filter !== null && $this->name !== null)
{
- if(is_array($this->filter))
- echo CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id'=>false,'prompt'=>''));
- elseif($this->filter===null)
- echo CHtml::activeTextField($this->grid->filter, $this->name, array('id'=>false));
+ if(strpos($this->name,'.') === false)
+ {
+ if(is_array($this->filter))
+ echo CHtml::activeDropDownList($this->grid->filter,$this->name,$this->filter,array('id' => false,'prompt' => ''));
+ elseif($this->filter === null)
+ echo CHtml::activeTextField($this->grid->filter,$this->name,array('id' => false));
+ }
+ else
+ {
+ $segs = explode('.',$this->name);
+ $name = array_pop($segs);
+ $model = $this->grid->filter;
+ foreach($segs as $seg)
+ {
+ if(isset($model->$seg))
+ $model = $model->$seg;
+ else
+ parent::renderFilterCellContent();
+ }
+ if(is_array($this->filter))
+ {
+ echo CHtml::activeDropDownList($model,$name,$this->filter,array('id' => false,'prompt' => ''));
+ }elseif($this->filter === null)
+ {
+ echo CHtml::activeTextField($model,$name,array('id' => false));
+ }
+ }
}
else
parent::renderFilterCellContent();
@@ -106,9 +134,9 @@ protected function renderFilterCellContent()
*/
protected function renderHeaderCellContent()
{
- if($this->grid->enableSorting && $this->sortable && $this->name!==null)
- echo $this->grid->dataProvider->getSort()->link($this->name,$this->header,array('class'=>'sort-link'));
- elseif($this->name!==null && $this->header===null)
+ if($this->grid->enableSorting && $this->sortable && $this->name !== null)
+ echo $this->grid->dataProvider->getSort()->link($this->name,$this->header,array('class' => 'sort-link'));
+ elseif($this->name !== null && $this->header === null)
{
if($this->grid->dataProvider instanceof CActiveDataProvider)
echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));
@@ -127,10 +155,11 @@ protected function renderHeaderCellContent()
*/
protected function renderDataCellContent($row,$data)
{
- if($this->value!==null)
- $value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
- elseif($this->name!==null)
- $value=CHtml::value($data,$this->name);
- echo $value===null ? $this->grid->nullDisplay : $this->grid->getFormatter()->format($value,$this->type);
+ if($this->value !== null)
+ $value = $this->evaluateExpression($this->value,array('data' => $data,'row' => $row));
+ elseif($this->name !== null)
+ $value = CHtml::value($data,$this->name);
+ echo $value === null ? $this->grid->nullDisplay : $this->grid->getFormatter()->format($value,$this->type);
}
+
}

0 comments on commit 3e4d7bb

Please sign in to comment.