3 * TTable and TTableRowCollection class file
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2014 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @package System.Web.UI.WebControls
13 * Includes TTableRow class
15 Prado::using('System.Web.UI.WebControls.TTableRow');
20 * TTable displays an HTML table on a Web page.
22 * A table may have {@link setCaption Caption}, whose alignment is specified
23 * via {@link setCaptionAlign CaptionAlign}. The table cellpadding and cellspacing
24 * are specified via {@link setCellPadding CellPadding} and {@link setCellSpacing CellSpacing}
25 * properties, respectively. The {@link setGridLines GridLines} specifies how
26 * the table should display its borders. The horizontal alignment of the table
27 * content can be specified via {@link setHorizontalAlign HorizontalAlign},
28 * and {@link setBackImageUrl BackImageUrl} can assign a background image to the table.
30 * A TTable maintains a list of {@link TTableRow} controls in its
31 * {@link getRows Rows} property. Each {@link TTableRow} represents
34 * To populate the table {@link getRows Rows}, you may either use control template
35 * or dynamically create {@link TTableRow} in code.
36 * In template, do as follows to create the table rows and cells,
40 * <com:TTableCell Text="content" />
41 * <com:TTableCell Text="content" />
44 * <com:TTableCell Text="content" />
45 * <com:TTableCell Text="content" />
49 * The above can also be accomplished in code as follows,
53 * $cell=new TTableCell; $cell->Text="content"; $row->Cells->add($cell);
54 * $cell=new TTableCell; $cell->Text="content"; $row->Cells->add($cell);
55 * $table->Rows->add($row);
57 * $cell=new TTableCell; $cell->Text="content"; $row->Cells->add($cell);
58 * $cell=new TTableCell; $cell->Text="content"; $row->Cells->add($cell);
59 * $table->Rows->add($row);
62 * @author Qiang Xue <qiang.xue@gmail.com>
63 * @package System.Web.UI.WebControls
66 class TTable extends TWebControl
69 * @return string tag name for the table
71 protected function getTagName()
77 * Adds object parsed from template to the control.
78 * This method adds only {@link TTableRow} objects into the {@link getRows Rows} collection.
79 * All other objects are ignored.
80 * @param mixed object parsed from template
82 public function addParsedObject($object)
84 if($object instanceof TTableRow)
85 $this->getRows()->add($object);
89 * Creates a style object for the control.
90 * This method creates a {@link TTableStyle} to be used by the table.
91 * @return TTableStyle control style to be used
93 protected function createStyle()
95 return new TTableStyle;
99 * Adds attributes to renderer.
100 * @param THtmlWriter the renderer
102 protected function addAttributesToRender($writer)
104 parent::addAttributesToRender($writer);
106 if($this->getHasStyle())
108 if($this->getGridLines()!==TTableGridLines::None)
110 if(($border=$this->getBorderWidth())==='')
113 $border=(int)$border;
116 $writer->addAttribute('border',"$border");
120 * Creates a control collection object that is to be used to hold child controls
121 * @return TTableRowCollection control collection
124 protected function createControlCollection()
126 return new TTableRowCollection($this);
130 * @return TTableRowCollection list of {@link TTableRow} controls
132 public function getRows()
134 return $this->getControls();
138 * @return string table caption
140 public function getCaption()
142 return $this->getViewState('Caption','');
146 * @param string table caption
148 public function setCaption($value)
150 $this->setViewState('Caption',$value,'');
154 * @return TTableCaptionAlign table caption alignment. Defaults to TTableCaptionAlign::NotSet.
156 public function getCaptionAlign()
158 return $this->getViewState('CaptionAlign',TTableCaptionAlign::NotSet);
162 * @param TTableCaptionAlign table caption alignment.
164 public function setCaptionAlign($value)
166 $this->setViewState('CaptionAlign',TPropertyValue::ensureEnum($value,'TTableCaptionAlign'),TTableCaptionAlign::NotSet);
170 * @return integer the cellspacing for the table. Defaults to -1, meaning not set.
172 public function getCellSpacing()
174 if($this->getHasStyle())
175 return $this->getStyle()->getCellSpacing();
181 * @param integer the cellspacing for the table. Defaults to -1, meaning not set.
183 public function setCellSpacing($value)
185 $this->getStyle()->setCellSpacing($value);
189 * @return integer the cellpadding for the table. Defaults to -1, meaning not set.
191 public function getCellPadding()
193 if($this->getHasStyle())
194 return $this->getStyle()->getCellPadding();
200 * @param integer the cellpadding for the table. Defaults to -1, meaning not set.
202 public function setCellPadding($value)
204 $this->getStyle()->setCellPadding($value);
208 * @return THorizontalAlign the horizontal alignment of the table content. Defaults to THorizontalAlign::NotSet.
210 public function getHorizontalAlign()
212 if($this->getHasStyle())
213 return $this->getStyle()->getHorizontalAlign();
215 return THorizontalAlign::NotSet;
219 * @param THorizontalAlign the horizontal alignment of the table content.
221 public function setHorizontalAlign($value)
223 $this->getStyle()->setHorizontalAlign($value);
227 * @return TTableGridLines the grid line setting of the table. Defaults to TTableGridLines::None.
229 public function getGridLines()
231 if($this->getHasStyle())
232 return $this->getStyle()->getGridLines();
234 return TTableGridLines::None;
238 * @param TTableGridLines the grid line setting of the table
240 public function setGridLines($value)
242 $this->getStyle()->setGridLines($value);
246 * @return string the URL of the background image for the table
248 public function getBackImageUrl()
250 if($this->getHasStyle())
251 return $this->getStyle()->getBackImageUrl();
257 * Sets the URL of the background image for the table
258 * @param string the URL
260 public function setBackImageUrl($value)
262 $this->getStyle()->setBackImageUrl($value);
266 * Renders the openning tag for the table control which will render table caption if present.
267 * @param THtmlWriter the writer used for the rendering purpose
269 public function renderBeginTag($writer)
271 parent::renderBeginTag($writer);
272 if(($caption=$this->getCaption())!=='')
274 if(($align=$this->getCaptionAlign())!==TTableCaptionAlign::NotSet)
275 $writer->addAttribute('align',strtolower($align));
276 $writer->renderBeginTag('caption');
277 $writer->write($caption);
278 $writer->renderEndTag();
283 * Renders body contents of the table.
284 * @param THtmlWriter the writer used for the rendering purpose.
286 public function renderContents($writer)
288 if($this->getHasControls())
290 $renderTableSection=false;
291 foreach($this->getControls() as $row)
293 if($row->getTableSection()!==TTableRowSection::Body)
295 $renderTableSection=true;
299 if($renderTableSection)
301 $currentSection=TTableRowSection::Header;
302 $writer->writeLine();
303 foreach($this->getControls() as $index=>$row)
305 if(($section=$row->getTableSection())===$currentSection)
307 if($index===0 && $currentSection===TTableRowSection::Header)
308 $writer->renderBeginTag('thead');
312 if($currentSection===TTableRowSection::Header)
315 $writer->renderEndTag();
316 if($section===TTableRowSection::Body)
317 $writer->renderBeginTag('tbody');
319 $writer->renderBeginTag('tfoot');
320 $currentSection=$section;
322 else if($currentSection===TTableRowSection::Body)
324 $writer->renderEndTag();
325 if($section===TTableRowSection::Footer)
326 $writer->renderBeginTag('tfoot');
328 throw new TConfigurationException('table_tablesection_outoforder');
329 $currentSection=$section;
332 throw new TConfigurationException('table_tablesection_outoforder');
334 $row->renderControl($writer);
335 $writer->writeLine();
337 $writer->renderEndTag();
341 $writer->writeLine();
342 foreach($this->getControls() as $row)
344 $row->renderControl($writer);
345 $writer->writeLine();
354 * TTableRowCollection class.
356 * TTableRowCollection is used to maintain a list of rows belong to a table.
358 * @author Qiang Xue <qiang.xue@gmail.com>
359 * @package System.Web.UI.WebControls
362 class TTableRowCollection extends TControlCollection
365 * Inserts an item at the specified position.
366 * This overrides the parent implementation by performing additional
367 * operations for each newly added table row.
368 * @param integer the speicified position.
369 * @param mixed new item
370 * @throws TInvalidDataTypeException if the item to be inserted is not a TTableRow object.
372 public function insertAt($index,$item)
374 if($item instanceof TTableRow)
375 parent::insertAt($index,$item);
377 throw new TInvalidDataTypeException('tablerowcollection_tablerow_required');
383 * TTableCaptionAlign class.
384 * TTableCaptionAlign defines the enumerable type for the possible alignments
385 * that a table caption can take.
387 * The following enumerable values are defined:
388 * - NotSet: alignment not specified
390 * - Bottom: bottom aligned
391 * - Left: left aligned
392 * - Right: right aligned
394 * @author Qiang Xue <qiang.xue@gmail.com>
395 * @package System.Web.UI.WebControls
398 class TTableCaptionAlign extends TEnumerable
400 const NotSet='NotSet';
402 const Bottom='Bottom';