From 598fac7617264c20b5c7e6f914b5aba18a677b01 Mon Sep 17 00:00:00 2001 From: xue <> Date: Thu, 19 Jan 2006 17:14:45 +0000 Subject: Fix a few issues with listcontrols about databinding. --- .../pages/Controls/Samples/TListBox/Home.page | 52 +++++++++++++- .../pages/Controls/Samples/TListBox/Home.php | 79 +++++++++++++++------- framework/Web/UI/WebControls/TDataBoundControl.php | 2 +- framework/Web/UI/WebControls/TListControl.php | 18 ++--- 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: - + @@ -154,4 +156,52 @@ Auto postback list box: +

List Boxes with DataBinding

+Use Shift + Mouse Click to change selection +
+ + + + + + + + + + + + + + + + + + +
+Databind to an integer-indexed array: + + + +
+Databind to an associative array: + + + +
+Databind with DataTextField and DataValueField specified: + + + +
\ 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: * - * <com:TListControl> - * <com:TListItem Value="xxx" Text="yyy" > - * <com:TListItem Value="xxx" Text="yyy" Selected="true" > - * <com:TListItem Value="xxx" Text="yyy" > - * </com:TListControl> + * + * + * + * + * * * * 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); } } -- cgit v1.2.3