From 97d5ad831a6003418562b7f44e9a08e562d88a0c Mon Sep 17 00:00:00 2001 From: xue <> Date: Sat, 4 Feb 2006 23:32:21 +0000 Subject: Added an example of TDataGrid showing updating and deleting functionalities. --- .../protected/pages/Controls/DataGrid2.page | 6 +- .../pages/Controls/Samples/TDataGrid/Sample3.page | 66 ++++++++ .../pages/Controls/Samples/TDataGrid/Sample3.php | 188 +++++++++++++++++++++ 3 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample3.page create mode 100644 demos/quickstart/protected/pages/Controls/Samples/TDataGrid/Sample3.php (limited to 'demos') 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 @@

Interacting with TDataGrid

-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 TEditCommandColumn 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 TBoundColumn 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 TEditCommandColumn for switching item modes. In addition, TButtonColumn offers developers the flexibility of creating arbitrary buttons for various user interactions. +

+

+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: TEditCommandColumn and TButtonColumn.

-

Sorting

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 @@ + + +

TDataGrid Sample 3

+

Interacting with TDataGrid

+ + + + + + + + + + + +
\ 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 @@ +_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 -- cgit v1.2.3