Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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