diff options
4 files changed, 115 insertions, 36 deletions
| diff --git a/demos/quickstart/protected/pages/Controls/Samples/TListBox/Home.page b/demos/quickstart/protected/pages/Controls/Samples/TListBox/Home.page index f6640879..66ac4fa2 100644 --- a/demos/quickstart/protected/pages/Controls/Samples/TListBox/Home.page +++ b/demos/quickstart/protected/pages/Controls/Samples/TListBox/Home.page @@ -142,7 +142,9 @@ List box's behavior upon postback:  Auto postback list box:
  </td>
  <td class="sampleaction">
 -<com:TListBox AutoPostBack="true" SelectionMode="Multiple" SelectedIndexChanged="multiSelectionChanged">
 +<com:TListBox AutoPostBack="true"
 +	SelectionMode="Multiple"
 +	SelectedIndexChanged="multiSelectionChanged">
    <com:TListItem Value="value 1" Text="item 1" />
    <com:TListItem Value="value 2" Text="item 2" Selected="true" />
    <com:TListItem Value="value 3" Text="item 3" />
 @@ -154,4 +156,52 @@ Auto postback list box:  </table>
 +<h2>List Boxes with DataBinding</h2>
 +<i>Use Shift + Mouse Click to change selection</i>
 +<br/>
 +
 +<table class="sampletable">
 +
 +<tr>
 +<td class="samplenote">
 +Databind to an integer-indexed array:
 +</td>
 +<td class="sampleaction">
 +<com:TListBox ID="DBListBox1"
 +	AutoPostBack="true"
 +	SelectionMode="Multiple"
 +	SelectedIndexChanged="DBListBox1Changed" />
 +<com:TLabel ID="DBListBox1Result" ForeColor="red" />
 +</td>
 +</tr>
 +
 +<tr>
 +<td class="samplenote">
 +Databind to an associative array:
 +</td>
 +<td class="sampleaction">
 +<com:TListBox ID="DBListBox2"
 +	AutoPostBack="true"
 +	SelectionMode="Multiple"
 +	SelectedIndexChanged="DBListBox2Changed" />
 +<com:TLabel ID="DBListBox2Result" ForeColor="red" />
 +</td>
 +</tr>
 +
 +<tr>
 +<td class="samplenote">
 +Databind with DataTextField and DataValueField specified:
 +</td>
 +<td class="sampleaction">
 +<com:TListBox ID="DBListBox3"
 +	AutoPostBack="true"
 +	SelectionMode="Multiple"
 +	DataTextField="name"
 +	DataValueField="id"
 +	SelectedIndexChanged="DBListBox3Changed" />
 +<com:TLabel ID="DBListBox3Result" ForeColor="red" />
 +</td>
 +</tr>
 +
 +</table>
  </com:TContent>
