/** * Creates a datalist item instance based on the item type and index. * @param integer zero-based item index * @param TListItemType item type * @return TControl created datalist item */ protected function createItem($itemIndex, $itemType) { $template = null; $classPath = null; switch ($itemType) { case TListItemType::Item: $classPath = $this->getItemRenderer(); $template = $this->_itemTemplate; break; case TListItemType::AlternatingItem: list($classPath, $template) = $this->getAlternatingItemDisplay(); break; case TListItemType::SelectedItem: list($classPath, $template) = $this->getSelectedItemDisplay($itemIndex); break; case TListItemType::EditItem: list($classPath, $template) = $this->getEditItemDisplay($itemIndex); break; case TListItemType::Header: $classPath = $this->getHeaderRenderer(); $template = $this->_headerTemplate; break; case TListItemType::Footer: $classPath = $this->getFooterRenderer(); $template = $this->_footerTemplate; break; case TListItemType::Separator: $classPath = $this->getSeparatorRenderer(); $template = $this->_separatorTemplate; break; default: throw new TInvalidDataValueException('datalist_itemtype_unknown', $itemType); } if ($classPath !== '') { $item = Prado::createComponent($classPath); if ($item instanceof IItemDataRenderer) { $item->setItemIndex($itemIndex); $item->setItemType($itemType); } } else { if ($template !== null) { $item = new TDataListItem(); $item->setItemIndex($itemIndex); $item->setItemType($itemType); $template->instantiateIn($item); } else { $item = null; } } return $item; }
/** * Displays the data list. * @return string the rendering result. */ public function render() { $attr = $this->renderAttributes(); if ($this->getRepeatLayout() == 'Raw') { $content = "<span {$attr}>"; foreach ($this->getBodies() as $body) { $content .= $body->render(); } return $content . "</span>"; } $count = $this->items->length(); $cols = $this->getRepeatColumns(); $rows = $count % $cols == 0 ? $count / $cols : intval($count / $cols) + 1; $output = array(); if ($this->getRepeatDirection() === 'Vertical') { $n = 0; for ($j = 0; $j < $cols; ++$j) { $r = ($count - $n) % ($cols - $j) == 0 ? ($count - $n) / ($cols - $j) : intval(($count - $n) / ($cols - $j)) + 1; for ($i = 0; $i < $rows; ++$i) { if ($i < $r) { $output[$i][$j] = $n; $n++; } else { $output[$i][$j] = -1; } } } } else { for ($i = 0; $i < $rows; ++$i) { for ($j = 0; $j < $cols; ++$j) { $n = $i * $cols + $j; if ($n < $count) { $output[$i][$j] = $n; } else { $output[$i][$j] = -1; } } } } $itemStyle = $this->getItemStyle(); if (strlen($itemStyle)) { $itemStyle = ' style="' . $itemStyle . '"'; } $altItemStyle = $this->getAlternatingItemStyle(); if (strlen($altItemStyle)) { $altItemStyle = ' style="' . $altItemStyle . '"'; } $separatorStyle = $this->getSeparatorStyle(); if (strlen($separatorStyle)) { $separatorStyle = ' style="' . $separatorStyle . '"'; } $editItemStyle = $this->getEditItemStyle(); if (strlen($editItemStyle)) { $editItemStyle = ' style="' . $editItemStyle . '"'; } $selectedItemStyle = $this->getSelectedItemStyle(); if (strlen($selectedItemStyle)) { $selectedItemStyle = ' style="' . $selectedItemStyle . '"'; } $headerStyle = $this->getHeaderStyle(); if (strlen($headerStyle)) { $headerStyle = ' style="' . $headerStyle . '"'; } $footerStyle = $this->getFooterStyle(); if (strlen($footerStyle)) { $footerStyle = ' style="' . $footerStyle . '"'; } if ($this->getRepeatLayout() === 'Table') { if ($cols <= 1) { $colspan = ''; } else { if (count($seps) > 0) { $colspan = " colspan=\"" . ($cols + $cols) . "\""; } else { $colspan = " colspan=\"{$cols}\""; } } $content = "<table {$attr}>\n"; if ($this->header && $this->isShowHeader()) { $a = $this->header->renderAttributes(); $content .= "<tr>\n<td{$colspan}{$headerStyle} {$a}>" . $this->header->render() . "</td></tr>\n"; } for ($i = 0; $i < $rows; ++$i) { $content .= "<tr>\n"; for ($j = 0; $j < $cols; ++$j) { $index = $output[$i][$j]; if (isset($this->items[$index])) { $item = $this->items[$index]; $type = $item->getType(); if ($type == TDataListItem::TYPE_SELECTED_ITEM) { $style = $selectedItemStyle; } else { if ($type == TDataListItem::TYPE_EDIT_ITEM) { $style = $editItemStyle; } else { if ($type == TDataListItem::TYPE_ITEM) { $style = $itemStyle; } else { if ($type == TDataListItem::TYPE_ALTERNATING_ITEM) { $style = $altItemStyle; } else { $style = ''; } } } } $a = $item->renderAttributes(); $content .= "<td{$style} {$a}>" . $item->render() . "</td>\n"; } else { $content .= "<td> </td>\n"; } if (isset($this->separators[$index])) { $separator = $this->separators[$index]; $a = $separator->renderAttributes(); if ($cols > 1) { $content .= "<td{$separatorStyle} {$a}>" . $separator->render() . "</td>\n"; } else { $content .= "</tr>\n<tr>\n<td{$separatorStyle} {$a}>" . $separator->render() . "</td>\n"; } } } $content .= "</tr>\n"; } if ($this->footer && $this->isShowFooter()) { $a = $this->footer->renderAttributes(); $content .= "<tr>\n<td{$colspan}{$footerStyle} {$a}>" . $this->footer->render() . "</td></tr>\n"; } $content .= "</table>\n"; } else { $content = "<span {$attr}>"; if ($this->header && $this->isShowHeader()) { $a = $this->header->renderAttributes(); $content .= "<span{$headerStyle} {$a}>" . $this->header->render() . "</span><br/>\n"; } for ($i = 0; $i < $rows; ++$i) { for ($j = 0; $j < $cols; ++$j) { $index = $output[$i][$j]; if (isset($this->items[$index])) { $item = $this->items[$index]; $type = $item->getType(); if ($type == TDataListItem::TYPE_SELECTED_ITEM) { $style = $selectedItemStyle; } else { if ($type == TDataListItem::TYPE_EDIT_ITEM) { $style = $editItemStyle; } else { if ($type == TDataListItem::TYPE_ITEM) { $style = $itemStyle; } else { if ($type == TDataListItem::TYPE_ALTERNATING_ITEM) { $style = $altItemStyle; } else { $style = ''; } } } } $a = $item->renderAttributes(); $content .= "<span{$style} {$a}>" . $item->render() . "</span>\n"; } if (isset($this->separators[$index])) { $separator = $this->separators[$index]; $a = $separator->renderAttributes(); if ($cols > 1) { $content .= "<span{$separatorStyle} {$a}>" . $separator->render() . "</span>"; } else { $content .= "</span><br/>\n<span{$separatorStyle} {$a}>" . $separator->render() . "</span>"; } } } $content .= "<br/>\n"; } if ($this->footer && $this->isShowFooter()) { $a = $this->footer->renderAttributes(); $content .= "<span{$footerStyle} {$a}>" . $this->footer->render() . "</span><br/>\n"; } $content .= "</span>\n"; } return $content; }