summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxue <>2006-02-01 19:26:46 +0000
committerxue <>2006-02-01 19:26:46 +0000
commitd602f32bf108ccf58df7880227dc64d2cfa33e13 (patch)
tree3fa3d93ec96b3c9e88a004cb6ed2c67e184d4d12
parenta7edca99c188c574125785c87198606527981d01 (diff)
Modified TDataList demo 2 to make it more demonstrative.
-rw-r--r--demos/quickstart/protected/pages/Controls/DataList.page2
-rw-r--r--demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.page123
-rw-r--r--demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.php108
-rw-r--r--framework/Web/UI/WebControls/TDataList.php2
4 files changed, 146 insertions, 89 deletions
diff --git a/demos/quickstart/protected/pages/Controls/DataList.page b/demos/quickstart/protected/pages/Controls/DataList.page
index 1927f558..d003ae8e 100644
--- a/demos/quickstart/protected/pages/Controls/DataList.page
+++ b/demos/quickstart/protected/pages/Controls/DataList.page
@@ -50,7 +50,7 @@ The following example shows how to use TDataList to display tabular data, with d
</p>
<com:RunBar PagePath="Controls.Samples.TDataList.Sample1" />
<p>
-An important use of TDataList is for maintaining tabular data, including browsing, editting, deleting data items. This is enabled by the command events and various item templates of TDataList. The following example displays a computer product information. Users can add new products, modify or delete existing ones.
+A common use of TDataList is for maintaining tabular data, including browsing, editting, deleting data items. This is enabled by the command events and various item templates of TDataList. The following example displays a computer product information. Users can add new products, modify or delete existing ones. In order to locate the data item for updating or deleting, <tt>DataKeys</tt> property is used.
</p>
<com:RunBar PagePath="Controls.Samples.TDataList.Sample2" />
diff --git a/demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.page b/demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.page
index 018aabfd..16adf66b 100644
--- a/demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.page
+++ b/demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.page
@@ -1,74 +1,79 @@
<com:TContent ID="body">
<h1>TDataList Sample 2</h1>
-<p>This demo shows a simple application of TDataList control.</p>
-<p>TDataList allows you to show a list of data items (rows), edit, select and delete them.</p>
+<p>This demo shows a simple application of TDataList control in maintaining a list of computer products. The application allows you to browse a list of inventory computer products, edit, select and delete them.
+</p>
+<p>
+Be aware, for simplicity, this application does not do any input validation. In real applications, make sure user inputs are valid before saving them into databases.
+</p>
+
+<com:TDataList
+ ID="DataList"
+ DataKeyField="id"
+ OnEditCommand="editItem"
+ OnCancelCommand="cancelItem"
+ OnUpdateCommand="updateItem"
+ OnDeleteCommand="deleteItem"
+ OnSelectedIndexChanged="selectItem"
+ HeaderStyle.BackColor="#AAAADD"
+ ItemStyle.BackColor="#EEEEEE"
+ AlternatingItemStyle.BackColor="#F5F5F5"
+ EditItemStyle.BackColor="lightgreen"
+ SelectedItemStyle.BackColor="lightyellow">
-<com:TDataList ID="DataList"
- CellPadding="2"
- CellSpacing="3"
- GridLines="Both"
- RepeatColumns="1"
- RepeatLayout="Table"
- RepeatDirection="Vertical"
- OnEditCommand="editItem"
- OnCancelCommand="cancelItem"
- OnUpdateCommand="updateItem"
- OnDeleteCommand="deleteItem"
- OnSelectedIndexChanged="selectItem"
- HeaderStyle.BackColor="#AAAADD"
- ItemStyle.BackColor="#EEEEEE"
- SeparatorStyle.BackColor="#FAFAFA"
- FooterStyle.BackColor="#BBBBBB"
- EditItemStyle.BackColor="lightgreen"
- SelectedItemStyle.BackColor="lightyellow">
<prop:HeaderTemplate>
<div style="font-weight:bold; text-align:center;">Computer Parts</div>
</prop:HeaderTemplate>
<prop:ItemTemplate>
<table border="0" width="100%">
-<tr><td>
-<com:TLinkButton
- Text=<%#$this->Parent->DataItem['id']%>
- CommandName="select"
- />
-<%#$this->Parent->DataItem['name']%>
-</td>
-<td align="right">
-<com:TLinkButton
- Text="Edit"
- CommandName="edit"
- />
-<com:TLinkButton
- Text="Delete"
- CommandName="delete"
- Attributes.onclick="if(!confirm('Are you sure?')) return false;"
- />
-</td></tr>
+<tr>
+ <td>
+ <com:TLinkButton
+ Text=<%#$this->Parent->DataItem['id']%>
+ CommandName="select"
+ />
+ <%#$this->Parent->DataItem['name']%>
+ </td>
+ <td align="right">
+ <com:TLinkButton
+ Text="Edit"
+ CommandName="edit"
+ />
+ <com:TLinkButton
+ Text="Delete"
+ CommandName="delete"
+ Attributes.onclick="if(!confirm('Are you sure?')) return false;"
+ />
+ </td>
+</tr>
</table>
</prop:ItemTemplate>
<prop:SelectedItemTemplate>
<table border="1" width="100%">
-<tr><th>ID</th><th>Name</th><th>Quantity</th><th>Price</th></tr>
<tr>
-<td align="right"><%#$this->Parent->DataItem['id'] %></td>
-<td align="right"><%#$this->Parent->DataItem['name'] %></td>
-<td align="right"><%#$this->Parent->DataItem['quantity'] %></td>
-<td align="right">$<%#$this->Parent->DataItem['price'] %></td>
+ <th>ID</th><th>Name</th><th>Quantity</th><th>Price</th>
+</tr>
+<tr>
+ <td align="right"><%#$this->Parent->DataItem['id'] %></td>
+ <td align="right"><%#$this->Parent->DataItem['name'] %></td>
+ <td align="right"><%#$this->Parent->DataItem['quantity'] %></td>
+ <td align="right">$<%#$this->Parent->DataItem['price'] %></td>
+</tr>
+<tr>
+ <td colspan="4" align="right">
+ <com:TLinkButton
+ Text="Edit"
+ CommandName="edit"
+ />
+ <com:TLinkButton
+ Text="Delete"
+ CommandName="delete"
+ Attributes.onclick="if(!confirm('Are you sure?')) return false;"
+ />
+ </td>
</tr>
-<tr><td colspan="4" align="right">
-<com:TLinkButton
- Text="Edit"
- CommandName="edit"
- />
-<com:TLinkButton
- Text="Delete"
- CommandName="delete"
- Attributes.onclick="if(!confirm('Are you sure?')) return false;"
- />
-</td></tr>
</table>
</prop:SelectedItemTemplate>
@@ -76,7 +81,7 @@
<table border="0" width="100%">
<tr>
<td align="right">ID</td>
- <td><%#$this->Parent->DataItem['id']%></td>
+ <td><b><%#$this->Parent->DataItem['id']%></b></td>
</tr>
<tr>
<td align="right">Name</td>
@@ -103,6 +108,14 @@
</td>
</tr>
<tr>
+ <td align="right">Imported</td>
+ <td><com:TCheckBox
+ ID="ProductImported"
+ Checked=<%#$this->Parent->DataItem['imported'] %>
+ />
+ </td>
+</tr>
+<tr>
<td colspan="2" align="right">
<com:TLinkButton Text="Save" CommandName="update" />
<com:TLinkButton Text="Cancel" CommandName="cancel" />
diff --git a/demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.php b/demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.php
index ccc87498..cc15e27e 100644
--- a/demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.php
+++ b/demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.php
@@ -2,33 +2,77 @@
class Sample2 extends TPage
{
+ private $_data=null;
+
protected function getData()
{
+ if($this->_data===null)
+ $this->loadData();
+ return $this->_data;
+ }
+
+ protected function loadData()
+ {
// We use viewstate keep track of data.
- // In real applications, data should come from database.
- if(($data=$this->getViewState('Data',null))===null)
+ // In real applications, data should come from database using an SQL SELECT statement.
+ // In the following tabular data, field 'id' is the primary key.
+ // All update and delete operations should come with an 'id' value in order to go through.
+ if(($this->_data=$this->getViewState('Data',null))===null)
{
- $data=array(
- array('id'=>'ITN001','name'=>'Motherboard','quantity'=>1,'price'=>100.00),
- array('id'=>'ITN002','name'=>'CPU','quantity'=>1,'price'=>150.00),
- array('id'=>'ITN003','name'=>'Harddrive','quantity'=>2,'price'=>80.00),
- array('id'=>'ITN004','name'=>'Sound card','quantity'=>1,'price'=>40.00),
- array('id'=>'ITN005','name'=>'Video card','quantity'=>1,'price'=>150.00),
- array('id'=>'ITN006','name'=>'Keyboard','quantity'=>1,'price'=>20.00),
- array('id'=>'ITN007','name'=>'Monitor','quantity'=>2,'price'=>300.00),
+ $this->_data=array(
+ array('id'=>'ITN001','name'=>'Motherboard','quantity'=>1,'price'=>100.00,'imported'=>true),
+ array('id'=>'ITN002','name'=>'CPU','quantity'=>1,'price'=>150.00,'imported'=>true),
+ array('id'=>'ITN003','name'=>'Harddrive','quantity'=>2,'price'=>80.00,'imported'=>false),
+ array('id'=>'ITN004','name'=>'Sound card','quantity'=>1,'price'=>40.00,'imported'=>false),
+ array('id'=>'ITN005','name'=>'Video card','quantity'=>1,'price'=>150.00,'imported'=>true),
+ array('id'=>'ITN006','name'=>'Keyboard','quantity'=>1,'price'=>20.00,'imported'=>true),
+ array('id'=>'ITN007','name'=>'Monitor','quantity'=>2,'price'=>300.00,'imported'=>false),
);
- $this->saveData($data);
+ $this->saveData();
+ }
+ }
+
+ protected function saveData()
+ {
+ $this->setViewState('Data',$this->_data);
+ }
+
+ protected function updateProduct($id,$name,$quantity,$price,$imported)
+ {
+ // In real applications, data should be saved to database using an SQL UPDATE statement
+ if($this->_data===null)
+ $this->loadData();
+ $updateRow=null;
+ foreach($this->_data as $index=>$row)
+ if($row['id']===$id)
+ $updateRow=&$this->_data[$index];
+ if($updateRow!==null)
+ {
+ $updateRow['name']=$name;
+ $updateRow['quantity']=TPropertyValue::ensureInteger($quantity);
+ $updateRow['price']=TPropertyValue::ensureFloat($price);
+ $updateRow['imported']=TPropertyValue::ensureBoolean($imported);
+ $this->saveData();
}
- return $data;
}
- protected function saveData($data)
+ protected function deleteProduct($id)
{
- // In real applications, data should be saved to database.
- $this->setViewState('Data',$data);
+ // In real applications, data should be saved to database using an SQL DELETE statement
+ if($this->_data===null)
+ $this->loadData();
+ $deleteIndex=-1;
+ foreach($this->_data as $index=>$row)
+ if($row['id']===$id)
+ $deleteIndex=$index;
+ if($deleteIndex>=0)
+ {
+ unset($this->_data[$deleteIndex]);
+ $this->saveData();
+ }
}
- function onLoad($param)
+ public function onLoad($param)
{
parent::onLoad($param);
if(!$this->IsPostBack)
@@ -38,14 +82,15 @@ class Sample2 extends TPage
}
}
- function editItem($sender,$param)
+ public function editItem($sender,$param)
{
+ $this->DataList->SelectedItemIndex=-1;
$this->DataList->EditItemIndex=$param->Item->ItemIndex;
$this->DataList->DataSource=$this->Data;
$this->DataList->dataBind();
}
- function cancelItem($sender,$param)
+ public function cancelItem($sender,$param)
{
$this->DataList->SelectedItemIndex=-1;
$this->DataList->EditItemIndex=-1;
@@ -53,33 +98,32 @@ class Sample2 extends TPage
$this->DataList->dataBind();
}
- function updateItem($sender,$param)
+ public function updateItem($sender,$param)
{
$item=$param->Item;
- $data=$this->Data;
- $product=&$data[$item->ItemIndex];
- $product['name']=$item->ProductName->Text;
- $product['price']=TPropertyValue::ensureFloat($item->ProductPrice->Text);
- $product['quantity']=TPropertyValue::ensureInteger($item->ProductQuantity->Text);
- $this->saveData($data);
+ $this->updateProduct(
+ $this->DataList->DataKeys[$item->ItemIndex],
+ $item->ProductName->Text,
+ $item->ProductPrice->Text,
+ $item->ProductQuantity->Text,
+ $item->ProductImported->Checked);
$this->DataList->EditItemIndex=-1;
- $this->DataList->DataSource=$data;
+ $this->DataList->DataSource=$this->Data;
$this->DataList->dataBind();
}
- function deleteItem($sender,$param)
+ public function deleteItem($sender,$param)
{
- $data=$this->Data;
- array_splice($data,$param->Item->ItemIndex,1);
- $this->saveData($data);
+ $this->deleteProduct($this->DataList->DataKeys[$param->Item->ItemIndex]);
$this->DataList->SelectedItemIndex=-1;
$this->DataList->EditItemIndex=-1;
- $this->DataList->DataSource=$data;
+ $this->DataList->DataSource=$this->Data;
$this->DataList->dataBind();
}
- function selectItem($sender,$param)
+ public function selectItem($sender,$param)
{
+ $this->DataList->EditItemIndex=-1;
$this->DataList->DataSource=$this->Data;
$this->DataList->dataBind();
}
diff --git a/framework/Web/UI/WebControls/TDataList.php b/framework/Web/UI/WebControls/TDataList.php
index 95919372..f5292220 100644
--- a/framework/Web/UI/WebControls/TDataList.php
+++ b/framework/Web/UI/WebControls/TDataList.php
@@ -447,7 +447,7 @@ class TDataList extends TBaseDataList implements INamingContainer, IRepeatInfoUs
{
if($this->getDataKeyField()==='')
throw new TInvalidOperationException('datalist_datakeyfield_required');
- $index=$this->getSelectedIndex();
+ $index=$this->getSelectedItemIndex();
$dataKeys=$this->getDataKeys();
if($index>=0 && $index<$dataKeys->getCount())
return $dataKeys->itemAt($index);