3 * TCheckBoxColumn 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');
17 * TCheckBox class file
19 Prado::using('System.Web.UI.WebControls.TCheckBox');
22 * TCheckBoxColumn class
24 * TCheckBoxColumn represents a checkbox column that is bound to a field in a data source.
25 * The checked state of the checkboxes are determiend by the bound data at
26 * {@link setDataField DataField}. If {@link setReadOnly ReadOnly} is false,
27 * TCheckBoxColumn will display an enabled checkbox provided the cells are
28 * in edit mode. Otherwise, the checkboxes will be disabled to prevent from editting.
30 * The checkbox control in the TCheckBoxColumn can be accessed by one of
31 * the following two methods:
33 * $datagridItem->CheckBoxColumnID->CheckBox
34 * $datagridItem->CheckBoxColumnID->Controls[0]
36 * The second method is possible because the checkbox control created within the
37 * datagrid cell is the first child.
39 * @author Qiang Xue <qiang.xue@gmail.com>
40 * @package System.Web.UI.WebControls
43 class TCheckBoxColumn extends TDataGridColumn
46 * @return string the field name from the data source to bind to the column
48 public function getDataField()
50 return $this->getViewState('DataField','');
54 * @param string the field name from the data source to bind to the column
56 public function setDataField($value)
58 $this->setViewState('DataField',$value,'');
62 * @return boolean whether the items in the column can be edited. Defaults to false.
64 public function getReadOnly()
66 return $this->getViewState('ReadOnly',false);
70 * @param boolean whether the items in the column can be edited
72 public function setReadOnly($value)
74 $this->setViewState('ReadOnly',TPropertyValue::ensureBoolean($value),false);
78 * Initializes the specified cell to its initial values.
79 * This method overrides the parent implementation.
80 * It creates a checkbox inside the cell.
81 * If the column is read-only or if the item is not in edit mode,
82 * the checkbox will be set disabled.
83 * @param TTableCell the cell to be initialized.
84 * @param integer the index to the Columns property that the cell resides in.
85 * @param string the type of cell (Header,Footer,Item,AlternatingItem,EditItem,SelectedItem)
87 public function initializeCell($cell,$columnIndex,$itemType)
89 if($itemType===TListItemType::Item || $itemType===TListItemType::AlternatingItem || $itemType===TListItemType::SelectedItem || $itemType===TListItemType::EditItem)
91 $checkBox=new TCheckBox;
92 if($this->getReadOnly() || $itemType!==TListItemType::EditItem)
93 $checkBox->setEnabled(false);
94 $cell->setHorizontalAlign('Center');
95 $cell->getControls()->add($checkBox);
96 $cell->registerObject('CheckBox',$checkBox);
97 if($this->getDataField()!=='')
98 $checkBox->attachEventHandler('OnDataBinding',array($this,'dataBindColumn'));
101 parent::initializeCell($cell,$columnIndex,$itemType);
105 * Databinds a cell in the column.
106 * This method is invoked when datagrid performs databinding.
107 * It populates the content of the cell with the relevant data from data source.
109 public function dataBindColumn($sender,$param)
111 $item=$sender->getNamingContainer();
112 $data=$item->getData();
113 if(($field=$this->getDataField())!=='')
114 $value=TPropertyValue::ensureBoolean($this->getDataFieldValue($data,$field));
116 $value=TPropertyValue::ensureBoolean($data);
117 if($sender instanceof TCheckBox)
118 $sender->setChecked($value);