diff options
author | xue <> | 2006-02-04 23:32:21 +0000 |
---|---|---|
committer | xue <> | 2006-02-04 23:32:21 +0000 |
commit | 97d5ad831a6003418562b7f44e9a08e562d88a0c (patch) | |
tree | 619067b19bab202fb174c904f5fa287886bd186d | |
parent | e7622f08814c6c7a1166addf9bc23c5780bd74b0 (diff) |
Added an example of TDataGrid showing updating and deleting functionalities.
5 files changed, 264 insertions, 5 deletions
diff --git a/.gitattributes b/.gitattributes index 460c053c..539b3b89 100644 --- a/.gitattributes +++ b/.gitattributes @@ -124,6 +124,8 @@ demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample1.page -text demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample1.php -text demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample2.page -text demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample2.php -text +demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample3.page -text +demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample3.php -text demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample1.page -text demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample1.php -text demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.page -text diff --git a/demos/quickstart/protected/pages/Controls/DataGrid2.page b/demos/quickstart/protected/pages/Controls/DataGrid2.page index a40d3987..8039a738 100644 --- a/demos/quickstart/protected/pages/Controls/DataGrid2.page +++ b/demos/quickstart/protected/pages/Controls/DataGrid2.page @@ -4,9 +4,11 @@ <h2>Interacting with TDataGrid</h2>
<p>
-Besides the rich data presentation functionalities as demonstrated in previous section, TDataGrid is also highly user interactive. An import usage of TDataGrid is to use it for editting or deleting rows of data. The <tt>TEditCommandColumn</tt> is specically designed for this purpose.
+Besides the rich data presentation functionalities as demonstrated in previous section, TDataGrid is also highly user interactive. An import usage of TDataGrid is editting or deleting rows of data. The <tt>TBoundColumn</tt> can adjust the associated cell presentation according to the mode of datagrid items. When an item is in browsing mode, the cell is displayed with a static text; when the item is in editting mode, a textbox is displayed to collect user inputs. TDataGrid provides <tt>TEditCommandColumn</tt> for switching item modes. In addition, <tt>TButtonColumn</tt> offers developers the flexibility of creating arbitrary buttons for various user interactions.
+</p>
+<p>
+The following example shows how to make the previous book information table an interactive one. It allows users to edit and delete book items from the table. Two additional columns are used in the example to allow users interact with the datagrid: <tt>TEditCommandColumn</tt> and <tt>TButtonColumn</tt>.
</p>
-
<com:RunBar PagePath="Controls.Samples.TDataGrid.Sample3" />
<h2>Sorting</h2>
diff --git a/demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample3.page b/demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample3.page new file mode 100644 index 00000000..82253c30 --- /dev/null +++ b/demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample3.page @@ -0,0 +1,66 @@ +<com:TContent ID="body">
+
+<h1>TDataGrid Sample 3</h1>
+<h2>Interacting with TDataGrid</h2>
+
+<com:TDataGrid
+ Width="800px"
+ CellPadding="2"
+ ID="DataGrid"
+ DataKeyField="ISBN"
+ AutoGenerateColumns="false"
+ HeaderStyle.BackColor="black"
+ HeaderStyle.ForeColor="white"
+ ItemStyle.BackColor="#BFCFFF"
+ ItemStyle.Font.Italic="true"
+ AlternatingItemStyle.BackColor="#E6ECFF"
+ EditItemStyle.BackColor="lightyellow"
+ OnItemCreated="itemCreated"
+ OnEditCommand="editItem"
+ OnUpdateCommand="saveItem"
+ OnCancelCommand="cancelItem"
+ OnDeleteCommand="deleteItem"
+ >
+
+ <com:TBoundColumn
+ HeaderText="Book Title"
+ HeaderStyle.Width="400px"
+ DataField="title"
+ />
+ <com:TBoundColumn
+ HeaderText="Publisher"
+ HeaderStyle.Width="150px"
+ DataField="publisher"
+ />
+ <com:TBoundColumn
+ ItemStyle.HorizontalAlign="Right"
+ ItemStyle.Wrap="false"
+ ItemStyle.Font.Italic="false"
+ ItemStyle.ForeColor="green"
+ HeaderText="Price"
+ HeaderStyle.Width="70px"
+ DataField="price"
+ DataFormatString="$%.2f"
+ />
+ <com:TCheckBoxColumn
+ HeaderText="In-stock"
+ DataField="instock"
+ />
+ <com:TEditCommandColumn
+ HeaderText="Edit"
+ HeaderStyle.Width="100px"
+ UpdateText="Save"
+ ItemStyle.HorizontalAlign="Center"
+ ItemStyle.Font.Italic="false"
+ />
+ <com:TButtonColumn
+ HeaderText="Delete"
+ HeaderStyle.Width="50px"
+ ItemStyle.HorizontalAlign="Center"
+ ItemStyle.Font.Italic="false"
+ Text="Delete"
+ CommandName="delete"
+ />
+</com:TDataGrid>
+
+</com:TContent>
\ No newline at end of file diff --git a/demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample3.php b/demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample3.php new file mode 100644 index 00000000..89adcc37 --- /dev/null +++ b/demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample3.php @@ -0,0 +1,188 @@ +<?php
+
+class Sample3 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 using an SQL SELECT statement.
+ // In the following tabular data, field 'ISBN' 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)
+ {
+ $this->_data=array(
+ array(
+ 'ISBN'=>'0596007124',
+ 'title'=>'Head First Design Patterns',
+ 'publisher'=>'O\'Reilly Media, Inc.',
+ 'price'=>29.67,
+ 'instock'=>true,
+ 'rating'=>4,
+ ),
+ array(
+ 'ISBN'=>'0201633612',
+ 'title'=>'Design Patterns: Elements of Reusable Object-Oriented Software',
+ 'publisher'=>'Addison-Wesley Professional',
+ 'price'=>47.04,
+ 'instock'=>true,
+ 'rating'=>5,
+ ),
+ array(
+ 'ISBN'=>'0321247140',
+ 'title'=>'Design Patterns Explained : A New Perspective on Object-Oriented Design',
+ 'publisher'=>'Addison-Wesley Professional',
+ 'price'=>37.49,
+ 'instock'=>true,
+ 'rating'=>4,
+ ),
+ array(
+ 'ISBN'=>'0201485672',
+ 'title'=>'Refactoring: Improving the Design of Existing Code',
+ 'publisher'=>'Addison-Wesley Professional',
+ 'price'=>47.14,
+ 'instock'=>true,
+ 'rating'=>3,
+ ),
+ array(
+ 'ISBN'=>'0321213351',
+ 'title'=>'Refactoring to Patterns',
+ 'publisher'=>'Addison-Wesley Professional',
+ 'price'=>38.49,
+ 'instock'=>true,
+ 'rating'=>2,
+ ),
+ array(
+ 'ISBN'=>'0735619670',
+ 'title'=>'Code Complete',
+ 'publisher'=>'Microsoft Press',
+ 'price'=>32.99,
+ 'instock'=>false,
+ 'rating'=>4,
+ ),
+ array(
+ 'ISBN'=>'0321278658 ',
+ 'title'=>'Extreme Programming Explained : Embrace Change',
+ 'publisher'=>'Addison-Wesley Professional',
+ 'price'=>34.99,
+ 'instock'=>true,
+ 'rating'=>3,
+ ),
+ );
+ $this->saveData();
+ }
+ }
+
+ protected function saveData()
+ {
+ $this->setViewState('Data',$this->_data);
+ }
+
+ protected function updateBook($isbn,$title,$publisher,$price,$instock)
+ {
+ // 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['ISBN']===$isbn)
+ $updateRow=&$this->_data[$index];
+ if($updateRow!==null)
+ {
+ $updateRow['title']=$title;
+ $updateRow['publisher']=$publisher;
+ $updateRow['price']=TPropertyValue::ensureFloat(ltrim($price,'$'));
+ $updateRow['instock']=TPropertyValue::ensureBoolean($instock);
+ $this->saveData();
+ }
+ }
+
+ protected function deleteBook($isbn)
+ {
+ // 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['ISBN']===$isbn)
+ $deleteIndex=$index;
+ if($deleteIndex>=0)
+ {
+ unset($this->_data[$deleteIndex]);
+ $this->saveData();
+ }
+ }
+
+ public function onLoad($param)
+ {
+ parent::onLoad($param);
+ if(!$this->IsPostBack)
+ {
+ $this->DataGrid->DataSource=$this->Data;
+ $this->DataGrid->dataBind();
+ }
+ }
+
+ public function itemCreated($sender,$param)
+ {
+ $item=$param->Item;
+ if($item->ItemType==='EditItem')
+ {
+ // set column width of textboxes
+ $item->Cells[0]->Controls[0]->Columns=40;
+ $item->Cells[2]->Controls[0]->Columns=5;
+ }
+ if($item->ItemType==='Item' || $item->ItemType==='AlternatingItem' || $item->ItemType==='EditItem')
+ {
+ // add an aleart dialog to delete buttons
+ $item->Cells[5]->Controls[0]->Attributes->onclick='if(!confirm(\'Are you sure?\')) return false;';
+ }
+ }
+
+ public function editItem($sender,$param)
+ {
+ $this->DataGrid->EditItemIndex=$param->Item->ItemIndex;
+ $this->DataGrid->DataSource=$this->Data;
+ $this->DataGrid->dataBind();
+ }
+
+ public function saveItem($sender,$param)
+ {
+ $item=$param->Item;
+ $this->updateBook(
+ $this->DataGrid->DataKeys[$item->ItemIndex], // ISBN
+ $item->Cells[0]->Controls[0]->Text, // title
+ $item->Cells[1]->Controls[0]->Text, // publisher
+ $item->Cells[2]->Controls[0]->Text, // price
+ $item->Cells[3]->Controls[0]->Checked // instock
+ );
+ $this->DataGrid->EditItemIndex=-1;
+ $this->DataGrid->DataSource=$this->Data;
+ $this->DataGrid->dataBind();
+ }
+
+ public function cancelItem($sender,$param)
+ {
+ $this->DataGrid->EditItemIndex=-1;
+ $this->DataGrid->DataSource=$this->Data;
+ $this->DataGrid->dataBind();
+ }
+
+ public function deleteItem($sender,$param)
+ {
+ $this->deleteBook($this->DataGrid->DataKeys[$param->Item->ItemIndex]);
+ $this->DataGrid->EditItemIndex=-1;
+ $this->DataGrid->DataSource=$this->Data;
+ $this->DataGrid->dataBind();
+ }
+}
+
+?>
\ No newline at end of file diff --git a/framework/Web/UI/WebControls/TDataGrid.php b/framework/Web/UI/WebControls/TDataGrid.php index ac22fb9c..eceb498b 100644 --- a/framework/Web/UI/WebControls/TDataGrid.php +++ b/framework/Web/UI/WebControls/TDataGrid.php @@ -106,7 +106,7 @@ Prado::using('System.Web.UI.WebControls.TTable'); * @package System.Web.UI.WebControls
* @since 3.0
*/
-class TDataGrid extends TBaseDataList
+class TDataGrid extends TBaseDataList implements INamingContainer
{
private $_columns=null;
private $_autoColumns=null;
@@ -820,7 +820,6 @@ class TDataGrid extends TBaseDataList $ds->setDataSource(new TDummyDataSource($itemCount));
else
$ds->setDataSource(new TDummyDataSource($this->getViewState('DataSourceCount',0)));
-
$columns=new TList($this->getColumns());
$columns->mergeWith($this->_autoColumns);
@@ -879,12 +878,14 @@ class TDataGrid extends TBaseDataList if($allowPaging && $ds->getCurrentPageIndex()>=$ds->getPageCount())
throw new TInvalidDataValueException('datagrid_currentpageindex_invalid');
// get all columns
- $columns=new TList($this->getColumns());
if($this->getAutoGenerateColumns())
{
+ $columns=new TList($this->getColumns());
$autoColumns=$this->createAutoColumns($ds);
$columns->mergeWith($autoColumns);
}
+ else
+ $columns=$this->getColumns();
$items=$this->getItems();
|