summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TDropDownListColumn.php
blob: 17686c573e316266f0a72a58aef278b15b3f2a89 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
 * TDropDownListColumn class file
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2006 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Revision: $  $Date: $
 * @package System.Web.UI.WebControls
 */

Prado::using('System.Web.UI.WebControls.TDataGridColumn');
Prado::using('System.Web.UI.WebControls.TDropDownList');

/**
 * TDropDownListColumn class
 *
 * TDropDownListColumn represents a column that is bound to a field in a data source.
 * The cells in the column will be displayed using the data indexed by
 * {@link setDataField DataField}. You can customize the display by
 * setting {@link setDataFormatString DataFormatString}.
 *
 * If {@link setReadOnly ReadOnly} is false, TDropDownListColumn will display cells in edit mode
 * with dropdown lists. Otherwise, a static text is displayed.
 *
 * There are two approaches to specify the list items available for selection.
 * The first approach uses template syntax as follows,
 * <code>
 *   <com:TDropDownListColumn ....>
 *     <com:TListItem Value="1" Text="first item" />
 *     <com:TListItem Value="2" Text="second item" />
 *     <com:TListItem Value="3" Text="third item" />
 *   </com:TDropDownListColumn>
 * </code>
 * The second approach specifies a data source to be bound to the dropdown lists
 * by setting {@link setListDataSource ListDataSource}. Like generic list controls,
 * you may also want to specify which data fields are used for item values and texts
 * by setting {@link setListValueField ListValueField} and
 * {@link setListTextField ListTextField}, respectively.
 * Furthermore, the item texts may be formatted by using {@link setListTextFormatString ListTextFormatString}.
 * Note, if you specify {@link setListDataSource ListDataSource}, do it before
 * calling the datagrid's dataBind().
 *
 * The dropdown list control in the TDropDownListColumn can be accessed by one of
 * the following two methods:
 * <code>
 * $datagridItem->DropDownListColumnID->DropDownList
 * $datagridItem->DropDownListColumnID->Controls[0]
 * </code>
 * The second method is possible because the dropdown list control created within the
 * datagrid cell is the first child.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @version $Revision: $  $Date: $
 * @package System.Web.UI.WebControls
 * @since 3.0.4
 */
class TDropDownListColumn extends TDataGridColumn
{
	private $_stateLoaded=false;
	private $_dataBound=false;
	private $_listControl=null;

	public function __construct()
	{
		$this->_listControl=new TDropDownList;
	}

	/**
	 * Loads items from viewstate.
	 * This method overrides the parent implementation by loading list items
	 * @param mixed state values
	 */
	public function loadState($state)
	{
		parent::loadState($state);
		$this->_stateLoaded=true;
		if(!$this->_dataBound)
			$this->_listControl->getItems()->loadState($this->getViewState('Items',null));
	}

	/**
	 * Saves items into viewstate.
	 * This method overrides the parent implementation by saving list items
	 */
	public function saveState()
	{
		$this->setViewState('Items',$this->_listControl->getItems()->saveState(),null);
		return parent::saveState();
	}

	/**
	 * Adds object parsed from template to the control.
	 * This method adds only {@link TListItem} objects into the {@link getItems Items} collection.
	 * All other objects are ignored.
	 * @param mixed object parsed from template
	 */
	public function addParsedObject($object)
	{
		// Do not add items from template if items are loaded from viewstate
		if(!$this->_stateLoaded && ($object instanceof TListItem))
		{
			$object->setSelected(false);
			$index=$this->_listControl->getItems()->add($object);
		}
	}

	/**
	 * @return string the field name from the data source to bind to the column
	 */
	public function getDataField()
	{
		return $this->getViewState('DataField','');
	}

	/**
	 * @param string the field name from the data source to bind to the column
	 */
	public function setDataField($value)
	{
		$this->setViewState('DataField',$value,'');
	}

	/**
	 * @return string the formatting string used to control how the bound data will be displayed.
	 */
	public function getDataFormatString()
	{
		return $this->getViewState('DataFormatString','');
	}

	/**
	 * @param string the formatting string used to control how the bound data will be displayed.
	 */
	public function setDataFormatString($value)
	{
		$this->setViewState('DataFormatString',$value,'');
	}

