1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
<?php
Prado::using('System.Web.UI.ActiveControls.*');
class Issue516 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'=>'',
),
array(
'ISBN'=>'0201633612',
'title'=>'Design Patterns: Elements of Reusable Object-Oriented Software',
),
array(
'ISBN'=>'0321247140',
'title'=>'Design Patterns Explained : A New Perspective on Object-Oriented Design',
),
array(
'ISBN'=>'0201485672',
'title'=>'Refactoring: Improving the Design of Existing Code',
),
array(
'ISBN'=>'0321213351',
'title'=>'Refactoring to Patterns',
),
array(
'ISBN'=>'0735619670',
'title'=>'Code Complete',
),
array(
'ISBN'=>'0321278658 ',
'title'=>'Extreme Programming Explained : Embrace Change',
),
);
$this->saveData();
}
}
protected function saveData()
{
$this->setViewState('Data',$this->_data);
}
protected function updateBook($isbn,$title)
{
// 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;
$this->saveData();
}
}
public function onLoad($param)
{
parent::onLoad($param);
if(!$this->IsPostBack && !$this->IsCallBack)
{
$this->DataGrid->DataSource=$this->Data;
$this->DataGrid->dataBind();
}
}
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->BookTitleColumn->TextBox->Text // title
);
$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();
}
}
|