1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TDropDownListColumn class file |
4
|
|
|
* |
5
|
|
|
* @author Qiang Xue <[email protected]> |
6
|
|
|
* @link https://github.com/pradosoft/prado |
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
8
|
|
|
* @package Prado\Web\UI\WebControls |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Prado\Web\UI\WebControls; |
12
|
|
|
|
13
|
|
|
use Prado\TPropertyValue; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* TDropDownListColumn class |
17
|
|
|
* |
18
|
|
|
* TDropDownListColumn represents a column that is bound to a field in a data source. |
19
|
|
|
* The cells in the column will be displayed using the data indexed by |
20
|
|
|
* {@link setDataTextField DataTextField}. You can customize the display by |
21
|
|
|
* setting {@link setDataTextFormatString DataTextFormatString}. |
22
|
|
|
* |
23
|
|
|
* If {@link setReadOnly ReadOnly} is false, TDropDownListColumn will display cells in edit mode |
24
|
|
|
* with dropdown lists. Otherwise, a static text is displayed. |
25
|
|
|
* The currently selected dropndown list item is specified by the data indexed with |
26
|
|
|
* {@link setDataValueField DataValueField}. |
27
|
|
|
* |
28
|
|
|
* There are two approaches to specify the list items available for selection. |
29
|
|
|
* The first approach uses template syntax as follows, |
30
|
|
|
* <code> |
31
|
|
|
* <com:TDropDownListColumn ....> |
32
|
|
|
* <com:TListItem Value="1" Text="first item" /> |
33
|
|
|
* <com:TListItem Value="2" Text="second item" /> |
34
|
|
|
* <com:TListItem Value="3" Text="third item" /> |
35
|
|
|
* </com:TDropDownListColumn> |
36
|
|
|
* </code> |
37
|
|
|
* The second approach specifies a data source to be bound to the dropdown lists |
38
|
|
|
* by setting {@link setListDataSource ListDataSource}. Like generic list controls, |
39
|
|
|
* you may also want to specify which data fields are used for item values and texts |
40
|
|
|
* by setting {@link setListValueField ListValueField} and |
41
|
|
|
* {@link setListTextField ListTextField}, respectively. |
42
|
|
|
* Furthermore, the item texts may be formatted by using {@link setListTextFormatString ListTextFormatString}. |
43
|
|
|
* Note, if you specify {@link setListDataSource ListDataSource}, do it before |
44
|
|
|
* calling the datagrid's dataBind(). |
45
|
|
|
* |
46
|
|
|
* The dropdown list control in the TDropDownListColumn can be accessed by one of |
47
|
|
|
* the following two methods: |
48
|
|
|
* <code> |
49
|
|
|
* $datagridItem->DropDownListColumnID->DropDownList |
50
|
|
|
* $datagridItem->DropDownListColumnID->Controls[0] |
51
|
|
|
* </code> |
52
|
|
|
* The second method is possible because the dropdown list control created within the |
53
|
|
|
* datagrid cell is the first child. |
54
|
|
|
* |
55
|
|
|
* @author Qiang Xue <[email protected]> |
56
|
|
|
* @package Prado\Web\UI\WebControls |
57
|
|
|
* @since 3.0.4 |
58
|
|
|
*/ |
59
|
|
|
class TDropDownListColumn extends TDataGridColumn |
60
|
|
|
{ |
61
|
|
|
private $_stateLoaded = false; |
62
|
|
|
private $_dataBound = false; |
63
|
|
|
private $_listControl; |
64
|
|
|
|
65
|
|
|
public function __construct() |
66
|
|
|
{ |
67
|
|
|
$this->_listControl = new TDropDownList; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Loads items from viewstate. |
72
|
|
|
* This method overrides the parent implementation by loading list items |
73
|
|
|
* @param mixed $state state values |
74
|
|
|
*/ |
75
|
|
|
public function loadState($state) |
76
|
|
|
{ |
77
|
|
|
parent::loadState($state); |
78
|
|
|
$this->_stateLoaded = true; |
79
|
|
|
if (!$this->_dataBound) { |
80
|
|
|
$this->_listControl->getItems()->loadState($this->getViewState('Items', null)); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Saves items into viewstate. |
86
|
|
|
* This method overrides the parent implementation by saving list items |
87
|
|
|
*/ |
88
|
|
|
public function saveState() |
89
|
|
|
{ |
90
|
|
|
$this->setViewState('Items', $this->_listControl->getItems()->saveState(), null); |
91
|
|
|
return parent::saveState(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Adds object parsed from template to the control. |
96
|
|
|
* This method adds only {@link TListItem} objects into the {@link getItems Items} collection. |
97
|
|
|
* All other objects are ignored. |
98
|
|
|
* @param mixed $object object parsed from template |
99
|
|
|
*/ |
100
|
|
|
public function addParsedObject($object) |
101
|
|
|
{ |
102
|
|
|
// Do not add items from template if items are loaded from viewstate |
103
|
|
|
if (!$this->_stateLoaded && ($object instanceof TListItem)) { |
104
|
|
|
$object->setSelected(false); |
105
|
|
|
$index = $this->_listControl->getItems()->add($object); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string the field of the data source that provides the text content of the column. |
111
|
|
|
*/ |
112
|
|
|
public function getDataTextField() |
113
|
|
|
{ |
114
|
|
|
return $this->getViewState('DataTextField', ''); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Sets the field of the data source that provides the text content of the column. |
119
|
|
|
* If this is not set, the data specified via {@link getDataValueField DataValueField} |
120
|
|
|
* will be displayed in the column. |
121
|
|
|
* @param string $value the field of the data source that provides the text content of the column. |
122
|
|
|
*/ |
123
|
|
|
public function setDataTextField($value) |
124
|
|
|
{ |
125
|
|
|
$this->setViewState('DataTextField', $value, ''); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string the formatting string used to control how the bound data will be displayed. |
130
|
|
|
*/ |
131
|
|
|
public function getDataTextFormatString() |
132
|
|
|
{ |
133
|
|
|
return $this->getViewState('DataTextFormatString', ''); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $value the formatting string used to control how the bound data will be displayed. |
138
|
|
|
*/ |
139
|
|
|
public function setDataTextFormatString($value) |
140
|
|
|
{ |
141
|
|
|
$this->setViewState('DataTextFormatString', $value, ''); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string the field of the data source that provides the key selecting an item in dropdown list. |
146
|
|
|
*/ |
147
|
|
|
public function getDataValueField() |
148
|
|
|
{ |
149
|
|
|
return $this->getViewState('DataValueField', ''); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Sets the field of the data source that provides the key selecting an item in dropdown list. |
154
|
|
|
* If this is not present, the data specified via {@link getDataTextField DataTextField} (without |
155
|
|
|
* applying the formatting string) will be used for selection, instead. |
156
|
|
|
* @param string $value the field of the data source that provides the key selecting an item in dropdown list. |
157
|
|
|
*/ |
158
|
|
|
public function setDataValueField($value) |
159
|
|
|
{ |
160
|
|
|
$this->setViewState('DataValueField', $value, ''); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return bool whether the items in the column can be edited. Defaults to false. |
165
|
|
|
*/ |
166
|
|
|
public function getReadOnly() |
167
|
|
|
{ |
168
|
|
|
return $this->getViewState('ReadOnly', false); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param bool $value whether the items in the column can be edited |
173
|
|
|
*/ |
174
|
|
|
public function setReadOnly($value) |
175
|
|
|
{ |
176
|
|
|
$this->setViewState('ReadOnly', TPropertyValue::ensureBoolean($value), false); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return Traversable data source to be bound to the dropdown list boxes. |
|
|
|
|
181
|
|
|
*/ |
182
|
|
|
public function getListDataSource() |
183
|
|
|
{ |
184
|
|
|
return $this->_listControl->getDataSource(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param array|string|Traversable $value data source to be bound to the dropdown list boxes. |
189
|
|
|
*/ |
190
|
|
|
public function setListDataSource($value) |
191
|
|
|
{ |
192
|
|
|
$this->_listControl->setDataSource($value); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string the data field used to populate the values of the dropdown list items. Defaults to empty. |
197
|
|
|
*/ |
198
|
|
|
public function getListValueField() |
199
|
|
|
{ |
200
|
|
|
return $this->getViewState('ListValueField', ''); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $value the data field used to populate the values of the dropdown list items |
205
|
|
|
*/ |
206
|
|
|
public function setListValueField($value) |
207
|
|
|
{ |
208
|
|
|
$this->setViewState('ListValueField', $value, ''); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return string the data field used to populate the texts of the dropdown list items. Defaults to empty. |
213
|
|
|
*/ |
214
|
|
|
public function getListTextField() |
215
|
|
|
{ |
216
|
|
|
return $this->getViewState('ListTextField', ''); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $value the data field used to populate the texts of the dropdown list items |
221
|
|
|
*/ |
222
|
|
|
public function setListTextField($value) |
223
|
|
|
{ |
224
|
|
|
$this->setViewState('ListTextField', $value, ''); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return string the formatting string used to control how the list item texts will be displayed. |
229
|
|
|
*/ |
230
|
|
|
public function getListTextFormatString() |
231
|
|
|
{ |
232
|
|
|
return $this->getViewState('ListTextFormatString', ''); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param string $value the formatting string used to control how the list item texts will be displayed. |
237
|
|
|
*/ |
238
|
|
|
public function setListTextFormatString($value) |
239
|
|
|
{ |
240
|
|
|
$this->setViewState('ListTextFormatString', $value, ''); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Initializes the specified cell to its initial values. |
245
|
|
|
* This method overrides the parent implementation. |
246
|
|
|
* It creates a textbox for item in edit mode and the column is not read-only. |
247
|
|
|
* Otherwise it displays a static text. |
248
|
|
|
* The caption of the button and the static text are retrieved |
249
|
|
|
* from the datasource. |
250
|
|
|
* @param TTableCell $cell the cell to be initialized. |
251
|
|
|
* @param int $columnIndex the index to the Columns property that the cell resides in. |
252
|
|
|
* @param string $itemType the type of cell (Header,Footer,Item,AlternatingItem,EditItem,SelectedItem) |
253
|
|
|
*/ |
254
|
|
|
public function initializeCell($cell, $columnIndex, $itemType) |
255
|
|
|
{ |
256
|
|
|
if (!$this->_dataBound && $this->_listControl->getDataSource() !== null) { |
257
|
|
|
$this->_listControl->setDataTextField($this->getListTextField()); |
258
|
|
|
$this->_listControl->setDataValueField($this->getListValueField()); |
259
|
|
|
$this->_listControl->setDataTextFormatString($this->getListTextFormatString()); |
260
|
|
|
$this->_listControl->dataBind(); |
261
|
|
|
$this->_dataBound = true; |
262
|
|
|
} |
263
|
|
|
switch ($itemType) { |
264
|
|
|
case TListItemType::EditItem: |
265
|
|
|
if (!$this->getReadOnly()) { |
266
|
|
|
$listControl = clone $this->_listControl; |
267
|
|
|
$cell->getControls()->add($listControl); |
268
|
|
|
$cell->registerObject('DropDownList', $listControl); |
269
|
|
|
$control = $listControl; |
270
|
|
|
} else { |
271
|
|
|
$control = $cell; |
272
|
|
|
} |
273
|
|
|
$control->attachEventHandler('OnDataBinding', [$this, 'dataBindColumn']); |
274
|
|
|
break; |
275
|
|
|
case TListItemType::Item: |
276
|
|
|
case TListItemType::AlternatingItem: |
277
|
|
|
case TListItemType::SelectedItem: |
278
|
|
|
if ($this->getDataTextField() !== '' || $this->getDataValueField() !== '') { |
279
|
|
|
$cell->attachEventHandler('OnDataBinding', [$this, 'dataBindColumn']); |
280
|
|
|
} |
281
|
|
|
break; |
282
|
|
|
default: |
283
|
|
|
parent::initializeCell($cell, $columnIndex, $itemType); |
284
|
|
|
break; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Databinds a cell in the column. |
290
|
|
|
* This method is invoked when datagrid performs databinding. |
291
|
|
|
* It populates the content of the cell with the relevant data from data source. |
292
|
|
|
* @param mixed $sender |
293
|
|
|
* @param mixed $param |
294
|
|
|
*/ |
295
|
|
|
public function dataBindColumn($sender, $param) |
|
|
|
|
296
|
|
|
{ |
297
|
|
|
$item = $sender->getNamingContainer(); |
298
|
|
|
$data = $item->getData(); |
299
|
|
|
if (($valueField = $this->getDataValueField()) !== '') { |
300
|
|
|
$value = $this->getDataFieldValue($data, $valueField); |
301
|
|
|
} else { |
302
|
|
|
$value = ''; |
303
|
|
|
} |
304
|
|
|
if (($textField = $this->getDataTextField()) !== '') { |
305
|
|
|
$text = $this->getDataFieldValue($data, $textField); |
306
|
|
|
if ($valueField === '') { |
307
|
|
|
$value = $text; |
308
|
|
|
} |
309
|
|
|
$formatString = $this->getDataTextFormatString(); |
310
|
|
|
$text = $this->formatDataValue($formatString, $text); |
311
|
|
|
} else { |
312
|
|
|
$text = $value; |
313
|
|
|
} |
314
|
|
|
if ($sender instanceof TTableCell) { |
315
|
|
|
$sender->setText($text); |
316
|
|
|
} elseif ($sender instanceof TDropDownList) { |
317
|
|
|
$sender->setSelectedValue($value); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|