	/**
	 * @return boolean whether the items in the column can be edited. Defaults to false.
	 */
	public function getReadOnly()
	{
		return $this->getViewState('ReadOnly',false);
	}

	/**
	 * @param boolean whether the items in the column can be edited
	 */
	public function setReadOnly($value)
	{
		$this->setViewState('ReadOnly',TPropertyValue::ensureBoolean($value),false);
	}

	/**
	 * @return Traversable data source to be bound to the dropdown list boxes.
	 */
	public function getListDataSource()
	{
		return $this->_listControl->getDataSource();
	}

	/**
	 * @param Traversable|array|string data source to be bound to the dropdown list boxes.
	 */
	public function setListDataSource($value)
	{
		$this->_listControl->setDataSource($value);
	}

	/**
	 * @return string the data field used to populate the values of the dropdown list items. Defaults to empty.
	 */
	public function getListValueField()
	{
		return $this->getViewState('ListValueField','');
	}

	/**
	 * @param string the data field used to populate the values of the dropdown list items
	 */
	public function setListValueField($value)
	{
		$this->setViewState('ListValueField',$value,'');
	}

	/**
	 * @return string the data field used to populate the texts of the dropdown list items. Defaults to empty.
	 */
	public function getListTextField()
	{
		return $this->getViewState('ListTextField','');
	}

	/**
	 * @param string the data field used to populate the texts of the dropdown list items
	 */
	public function setListTextField($value)
	{
		$this->setViewState('ListTextField',$value,'');
	}

	/**
	 * @return string the formatting string used to control how the list item texts will be displayed.
	 */
	public function getListTextFormatString()
	{
		return $this->getViewState('ListTextFormatString','');
	}

	/**
	 * @param string the formatting string used to control how the list item texts will be displayed.
	 */
	public function setListTextFormatString($value)
	{
		$this->setViewState('ListTextFormatString',$value,'');
	}

	/**
	 * Initializes the specified cell to its initial values.
	 * This method overrides the parent implementation.
	 * It creates a textbox for item in edit mode and the column is not read-only.
	 * Otherwise it displays a static text.
	 * The caption of the button and the static text are retrieved
	 * from the datasource.
	 * @param TTableCell the cell to be initialized.
	 * @param integer the index to the Columns property that the cell resides in.
	 * @param string the type of cell (Header,Footer,Item,AlternatingItem,EditItem,SelectedItem)
	 */
	public function initializeCell($cell,$columnIndex,$itemType)
	{
		parent::initializeCell($cell,$columnIndex,$itemType);
		if(!$this->_dataBound && $this->_listControl->getDataSource()!==null)
		{
			$this->_listControl->setDataTextField($this->getListTextField());
			$this->_listControl->setDataValueField($this->getListValueField());
			$this->_listControl->setDataTextFormatString($this->getListTextFormatString());
			$this->_listControl->dataBind();
			$this->_dataBound=true;
		}
		switch($itemType)
		{
			case TListItemType::EditItem:
				if(!$this->getReadOnly())
				{
					$listControl=clone $this->_listControl;
					$cell->getControls()->add($listControl);
					$cell->registerObject('DropDownList',$listControl);
					$control=$listControl;
				}
				else
					$control=$cell;
				$control->attachEventHandler('OnDataBinding',array($this,'dataBindColumn'));
				break;
			case TListItemType::Item:
			case TListItemType::AlternatingItem:
			case TListItemType::SelectedItem:
				if($this->getDataField()!=='')
					$cell->attachEventHandler('OnDataBinding',array($this,'dataBindColumn'));
				break;
		}
	}

	/**
	 * Databinds a cell in the column.
	 * This method is invoked when datagrid performs databinding.
	 * It populates the content of the cell with the relevant data from data source.
	 */
	public function dataBindColumn($sender,$param)
	{
		$item=$sender->getNamingContainer();
		$data=$item->getDataItem();
		if(($field=$this->getDataField())!=='')
			$data=$this->getDataFieldValue($data,$field);
		$formatString=$this->getDataFormatString();
		$value=$this->formatDataValue($formatString,$data);
		if($sender instanceof TTableCell)
			$sender->setText($value);
		else if($sender instanceof TDropDownList)
			$sender->setSelectedValue($data);
	}
}

?>