summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--HISTORY1
-rw-r--r--framework/Exceptions/messages.txt1
-rw-r--r--framework/Web/UI/WebControls/TListControl.php80
3 files changed, 53 insertions, 29 deletions
diff --git a/HISTORY b/HISTORY
index e1a68044..82bfd5bc 100644
--- a/HISTORY
+++ b/HISTORY
@@ -1,6 +1,7 @@
Version 3.0.4 September 3, 2006
===============================
BUG: Fixed a bug that would prevent from using <prop:> tag in skins (Qiang)
+ENH: TListControl.SelectedValues and SelectedIndices can now be set before databinding (Qiang)
Version 3.0.3 August 6, 2006
============================
diff --git a/framework/Exceptions/messages.txt b/framework/Exceptions/messages.txt
index cbb7cf04..d0aff516 100644
--- a/framework/Exceptions/messages.txt
+++ b/framework/Exceptions/messages.txt
@@ -192,6 +192,7 @@ listcontrol_selection_invalid = {0} has an invalid selection that is set befor
listcontrol_selectedindex_invalid = {0}.SelectedIndex has an invalid value {1}.
listcontrol_selectedvalue_invalid = {0}.SelectedValue has an invalid value '{1}'.
listcontrol_expression_invalid = {0} is evaluating an invalid expression '{1}' : {2}
+listcontrol_multiselect_unsupported = {0} does not support multiselection.
label_associatedcontrol_invalid = TLabel.AssociatedControl '{0}' cannot be found.
diff --git a/framework/Web/UI/WebControls/TListControl.php b/framework/Web/UI/WebControls/TListControl.php
index 15b6ec94..1c65259c 100644
--- a/framework/Web/UI/WebControls/TListControl.php
+++ b/framework/Web/UI/WebControls/TListControl.php
@@ -94,6 +94,8 @@ abstract class TListControl extends TDataBoundControl
*/
private $_cachedSelectedIndex=-1;
private $_cachedSelectedValue=null;
+ private $_cachedSelectedIndices=null;
+ private $_cachedSelectedValues=null;
/**
* @return string tag name of the list control
@@ -204,20 +206,34 @@ abstract class TListControl extends TDataBoundControl
// so we make them be effective now
if($this->_cachedSelectedValue!==null)
{
- $index=$items->findIndexByValue($this->_cachedSelectedValue);
- if($index===-1 || ($this->_cachedSelectedIndex!==-1 && $this->_cachedSelectedIndex!==$index))
- throw new TInvalidDataValueException('listcontrol_selection_invalid',get_class($this));
- $this->setSelectedIndex($index);
- $this->_cachedSelectedValue=null;
- $this->_cachedSelectedIndex=-1;
+ $this->setSelectedValue($this->_cachedSelectedValue);
+ $this->resetCachedSelections();
}
else if($this->_cachedSelectedIndex!==-1)
{
$this->setSelectedIndex($this->_cachedSelectedIndex);
- $this->_cachedSelectedIndex=-1;
+ $this->resetCachedSelections();
+ }
+ else if($this->_cachedSelectedValues!==null)
+ {
+ $this->setSelectedValues($this->_cachedSelectedValues);
+ $this->resetCachedSelections();
+ }
+ else if($this->_cachedSelectedIndices!==null)
+ {
+ $this->setSelectedIndices($this->_cachedSelectedIndices);
+ $this->resetCachedSelections();
}
}
+ private function resetCachedSelections()
+ {
+ $this->_cachedSelectedValue=null;
+ $this->_cachedSelectedIndex=-1;
+ $this->_cachedSelectedValues=null;
+ $this->_cachedSelectedIndices=null;
+ }
+
/**
* Creates a collection object to hold list items.
* This method may be overriden to create a customized collection.
@@ -446,23 +462,26 @@ abstract class TListControl extends TDataBoundControl
}
/**
- * Selects a list of values by their indices.
- * Unlike {@link setSelectedIndex}, this function should only be called
- * after the list items are populated. Otherwise, it would have no effect.
* @param array list of index of items to be selected
*/
public function setSelectedIndices($indices)
{
- if($this->_items)
+ if($this->getIsMultiSelect())
{
- $this->clearSelection();
- $n=$this->_items->getCount();
- foreach($indices as $index)
+ if($this->_items)
{
- if($index>=0 && $index<$n)
- $this->_items->itemAt($index)->setSelected(true);
+ $this->clearSelection();
+ $n=$this->_items->getCount();
+ foreach($indices as $index)
+ {
+ if($index>=0 && $index<$n)
+ $this->_items->itemAt($index)->setSelected(true);
+ }
}
+ $this->_cachedSelectedIndices=$indices;
}
+ else
+ throw new TNotSupportedException('listcontrol_multiselect_unsupported',get_class($this));
}
/**
@@ -527,27 +546,30 @@ abstract class TListControl extends TDataBoundControl
}
/**
- * Selects a list of values.
- * Unlike {@link setSelectedValue}, this function should only be called
- * after the list items are populated. Otherwise, it would have no effect.
* @param array list of the selected item values
*/
public function setSelectedValues($values)
{
- if($this->_items)
+ if($this->getIsMultiSelect())
{
- $this->clearSelection();
- $lookup=array();
- foreach($this->_items as $item)
- $lookup[$item->getValue()]=$item;
- foreach($values as $value)
+ if($this->_items)
{
- if(isset($lookup["$value"]))
- $lookup["$value"]->setSelected(true);
- else
- throw new TInvalidDataValueException('listcontrol_selectedvalue_invalid',get_class($this),$value);
+ $this->clearSelection();
+ $lookup=array();
+ foreach($this->_items as $item)
+ $lookup[$item->getValue()]=$item;
+ foreach($values as $value)
+ {
+ if(isset($lookup["$value"]))
+ $lookup["$value"]->setSelected(true);
+ else
+ throw new TInvalidDataValueException('listcontrol_selectedvalue_invalid',get_class($this),$value);
+ }
}
+ $this->_cachedSelectedValues=$values;
}
+ else
+ throw new TNotSupportedException('listcontrol_multiselect_unsupported',get_class($this));
}
/**