CDataColumn.php
5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* CDataColumn class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
Yii::import('zii.widgets.grid.CGridColumn');
/**
* CDataColumn represents a grid view column that is associated with a data attribute or expression.
*
* Either {@link name} or {@link value} should be specified. The former specifies
* a data attribute name, while the latter a PHP expression whose value should be rendered instead.
*
* The property {@link sortable} determines whether the grid view can be sorted according to this column.
* Note that the {@link name} should always be set if the column needs to be sortable. The {@link name}
* value will be used by {@link CSort} to render a clickable link in the header cell to trigger the sorting.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package zii.widgets.grid
* @since 1.1
*/
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';
/**
* @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;
/**
* @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
* {@link CGridView::filter} is set.
* If this property is not set, a text field will be generated as the filter input;
* If this property is an array, a dropdown list will be generated that uses this property value as
* the list options.
* If you don't want a filter for this data column, set this value to false.
* @since 1.1.1
*/
public $filter;
/**
* Initializes the column.
*/
public function init()
{
parent::init();
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.'));
}
/**
* Renders the filter cell content.
* This method will render the {@link filter} as is if it is a string.
* If {@link filter} is an array, it is assumed to be a list of options, and a dropdown selector will be rendered.
* Otherwise if {@link filter} is not false, a text field is rendered.
* @since 1.1.1
*/
protected function renderFilterCellContent()
{
if(is_string($this->filter))
echo $this->filter;
else if($this->filter!==false && $this->grid->filter!==null && $this->name!==null && strpos($this->name,'.')===false)
{
if(is_array($this->filter))
echo CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id'=>false,'prompt'=>''));
else if($this->filter===null)
echo CHtml::activeTextField($this->grid->filter, $this->name, array('id'=>false));
}
else
parent::renderFilterCellContent();
}
/**
* Renders the header cell content.
* This method will render a link that can trigger the sorting if the column is sortable.
*/
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'));
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();
}
/**
* Renders the data cell content.
* This method evaluates {@link value} or {@link name} and renders the result.
* @param integer $row the row number (zero-based)
* @param mixed $data the data associated with the row
*/
protected function renderDataCellContent($row,$data)
{
if($this->value!==null)
$value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
else if($this->name!==null)
$value=CHtml::value($data,$this->name);
echo $value===null ? $this->grid->nullDisplay : $this->grid->getFormatter()->format($value,$this->type);
}
}