3 * TBoundColumn class file
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Web.UI.WebControls
13 * TDataGridColumn class file
15 Prado::using('System.Web.UI.WebControls.TDataGridColumn');
20 * TBoundColumn represents a column that is bound to a field in a data source.
21 * The cells in the column will be displayed using the data indexed by
22 * {@link setDataField DataField}. You can customize the display by
23 * setting {@link setDataFormatString DataFormatString}.
25 * If {@link setReadOnly ReadOnly} is false, TBoundColumn will display cells in edit mode
26 * with textboxes. Otherwise, a static text is displayed.
28 * When a datagrid row is in edit mode, the textbox control in the TBoundColumn
29 * can be accessed by one of the following two methods:
31 * $datagridItem->BoundColumnID->TextBox
32 * $datagridItem->BoundColumnID->Controls[0]
34 * The second method is possible because the textbox control created within the
35 * datagrid cell is the first child.
37 * Since v3.1.0, TBoundColumn has introduced two new properties {@link setItemRenderer ItemRenderer}
38 * and {@link setEditItemRenderer EditItemRenderer} which can be used to specify
39 * the layout of the datagrid cells in browsing and editing mode.
40 * A renderer refers to a control class that is to be instantiated as a control.
41 * For more details, see {@link TRepeater} and {@link TDataList}.
43 * @author Qiang Xue <qiang.xue@gmail.com>
44 * @package System.Web.UI.WebControls
47 class TBoundColumn extends TDataGridColumn
50 * @return string the class name for the item cell renderer. Defaults to empty, meaning not set.
53 public function getItemRenderer()
55 return $this->getViewState('ItemRenderer','');
59 * Sets the item cell renderer class.
61 * If not empty, the class will be used to instantiate as a child control in the item cells of the column.
63 * If the class implements {@link IDataRenderer}, the <b>Data</b> property
64 * will be set as the data associated with the datagrid cell during databinding.
65 * The data can be either the whole data row or a field of the row if
66 * {@link getDataField DataField} is not empty. If {@link getDataFormatString DataFormatString}
67 * is not empty, the data will be formatted first before passing to the renderer.
69 * @param string the renderer class name in namespace format.
72 public function setItemRenderer($value)
74 $this->setViewState('ItemRenderer',$value,'');
78 * @return string the class name for the edit item cell renderer. Defaults to empty, meaning not set.
81 public function getEditItemRenderer()
83 return $this->getViewState('EditItemRenderer','');
87 * Sets the edit item cell renderer class.
89 * If not empty, the class will be used to instantiate as a child control in the item cell that is in edit mode.
91 * If the class implements {@link IDataRenderer}, the <b>Data</b> property
92 * will be set as the data associated with the datagrid cell during databinding.
93 * The data can be either the whole data row or a field of the row if
94 * {@link getDataField DataField} is not empty. If {@link getDataFormatString DataFormatString}
95 * is not empty, the data will be formatted first before passing to the renderer.
97 * @param string the renderer class name in namespace format.
100 public function setEditItemRenderer($value)
102 $this->setViewState('EditItemRenderer',$value,'');
106 * @return string the field name from the data source to bind to the column
108 public function getDataField()
110 return $this->getViewState('DataField','');
114 * @param string the field name from the data source to bind to the column
116 public function setDataField($value)
118 $this->setViewState('DataField',$value,'');
122 * @return string the formatting string used to control how the bound data will be displayed.
124 public function getDataFormatString()
126 return $this->getViewState('DataFormatString','');
130 * @param string the formatting string used to control how the bound data will be displayed.
132 public function setDataFormatString($value)
134 $this->setViewState('DataFormatString',$value,'');
138 * @return boolean whether the items in the column can be edited. Defaults to false.
140 public function getReadOnly()
142 return $this->getViewState('ReadOnly',false);
146 * @param boolean whether the items in the column can be edited
148 public function setReadOnly($value)
150 $this->setViewState('ReadOnly',TPropertyValue::ensureBoolean($value),false);
154 * Initializes the specified cell to its initial values.
155 * This method overrides the parent implementation.
156 * It creates a textbox for item in edit mode and the column is not read-only.
157 * Otherwise it displays a static text.
158 * The caption of the button and the static text are retrieved
159 * from the datasource.
160 * @param TTableCell the cell to be initialized.
161 * @param integer the index to the Columns property that the cell resides in.
162 * @param string the type of cell (Header,Footer,Item,AlternatingItem,EditItem,SelectedItem)
164 public function initializeCell($cell,$columnIndex,$itemType)
166 $item=$cell->getParent();
169 case TListItemType::Item:
170 case TListItemType::AlternatingItem:
171 case TListItemType::SelectedItem:
172 if(($classPath=$this->getItemRenderer())!=='')
174 $control=Prado::createComponent($classPath);
175 if($control instanceof IItemDataRenderer)
177 $control->setItemIndex($item->getItemIndex());
178 $control->setItemType($item->getItemType());
180 $cell->getControls()->add($control);
184 $control->attachEventHandler('OnDataBinding',array($this,'dataBindColumn'));
186 case TListItemType::EditItem:
187 if(!$this->getReadOnly())
189 if(($classPath=$this->getEditItemRenderer())!=='')
191 $control=Prado::createComponent($classPath);
192 if($control instanceof IItemDataRenderer)
194 $control->setItemIndex($item->getItemIndex());
195 $control->setItemType($item->getItemType());
197 $cell->getControls()->add($control);
198 $cell->registerObject('EditControl',$control);
202 $control=Prado::createComponent('System.Web.UI.WebControls.TTextBox');
203 $cell->getControls()->add($control);
204 $cell->registerObject('TextBox',$control);
209 if(($classPath=$this->getItemRenderer())!=='')
211 $control=Prado::createComponent($classPath);
212 if($control instanceof IItemDataRenderer)
214 $control->setItemIndex($item->getItemIndex());
215 $control->setItemType($item->getItemType());
217 $cell->getControls()->add($control);
222 $control->attachEventHandler('OnDataBinding',array($this,'dataBindColumn'));
225 parent::initializeCell($cell,$columnIndex,$itemType);
231 * Databinds a cell in the column.
232 * This method is invoked when datagrid performs databinding.
233 * It populates the content of the cell with the relevant data from data source.
235 public function dataBindColumn($sender,$param)
237 $item=$sender->getNamingContainer();
238 $data=$item->getData();
239 $formatString=$this->getDataFormatString();
240 if(($field=$this->getDataField())!=='')
241 $value=$this->formatDataValue($formatString,$this->getDataFieldValue($data,$field));
243 $value=$this->formatDataValue($formatString,$data);
244 $sender->setData($value);