3 * TTable and TTableRowCollection class file
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2013 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @version $Id: TTable.php 3245 2013-01-07 20:23:32Z ctrlaltca $
10 * @package System.Web.UI.WebControls
14 * Includes TTableRow class
16 Prado::using('System.Web.UI.WebControls.TTableRow');
21 * TTable displays an HTML table on a Web page.
23 * A table may have {@link setCaption Caption}, whose alignment is specified
24 * via {@link setCaptionAlign CaptionAlign}. The table cellpadding and cellspacing
25 * are specified via {@link setCellPadding CellPadding} and {@link setCellSpacing CellSpacing}
26 * properties, respectively. The {@link setGridLines GridLines} specifies how
27 * the table should display its borders. The horizontal alignment of the table
28 * content can be specified via {@link setHorizontalAlign HorizontalAlign},
29 * and {@link setBackImageUrl BackImageUrl} can assign a background image to the table.
31 * A TTable maintains a list of {@link TTableRow} controls in its
32 * {@link getRows Rows} property. Each {@link TTableRow} represents
35 * To populate the table {@link getRows Rows}, you may either use control template
36 * or dynamically create {@link TTableRow} in code.
37 * In template, do as follows to create the table rows and cells,
41 * <com:TTableCell Text="content" />
42 * <com:TTableCell Text="content" />
45 * <com:TTableCell Text="content" />
46 * <com:TTableCell Text="content" />
50 * The above can also be accomplished in code as follows,
54 * $cell=new TTableCell; $cell->Text="content"; $row->Cells->add($cell);
55 * $cell=new TTableCell; $cell->Text="content"; $row->Cells->add($cell);
56 * $table->Rows->add($row);
58 * $cell=new TTableCell; $cell->Text="content"; $row->Cells->add($cell);
59 * $cell=new TTableCell; $cell->Text="content"; $row->Cells->add($cell);
60 * $table->Rows->add($row);
63 * @author Qiang Xue <qiang.xue@gmail.com>
64 * @version $Id: TTable.php 3245 2013-01-07 20:23:32Z ctrlaltca $
65 * @package System.Web.UI.WebControls
68 class TTable extends TWebControl
71 * @return string tag name for the table
73 protected function getTagName()
79 * Adds object parsed from template to the control.
80 * This method adds only {@link TTableRow} objects into the {@link getRows Rows} collection.
81 * All other objects are ignored.
82 * @param mixed object parsed from template
84 public function addParsedObject($object)
86 if($object instanceof TTableRow)
87 $this->getRows()->add($object);
91 * Creates a style object for the control.
92 * This method creates a {@link TTableStyle} to be used by the table.
93 * @return TTableStyle control style to be used
95 protected function createStyle()
97 return new TTableStyle;
101 * Adds attributes to renderer.
102 * @param THtmlWriter the renderer
104 protected function addAttributesToRender($writer)
106 parent::addAttributesToRender($writer);
108 if($this->getHasStyle())
110 if($this->getGridLines()!==TTableGridLines::None)
112 if(($border=$this->getBorderWidth())==='')
115 $border=(int)$border;
118 $writer->addAttribute('border',"$border");
122 * Creates a control collection object that is to be used to hold child controls
123 * @return TTableRowCollection control collection
126 protected function createControlCollection()
128 return new TTableRowCollection($this);
132 * @return TTableRowCollection list of {@link TTableRow} controls
134 public function getRows()
136 return $this->getControls();
140 * @return string table caption
142 public function getCaption()
144 return $this->getViewState('Caption','');
148 * @param string table caption
150 public function setCaption($value)
152 $this->setViewState('Caption',$value,'');
156 * @return TTableCaptionAlign table caption alignment. Defaults to TTableCaptionAlign::NotSet.
158 public function getCaptionAlign()
160 return $this->getViewState('CaptionAlign',TTableCaptionAlign::NotSet);
164 * @param TTableCaptionAlign table caption alignment.
166 public function setCaptionAlign($value)
168 $this->setViewState('CaptionAlign',TPropertyValue::ensureEnum($value,'TTableCaptionAlign'),TTableCaptionAlign::NotSet);
172 * @return integer the cellspacing for the table. Defaults to -1, meaning not set.
174 public function getCellSpacing()
176 if($this->getHasStyle())
177 return $this->getStyle()->getCellSpacing();
183 * @param integer the cellspacing for the table. Defaults to -1, meaning not set.
185 public function setCellSpacing($value)
187 $this->getStyle()->setCellSpacing($value);
191 * @return integer the cellpadding for the table. Defaults to -1, meaning not set.
193 public function getCellPadding()
195 if($this->getHasStyle())
196 return $this->getStyle()->getCellPadding();
202 * @param integer the cellpadding for the table. Defaults to -1, meaning not set.
204 public function setCellPadding($value)
206 $this->getStyle()->setCellPadding($value);
210 * @return THorizontalAlign the horizontal alignment of the table content. Defaults to THorizontalAlign::NotSet.
212 public function getHorizontalAlign()
214 if($this->getHasStyle())
215 return $this->getStyle()->getHorizontalAlign();
217 return THorizontalAlign::NotSet;
221 * @param THorizontalAlign the horizontal alignment of the table content.
223 public function setHorizontalAlign($value)
225 $this->getStyle()->setHorizontalAlign($value);
229 * @return TTableGridLines the grid line setting of the table. Defaults to TTableGridLines::None.
231 public function getGridLines()
233 if($this->getHasStyle())
234 return $this->getStyle()->getGridLines();
236 return TTableGridLines::None;
240 * @param TTableGridLines the grid line setting of the table
242 public function setGridLines($value)
244 $this->getStyle()->setGridLines($value);
248 * @return string the URL of the background image for the table
250 public function getBackImageUrl()
252 if($this->getHasStyle())
253 return $this->getStyle()->getBackImageUrl();
259 * Sets the URL of the background image for the table
260 * @param string the URL
262 public function setBackImageUrl($value)
264 $this->getStyle()->setBackImageUrl($value);
268 * Renders the openning tag for the table control which will render table caption if present.
269 * @param THtmlWriter the writer used for the rendering purpose
271 public function renderBeginTag($writer)
273 parent::renderBeginTag($writer);
274 if(($caption=$this->getCaption())!=='')
276 if(($align=$this->getCaptionAlign())!==TTableCaptionAlign::NotSet)
277 $writer->addAttribute('align',strtolower($align));
278 $writer->renderBeginTag('caption');
279 $writer->write($caption);
280 $writer->renderEndTag();
285 * Renders body contents of the table.
286 * @param THtmlWriter the writer used for the rendering purpose.
288 public function renderContents($writer)
290 if($this->getHasControls())
292 $renderTableSection=false;
293 foreach($this->getControls() as $row)
295 if($row->getTableSection()!==TTableRowSection::Body)
297 $renderTableSection=true;
301 if($renderTableSection)
303 $currentSection=TTableRowSection::Header;
304 $writer->writeLine();
305 foreach($this->getControls() as $index=>$row)
307 if(($section=$row->getTableSection())===$currentSection)
309 if($index===0 && $currentSection===TTableRowSection::Header)
310 $writer->renderBeginTag('thead');
314 if($currentSection===TTableRowSection::Header)
317 $writer->renderEndTag();
318 if($section===TTableRowSection::Body)
319 $writer->renderBeginTag('tbody');
321 $writer->renderBeginTag('tfoot');
322 $currentSection=$section;
324 else if($currentSection===TTableRowSection::Body)
326 $writer->renderEndTag();
327 if($section===TTableRowSection::Footer)
328 $writer->renderBeginTag('tfoot');
330 throw new TConfigurationException('table_tablesection_outoforder');
331 $currentSection=$section;
334 throw new TConfigurationException('table_tablesection_outoforder');
336 $row->renderControl($writer);
337 $writer->writeLine();
339 $writer->renderEndTag();
343 $writer->writeLine();
344 foreach($this->getControls() as $row)
346 $row->renderControl($writer);
347 $writer->writeLine();
356 * TTableRowCollection class.
358 * TTableRowCollection is used to maintain a list of rows belong to a table.
360 * @author Qiang Xue <qiang.xue@gmail.com>
361 * @version $Id: TTable.php 3245 2013-01-07 20:23:32Z ctrlaltca $
362 * @package System.Web.UI.WebControls
365 class TTableRowCollection extends TControlCollection
368 * Inserts an item at the specified position.
369 * This overrides the parent implementation by performing additional
370 * operations for each newly added table row.
371 * @param integer the speicified position.
372 * @param mixed new item
373 * @throws TInvalidDataTypeException if the item to be inserted is not a TTableRow object.
375 public function insertAt($index,$item)
377 if($item instanceof TTableRow)
378 parent::insertAt($index,$item);
380 throw new TInvalidDataTypeException('tablerowcollection_tablerow_required');
386 * TTableCaptionAlign class.
387 * TTableCaptionAlign defines the enumerable type for the possible alignments
388 * that a table caption can take.
390 * The following enumerable values are defined:
391 * - NotSet: alignment not specified
393 * - Bottom: bottom aligned
394 * - Left: left aligned
395 * - Right: right aligned
397 * @author Qiang Xue <qiang.xue@gmail.com>
398 * @version $Id: TTable.php 3245 2013-01-07 20:23:32Z ctrlaltca $
399 * @package System.Web.UI.WebControls
402 class TTableCaptionAlign extends TEnumerable
404 const NotSet='NotSet';
406 const Bottom='Bottom';