\ No newline at end of file diff --git a/demos/quickstart/protected/pages/Controls/Samples/TListBox/Home.php b/demos/quickstart/protected/pages/Controls/Samples/TListBox/Home.php index 6b1d07e1..cd9d7c13 100644 --- a/demos/quickstart/protected/pages/Controls/Samples/TListBox/Home.php +++ b/demos/quickstart/protected/pages/Controls/Samples/TListBox/Home.php @@ -2,50 +2,77 @@  class Home extends TPage
  {
 -	public function selectionChanged($sender,$param)
 +	public function onLoad($param)
  	{
 -		$index=$sender->SelectedIndex;
 -		$value=$sender->SelectedValue;
 -		$text=$sender->SelectedItem->Text;
 -		$this->SelectionResult->Text="Your selection is (Index: $index, Value: $value, Text: $text).";
 +		parent::onLoad($param);
 +		if(!$this->IsPostBack)
 +		{
 +			$data=array('item 1','item 2','item 3','item 4');
 +			$this->DBListBox1->DataSource=$data;
 +			$this->DBListBox1->dataBind();
 +
 +			$data=array('key 1'=>'item 1','key 2'=>'item 2',
 +						'key 3'=>'item 3','key 4'=>'item 4');
 +			$this->DBListBox2->DataSource=$data;
 +			$this->DBListBox2->dataBind();
 +
 +			$data=array(
 +				array('id'=>'001','name'=>'John','age'=>31),
 +				array('id'=>'002','name'=>'Mary','age'=>30),
 +				array('id'=>'003','name'=>'Cary','age'=>20));
 +			$this->DBListBox3->DataSource=$data;
 +			$this->DBListBox3->dataBind();
 +		}
  	}
 -	public function buttonClicked($sender,$param)
 +	public function DBListBox1Changed($sender,$param)
  	{
 -		$index=$this->ListBox1->SelectedIndex;
 -		$value=$this->ListBox1->SelectedValue;
 -		$text=$this->ListBox1->SelectedItem->Text;
 -		$this->SelectionResult2->Text="Your selection is (Index: $index, Value: $value, Text: $text).";
 +		$this->collectSelectionResult($sender,$this->DBListBox1Result);
  	}
 -	public function multiSelectionChanged($sender,$param)
 +	public function DBListBox2Changed($sender,$param)
 +	{
 +		$this->collectSelectionResult($sender,$this->DBListBox2Result);
 +	}
 +
 +	public function DBListBox3Changed($sender,$param)
 +	{
 +		$this->collectSelectionResult($sender,$this->DBListBox3Result);
 +	}
 +
 +	protected function collectSelectionResult($input,$output)
  	{
 -		$indices=$sender->SelectedIndices;
 +		$indices=$input->SelectedIndices;
  		$result='';
  		foreach($indices as $index)
  		{
 -			$item=$sender->Items[$index];
 +			$item=$input->Items[$index];
  			$result.="(Index: $index, Value: $item->Value, Text: $item->Text)\n";
  		}
  		if($result==='')
 -			$this->MultiSelectionResult->Text='Your selection is empty.';
 +			$output->Text='Your selection is empty.';
  		else
 -			$this->MultiSelectionResult->Text='Your selection is: '.$result;
 +			$output->Text='Your selection is: '.$result;
 +	}
 +
 +	public function selectionChanged($sender,$param)
 +	{
 +		$this->collectSelectionResult($sender,$this->SelectionResult);
 +	}
 +
 +	public function buttonClicked($sender,$param)
 +	{
 +		$this->collectSelectionResult($this->ListBox1,$this->SelectionResult2);
 +	}
 +
 +	public function multiSelectionChanged($sender,$param)
 +	{
 +		$this->collectSelectionResult($sender,$this->MultiSelectionResult);
  	}
  	public function buttonClicked2($sender,$param)
  	{
 -		$indices=$this->ListBox2->SelectedIndices;
 -		$result='';
 -		foreach($indices as $index)
 -		{
 -			$item=$this->ListBox2->Items[$index];
 -			$result.="(Index: $index, Value: $item->Value, Text: $item->Text)\n";
 -		}
 -		if($result==='')
 -			$this->MultiSelectionResult2->Text='Your selection is empty.';
 -		else
 -			$this->MultiSelectionResult2->Text='Your selection is: '.$result;
 +		$this->collectSelectionResult($this->ListBox2,$this->MultiSelectionResult2);
  	}
  }
 diff --git a/framework/Web/UI/WebControls/TDataBoundControl.php b/framework/Web/UI/WebControls/TDataBoundControl.php index 1108e0f6..aa177f07 100644 --- a/framework/Web/UI/WebControls/TDataBoundControl.php +++ b/framework/Web/UI/WebControls/TDataBoundControl.php @@ -334,7 +334,7 @@ abstract class TDataBoundControl extends TWebControl  			return $list;
  		}
  		else if(is_array($value))
 -			return new TList($value);
 +			return new TMap($value);
  		else if(($value instanceof Traversable) || $value===null)
  			return $value;
  		else
 diff --git a/framework/Web/UI/WebControls/TListControl.php b/framework/Web/UI/WebControls/TListControl.php index 537df7c5..d15a7bf1 100644 --- a/framework/Web/UI/WebControls/TListControl.php +++ b/framework/Web/UI/WebControls/TListControl.php @@ -43,11 +43,11 @@ Prado::using('System.Web.UI.WebControls.TDataBoundControl');   * The latter two are covered in {@link TDataBoundControl}. To specify items via
   * template, using the following template syntax:
   * <code>
 - * <com:TListControl>
 - *   <com:TListItem Value="xxx" Text="yyy" >
 - *   <com:TListItem Value="xxx" Text="yyy" Selected="true" >
 - *   <com:TListItem Value="xxx" Text="yyy" >
 - * </com:TListControl>
 + * <com:TListControl>
 + *   <com:TListItem Value="xxx" Text="yyy" >
 + *   <com:TListItem Value="xxx" Text="yyy" Selected="true" >
 + *   <com:TListItem Value="xxx" Text="yyy" >
 + * </com:TListControl>
   * </code>
   *
   * When {@link setDataSource DataSource} or {@link setDataSourceID DataSourceID}
 @@ -167,16 +167,18 @@ abstract class TListControl extends TDataBoundControl  		if($valueField==='')
  			$valueField=1;
  		$textFormat=$this->getDataTextFormatString();
 -		foreach($data as $object)
 +		foreach($data as $key=>$object)
  		{
  			$item=new TListItem;
 -			if(isset($object[$textField]))
 +			if(!is_string($object) && isset($object[$textField]))
  				$text=$object[$textField];
  			else
  				$text=TPropertyValue::ensureString($object);
  			$item->setText($textFormat===''?$text:sprintf($textFormat,$text));
 -			if(isset($object[$valueField]))
 +			if(!is_string($object) && isset($object[$valueField]))
  				$item->setValue($object[$valueField]);
 +			else if(!is_integer($key))
 +				$item->setValue($key);
  			$items->add($item);
  		}
  	}
 | 
