3
* CDataColumn class file.
5
* @author Qiang Xue <qiang.xue@gmail.com>
6
* @link http://www.yiiframework.com/
7
* @copyright Copyright © 2008-2011 Yii Software LLC
8
* @license http://www.yiiframework.com/license/
11
Yii::import('zii.widgets.grid.CGridColumn');
14
* CDataColumn represents a grid view column that is associated with a data attribute or expression.
16
* Either {@link name} or {@link value} should be specified. The former specifies
17
* a data attribute name, while the latter a PHP expression whose value should be rendered instead.
19
* The property {@link sortable} determines whether the grid view can be sorted according to this column.
20
* Note that the {@link name} should always be set if the column needs to be sortable. The {@link name}
21
* value will be used by {@link CSort} to render a clickable link in the header cell to trigger the sorting.
23
* @author Qiang Xue <qiang.xue@gmail.com>
24
* @version $Id: CDataColumn.php 2799 2011-01-01 19:31:13Z qiang.xue $
25
* @package zii.widgets.grid
28
class CDataColumn extends CGridColumn
31
* @var string the attribute name of the data model. The corresponding attribute value will be rendered
32
* in each data cell. If {@link value} is specified, this property will be ignored
33
* unless the column needs to be sortable.
39
* @var string a PHP expression that will be evaluated for every data cell and whose result will be rendered
40
* as the content of the data cells. In this expression, the variable
41
* <code>$row</code> the row number (zero-based); <code>$data</code> the data model for the row;
42
* and <code>$this</code> the column object.
46
* @var string the type of the attribute value. This determines how the attribute value is formatted for display.
47
* Valid values include those recognizable by {@link CGridView::formatter}, such as: raw, text, ntext, html, date, time,
48
* datetime, boolean, number, email, image, url. For more details, please refer to {@link CFormatter}.
49
* Defaults to 'text' which means the attribute value will be HTML-encoded.
53
* @var boolean whether the column is sortable. If so, the header cell will contain a link that may trigger the sorting.
54
* Defaults to true. Note that if {@link name} is not set, or if {@link name} is not allowed by {@link CSort},
55
* this property will be treated as false.
58
public $sortable=true;
60
* @var mixed the HTML code representing a filter input (e.g. a text field, a dropdown list)
61
* that is used for this data column. This property is effective only when
62
* {@link CGridView::enableFiltering} is set true.
63
* If this property is not set, a text field will be generated as the filter input;
64
* If this property is an array, a dropdown list will be generated that uses this property value as
71
* Initializes the column.
73
public function init()
76
if($this->name===null)
77
$this->sortable=false;
78
if($this->name===null && $this->value===null)
79
throw new CException(Yii::t('zii','Either "name" or "value" must be specified for CDataColumn.'));
83
* Renders the filter cell content.
84
* This method will render the {@link filter} as is if it is a string.
85
* If {@link filter} is an array, it is assumed to be a list of options, and a dropdown selector will be rendered.
86
* Otherwise if {@link filter} is not false, a text field is rendered.
89
protected function renderFilterCellContent()
91
if($this->filter!==false && $this->grid->filter!==null && $this->name!==null && strpos($this->name,'.')===false)
93
if(is_array($this->filter))
94
echo CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id'=>false,'prompt'=>''));
95
else if($this->filter===null)
96
echo CHtml::activeTextField($this->grid->filter, $this->name, array('id'=>false));
101
parent::renderFilterCellContent();
105
* Renders the header cell content.
106
* This method will render a link that can trigger the sorting if the column is sortable.
108
protected function renderHeaderCellContent()
110
if($this->grid->enableSorting && $this->sortable && $this->name!==null)
111
echo $this->grid->dataProvider->getSort()->link($this->name,$this->header);
112
else if($this->name!==null && $this->header===null)
114
if($this->grid->dataProvider instanceof CActiveDataProvider)
115
echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));
117
echo CHtml::encode($this->name);
120
parent::renderHeaderCellContent();
124
* Renders the data cell content.
125
* This method evaluates {@link value} or {@link name} and renders the result.
126
* @param integer $row the row number (zero-based)
127
* @param mixed $data the data associated with the row
129
protected function renderDataCellContent($row,$data)
131
if($this->value!==null)
132
$value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
133
else if($this->name!==null)
134
$value=CHtml::value($data,$this->name);
135
echo $value===null ? $this->grid->nullDisplay : $this->grid->getFormatter()->format($value,$this->type);