1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\grid; |
9
|
|
|
|
10
|
|
|
use Closure; |
11
|
|
|
use yii\base\Object; |
12
|
|
|
use yii\helpers\Html; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Column is the base class of all [[GridView]] column classes. |
16
|
|
|
* |
17
|
|
|
* For more details and usage information on Column, see the [guide article on data widgets](guide:output-data-widgets). |
18
|
|
|
* |
19
|
|
|
* @author Qiang Xue <[email protected]> |
20
|
|
|
* @since 2.0 |
21
|
|
|
*/ |
22
|
|
|
class Column extends Object |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var GridView the grid view object that owns this column. |
26
|
|
|
*/ |
27
|
|
|
public $grid; |
28
|
|
|
/** |
29
|
|
|
* @var string the header cell content. Note that it will not be HTML-encoded. |
30
|
|
|
*/ |
31
|
|
|
public $header; |
32
|
|
|
/** |
33
|
|
|
* @var string the footer cell content. Note that it will not be HTML-encoded. |
34
|
|
|
*/ |
35
|
|
|
public $footer; |
36
|
|
|
/** |
37
|
|
|
* @var callable This is a callable that will be used to generate the content of each cell. |
38
|
|
|
* The signature of the function should be the following: `function ($model, $key, $index, $column)`. |
39
|
|
|
* Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered |
40
|
|
|
* and `$column` is a reference to the [[Column]] object. |
41
|
|
|
*/ |
42
|
|
|
public $content; |
43
|
|
|
/** |
44
|
|
|
* @var bool whether this column is visible. Defaults to true. |
45
|
|
|
*/ |
46
|
|
|
public $visible = true; |
47
|
|
|
/** |
48
|
|
|
* @var array the HTML attributes for the column group tag. |
49
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
50
|
|
|
*/ |
51
|
|
|
public $options = []; |
52
|
|
|
/** |
53
|
|
|
* @var array the HTML attributes for the header cell tag. |
54
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
55
|
|
|
*/ |
56
|
|
|
public $headerOptions = []; |
57
|
|
|
/** |
58
|
|
|
* @var array|\Closure the HTML attributes for the data cell tag. This can either be an array of |
59
|
|
|
* attributes or an anonymous function ([[Closure]]) that returns such an array. |
60
|
|
|
* The signature of the function should be the following: `function ($model, $key, $index, $column)`. |
61
|
|
|
* Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered |
62
|
|
|
* and `$column` is a reference to the [[Column]] object. |
63
|
|
|
* A function may be used to assign different attributes to different rows based on the data in that row. |
64
|
|
|
* |
65
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
66
|
|
|
*/ |
67
|
|
|
public $contentOptions = []; |
68
|
|
|
/** |
69
|
|
|
* @var array the HTML attributes for the footer cell tag. |
70
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
71
|
|
|
*/ |
72
|
|
|
public $footerOptions = []; |
73
|
|
|
/** |
74
|
|
|
* @var array the HTML attributes for the filter cell tag. |
75
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
76
|
|
|
*/ |
77
|
|
|
public $filterOptions = []; |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Renders the header cell. |
82
|
|
|
*/ |
83
|
2 |
|
public function renderHeaderCell() |
84
|
|
|
{ |
85
|
2 |
|
return Html::tag('th', $this->renderHeaderCellContent(), $this->headerOptions); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Renders the footer cell. |
90
|
|
|
*/ |
91
|
|
|
public function renderFooterCell() |
92
|
|
|
{ |
93
|
|
|
return Html::tag('td', $this->renderFooterCellContent(), $this->footerOptions); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Renders a data cell. |
98
|
|
|
* @param mixed $model the data model being rendered |
99
|
|
|
* @param mixed $key the key associated with the data model |
100
|
|
|
* @param int $index the zero-based index of the data item among the item array returned by [[GridView::dataProvider]]. |
101
|
|
|
* @return string the rendering result |
102
|
|
|
*/ |
103
|
5 |
|
public function renderDataCell($model, $key, $index) |
104
|
|
|
{ |
105
|
5 |
|
if ($this->contentOptions instanceof Closure) { |
106
|
|
|
$options = call_user_func($this->contentOptions, $model, $key, $index, $this); |
107
|
|
|
} else { |
108
|
5 |
|
$options = $this->contentOptions; |
109
|
|
|
} |
110
|
5 |
|
return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Renders the filter cell. |
115
|
|
|
*/ |
116
|
|
|
public function renderFilterCell() |
117
|
|
|
{ |
118
|
|
|
return Html::tag('td', $this->renderFilterCellContent(), $this->filterOptions); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Renders the header cell content. |
123
|
|
|
* The default implementation simply renders [[header]]. |
124
|
|
|
* This method may be overridden to customize the rendering of the header cell. |
125
|
|
|
* @return string the rendering result |
126
|
|
|
*/ |
127
|
1 |
|
protected function renderHeaderCellContent() |
128
|
|
|
{ |
129
|
1 |
|
return trim($this->header) !== '' ? $this->header : $this->getHeaderCellLabel(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns header cell label. |
134
|
|
|
* This method may be overridden to customize the label of the header cell. |
135
|
|
|
* @return string label |
136
|
|
|
* @since 2.0.8 |
137
|
|
|
*/ |
138
|
1 |
|
protected function getHeaderCellLabel() |
139
|
|
|
{ |
140
|
1 |
|
return $this->grid->emptyCell; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Renders the footer cell content. |
145
|
|
|
* The default implementation simply renders [[footer]]. |
146
|
|
|
* This method may be overridden to customize the rendering of the footer cell. |
147
|
|
|
* @return string the rendering result |
148
|
|
|
*/ |
149
|
|
|
protected function renderFooterCellContent() |
150
|
|
|
{ |
151
|
|
|
return trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Renders the data cell content. |
156
|
|
|
* @param mixed $model the data model |
157
|
|
|
* @param mixed $key the key associated with the data model |
158
|
|
|
* @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]]. |
159
|
|
|
* @return string the rendering result |
160
|
|
|
*/ |
161
|
|
|
protected function renderDataCellContent($model, $key, $index) |
162
|
|
|
{ |
163
|
|
|
if ($this->content !== null) { |
164
|
|
|
return call_user_func($this->content, $model, $key, $index, $this); |
165
|
|
|
} else { |
166
|
|
|
return $this->grid->emptyCell; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Renders the filter cell content. |
172
|
|
|
* The default implementation simply renders a space. |
173
|
|
|
* This method may be overridden to customize the rendering of the filter cell (if any). |
174
|
|
|
* @return string the rendering result |
175
|
|
|
*/ |
176
|
|
|
protected function renderFilterCellContent() |
177
|
|
|
{ |
178
|
|
|
return $this->grid->emptyCell; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|