3 * TEditCommandColumn 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');
18 * TEditCommandColumn class
20 * TEditCommandColumn contains the Edit command buttons for editing data items in each row.
22 * TEditCommandColumn will create an edit button if a cell is not in edit mode.
23 * Otherwise an update button and a cancel button will be created within the cell.
24 * The button captions are specified using {@link setEditText EditText},
25 * {@link setUpdateText UpdateText}, and {@link setCancelText CancelText}.
27 * The buttons in the column can be set to display as hyperlinks, push or image buttons
28 * by setting the {@link setButtonType ButtonType} property.
30 * When an edit button is clicked, the datagrid will generate an
31 * {@link onEditCommand OnEditCommand} event. When an update/cancel button
32 * is clicked, the datagrid will generate an
33 * {@link onUpdateCommand OnUpdateCommand} or an {@link onCancelCommand OnCancelCommand}
34 * You can write these event handlers to change the state of specific datagrid item.
36 * The {@link setCausesValidation CausesValidation} and {@link setValidationGroup ValidationGroup}
37 * properties affect the corresponding properties of the edit and update buttons.
38 * The cancel button does not cause validation by default.
40 * The command buttons in the column can be accessed by one of the following methods:
42 * $datagridItem->ButtonColumnID->EditButton (or UpdateButton, CancelButton)
43 * $datagridItem->ButtonColumnID->Controls[0]
45 * The second method is possible because the button control created within the
46 * datagrid cell is the first child.
48 * @author Qiang Xue <qiang.xue@gmail.com>
49 * @package System.Web.UI.WebControls
52 class TEditCommandColumn extends TDataGridColumn
55 * @return TButtonColumnType the type of command button. Defaults to TButtonColumnType::LinkButton.
57 public function getButtonType()
59 return $this->getViewState('ButtonType',TButtonColumnType::LinkButton);
63 * @param TButtonColumnType the type of command button.
65 public function setButtonType($value)
67 $this->setViewState('ButtonType',TPropertyValue::ensureEnum($value,'TButtonColumnType'),TButtonColumnType::LinkButton);
71 * @return string the caption of the edit button. Defaults to 'Edit'.
73 public function getEditText()
75 return $this->getViewState('EditText','Edit');
79 * @param string the caption of the edit button
81 public function setEditText($value)
83 $this->setViewState('EditText',$value,'Edit');
87 * @return string the URL of the image file for edit image buttons
89 public function getEditImageUrl()
91 return $this->getViewState('EditImageUrl','');
95 * @param string the URL of the image file for edit image buttons
97 public function setEditImageUrl($value)
99 $this->setViewState('EditImageUrl',$value,'');
103 * @return string the caption of the update button. Defaults to 'Update'.
105 public function getUpdateText()
107 return $this->getViewState('UpdateText','Update');
111 * @param string the caption of the update button
113 public function setUpdateText($value)
115 $this->setViewState('UpdateText',$value,'Update');
119 * @return string the URL of the image file for update image buttons
121 public function getUpdateImageUrl()
123 return $this->getViewState('UpdateImageUrl','');
127 * @param string the URL of the image file for update image buttons
129 public function setUpdateImageUrl($value)
131 $this->setViewState('UpdateImageUrl',$value,'');
135 * @return string the caption of the cancel button. Defaults to 'Cancel'.
137 public function getCancelText()
139 return $this->getViewState('CancelText','Cancel');
143 * @param string the caption of the cancel button
145 public function setCancelText($value)
147 $this->setViewState('CancelText',$value,'Cancel');
151 * @return string the URL of the image file for cancel image buttons
153 public function getCancelImageUrl()
155 return $this->getViewState('CancelImageUrl','');
159 * @param string the URL of the image file for cancel image buttons
161 public function setCancelImageUrl($value)
163 $this->setViewState('CancelImageUrl',$value,'');
167 * @return boolean whether postback event trigger by edit or update button will cause input validation, default is true
169 public function getCausesValidation()
171 return $this->getViewState('CausesValidation',true);
175 * @param boolean whether postback event trigger by edit or update button will cause input validation
177 public function setCausesValidation($value)
179 $this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true);
183 * @return string the group of validators which the edit or update button causes validation upon postback
185 public function getValidationGroup()
187 return $this->getViewState('ValidationGroup','');
191 * @param string the group of validators which the edit or update button causes validation upon postback
193 public function setValidationGroup($value)
195 $this->setViewState('ValidationGroup',$value,'');
199 * Initializes the specified cell to its initial values.
200 * This method overrides the parent implementation.
201 * It creates an update and a cancel button for cell in edit mode.
202 * Otherwise it creates an edit button.
203 * @param TTableCell the cell to be initialized.
204 * @param integer the index to the Columns property that the cell resides in.
205 * @param string the type of cell (Header,Footer,Item,AlternatingItem,EditItem,SelectedItem)
207 public function initializeCell($cell,$columnIndex,$itemType)
209 if($itemType===TListItemType::Item || $itemType===TListItemType::AlternatingItem || $itemType===TListItemType::SelectedItem)
211 $button=$this->createButton('Edit',$this->getEditText(),false,'');
212 $cell->getControls()->add($button);
213 $cell->registerObject('EditButton',$button);
215 else if($itemType===TListItemType::EditItem)
217 $controls=$cell->getControls();
218 $button=$this->createButton('Update',$this->getUpdateText(),$this->getCausesValidation(),$this->getValidationGroup());
219 $controls->add($button);
220 $cell->registerObject('UpdateButton',$button);
221 $controls->add(' ');
222 $button=$this->createButton('Cancel',$this->getCancelText(),false,'');
223 $controls->add($button);
224 $cell->registerObject('CancelButton',$button);
227 parent::initializeCell($cell,$columnIndex,$itemType);
231 * Creates a button and initializes its properties.
232 * The button type is determined by {@link getButtonType ButtonType}.
233 * @param string command name associated with the button
234 * @param string button caption
235 * @param boolean whether the button should cause validation
236 * @param string the validation group that the button belongs to
237 * @return mixed the newly created button.
239 protected function createButton($commandName,$text,$causesValidation,$validationGroup)
241 if($this->getButtonType()===TButtonColumnType::LinkButton)
242 $button=Prado::createComponent('System.Web.UI.WebControls.TLinkButton');
243 else if($this->getButtonType()===TButtonColumnType::PushButton)
244 $button=Prado::createComponent('System.Web.UI.WebControls.TButton');
245 else // image buttons
247 $button=Prado::createComponent('System.Web.UI.WebControls.TImageButton');
248 if(strcasecmp($commandName,'Update')===0)
249 $url=$this->getUpdateImageUrl();
250 else if(strcasecmp($commandName,'Cancel')===0)
251 $url=$this->getCancelImageUrl();
253 $url=$this->getEditImageUrl();
254 $button->setImageUrl($url);
256 $button->setText($text);
257 $button->setCommandName($commandName);
258 $button->setCausesValidation($causesValidation);
259 $button->setValidationGroup($validationGroup);