summaryrefslogtreecommitdiff
path: root/framework/Web
diff options
context:
space:
mode:
authorxue <>2006-05-17 20:16:19 +0000
committerxue <>2006-05-17 20:16:19 +0000
commite1d9e11577c1cea63e72a20a98cd9b6acc8e5ba7 (patch)
tree339cf40dec53d784e4e057e49f5e3e9b4b614552 /framework/Web
parentf63823ec5a46e633846625964a9c0b34b7694026 (diff)
Refactored TListControl.
Diffstat (limited to 'framework/Web')
-rw-r--r--framework/Web/UI/WebControls/TListControl.php58
1 files changed, 38 insertions, 20 deletions
diff --git a/framework/Web/UI/WebControls/TListControl.php b/framework/Web/UI/WebControls/TListControl.php
index 1c615edc..46fab074 100644
--- a/framework/Web/UI/WebControls/TListControl.php
+++ b/framework/Web/UI/WebControls/TListControl.php
@@ -175,7 +175,7 @@ abstract class TListControl extends TDataBoundControl
$textFormat=$this->getDataTextFormatString();
foreach($data as $key=>$object)
{
- $item=new TListItem;
+ $item=$items->createListItem();
if(is_array($object) || is_object($object))
{
$text=TDataFieldAccessor::getDataFieldValue($object,$textField);
@@ -188,7 +188,6 @@ abstract class TListControl extends TDataBoundControl
$item->setValue("$key");
}
$item->setText($this->formatDataValue($textFormat,$text));
- $items->add($item);
}
// SelectedValue or SelectedIndex may be set before databinding
// so we make them be effective now
@@ -209,6 +208,16 @@ abstract class TListControl extends TDataBoundControl
}
/**
+ * Creates a collection object to hold list items.
+ * This method may be overriden to create a customized collection.
+ * @return TListItemCollection the collection object
+ */
+ protected function createListItemCollection()
+ {
+ return new TListItemCollection;
+ }
+
+ /**
* Saves items into viewstate.
* This method is invoked right before control state is to be saved.
*/
@@ -231,7 +240,7 @@ abstract class TListControl extends TDataBoundControl
$this->_stateLoaded=true;
if(!$this->getIsDataBound())
{
- $this->_items=new TListItemCollection;
+ $this->_items=$this->createListItemCollection();
$this->_items->loadState($this->getViewState('Items',null));
}
$this->clearViewState('Items');
@@ -372,7 +381,7 @@ abstract class TListControl extends TDataBoundControl
public function getItems()
{
if(!$this->_items)
- $this->_items=new TListItemCollection;
+ $this->_items=$this->createListItemCollection();
return $this->_items;
}
@@ -630,6 +639,23 @@ abstract class TListControl extends TDataBoundControl
class TListItemCollection extends TList
{
/**
+ * Creates a list item object.
+ * This method may be overriden to provide a customized list item object.
+ * @param integer index where the newly created item is to be inserted at.
+ * If -1, the item will be appended to the end.
+ * @return TListItem list item object
+ */
+ public function createListItem($index=-1)
+ {
+ $item=new TListItem;
+ if($index<0)
+ $this->add($item);
+ else
+ $this->insertAt($index,$item);
+ return $item;
+ }
+
+ /**
* Inserts an item into the collection.
* @param integer the location where the item will be inserted.
* The current item at the place and the following ones will be moved backward.
@@ -638,10 +664,13 @@ class TListItemCollection extends TList
*/
public function insertAt($index,$item)
{
- if(is_string($item))
- parent::insertAt($index,new TListItem($item));
- else if($item instanceof TListItem)
+ if($item instanceof TListItem)
parent::insertAt($index,$item);
+ else if(is_string($item))
+ {
+ $item=$this->createListItem($index);
+ $item->setText($item);
+ }
else
throw new TInvalidDataTypeException('listitemcollection_item_invalid',get_class($this));
}
@@ -721,10 +750,7 @@ class TListItemCollection extends TList
{
$this->clear();
if($state!==null)
- {
- foreach($state as $item)
- $this->add(new TListItem($item[0],$item[1],$item[2],$item[3]));
- }
+ $this->copyFrom($state);
}
/**
@@ -734,15 +760,7 @@ class TListItemCollection extends TList
*/
public function saveState()
{
- if($this->getCount()>0)
- {
- $state=array();
- foreach($this as $item)
- $state[]=array($item->getText(),$item->getValue(),$item->getEnabled(),$item->getSelected());
- return $state;
- }
- else
- return null;
+ return ($this->getCount()>0) ? $this->toArray() : null;
}
}