| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
class Sample3 extends TPage
|
|
|
4 |
{
|
|
|
5 |
private $_data=null;
|
|
|
6 |
|
|
|
7 |
protected function getData()
|
|
|
8 |
{
|
|
|
9 |
if($this->_data===null)
|
|
|
10 |
$this->loadData();
|
|
|
11 |
return $this->_data;
|
|
|
12 |
}
|
|
|
13 |
|
|
|
14 |
protected function loadData()
|
|
|
15 |
{
|
|
|
16 |
// We use viewstate keep track of data.
|
|
|
17 |
// In real applications, data should come from database using an SQL SELECT statement.
|
|
|
18 |
// In the following tabular data, field 'ISBN' is the primary key.
|
|
|
19 |
// All update and delete operations should come with an 'id' value in order to go through.
|
|
|
20 |
if(($this->_data=$this->getViewState('Data',null))===null)
|
|
|
21 |
{
|
|
|
22 |
$this->_data=array(
|
|
|
23 |
array(
|
|
|
24 |
'ISBN'=>'0596007124',
|
|
|
25 |
'title'=>'Head First Design Patterns',
|
|
|
26 |
'publisher'=>'O\'Reilly Media, Inc.',
|
|
|
27 |
'price'=>29.67,
|
|
|
28 |
'instock'=>true,
|
|
|
29 |
'rating'=>4,
|
|
|
30 |
),
|
|
|
31 |
array(
|
|
|
32 |
'ISBN'=>'0201633612',
|
|
|
33 |
'title'=>'Design Patterns: Elements of Reusable Object-Oriented Software',
|
|
|
34 |
'publisher'=>'Addison-Wesley Professional',
|
|
|
35 |
'price'=>47.04,
|
|
|
36 |
'instock'=>true,
|
|
|
37 |
'rating'=>5,
|
|
|
38 |
),
|
|
|
39 |
array(
|
|
|
40 |
'ISBN'=>'0321247140',
|
|
|
41 |
'title'=>'Design Patterns Explained : A New Perspective on Object-Oriented Design',
|
|
|
42 |
'publisher'=>'Addison-Wesley Professional',
|
|
|
43 |
'price'=>37.49,
|
|
|
44 |
'instock'=>true,
|
|
|
45 |
'rating'=>4,
|
|
|
46 |
),
|
|
|
47 |
array(
|
|
|
48 |
'ISBN'=>'0201485672',
|
|
|
49 |
'title'=>'Refactoring: Improving the Design of Existing Code',
|
|
|
50 |
'publisher'=>'Addison-Wesley Professional',
|
|
|
51 |
'price'=>47.14,
|
|
|
52 |
'instock'=>true,
|
|
|
53 |
'rating'=>3,
|
|
|
54 |
),
|
|
|
55 |
array(
|
|
|
56 |
'ISBN'=>'0321213351',
|
|
|
57 |
'title'=>'Refactoring to Patterns',
|
|
|
58 |
'publisher'=>'Addison-Wesley Professional',
|
|
|
59 |
'price'=>38.49,
|
|
|
60 |
'instock'=>true,
|
|
|
61 |
'rating'=>2,
|
|
|
62 |
),
|
|
|
63 |
array(
|
|
|
64 |
'ISBN'=>'0735619670',
|
|
|
65 |
'title'=>'Code Complete',
|
|
|
66 |
'publisher'=>'Microsoft Press',
|
|
|
67 |
'price'=>32.99,
|
|
|
68 |
'instock'=>false,
|
|
|
69 |
'rating'=>4,
|
|
|
70 |
),
|
|
|
71 |
array(
|
|
|
72 |
'ISBN'=>'0321278658 ',
|
|
|
73 |
'title'=>'Extreme Programming Explained : Embrace Change',
|
|
|
74 |
'publisher'=>'Addison-Wesley Professional',
|
|
|
75 |
'price'=>34.99,
|
|
|
76 |
'instock'=>true,
|
|
|
77 |
'rating'=>3,
|
|
|
78 |
),
|
|
|
79 |
);
|
|
|
80 |
$this->saveData();
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
protected function saveData()
|
|
|
85 |
{
|
|
|
86 |
$this->setViewState('Data',$this->_data);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
protected function updateBook($isbn,$title,$publisher,$price,$instock,$rating)
|
|
|
90 |
{
|
|
|
91 |
// In real applications, data should be saved to database using an SQL UPDATE statement
|
|
|
92 |
if($this->_data===null)
|
|
|
93 |
$this->loadData();
|
|
|
94 |
$updateRow=null;
|
|
|
95 |
foreach($this->_data as $index=>$row)
|
|
|
96 |
if($row['ISBN']===$isbn)
|
|
|
97 |
$updateRow=&$this->_data[$index];
|
|
|
98 |
if($updateRow!==null)
|
|
|
99 |
{
|
|
|
100 |
$updateRow['title']=$title;
|
|
|
101 |
$updateRow['publisher']=$publisher;
|
|
|
102 |
$updateRow['price']=TPropertyValue::ensureFloat(ltrim($price,'$'));
|
|
|
103 |
$updateRow['instock']=TPropertyValue::ensureBoolean($instock);
|
|
|
104 |
$updateRow['rating']=TPropertyValue::ensureInteger($rating);
|
|
|
105 |
$this->saveData();
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
protected function deleteBook($isbn)
|
|
|
110 |
{
|
|
|
111 |
// In real applications, data should be saved to database using an SQL DELETE statement
|
|
|
112 |
if($this->_data===null)
|
|
|
113 |
$this->loadData();
|
|
|
114 |
$deleteIndex=-1;
|
|
|
115 |
foreach($this->_data as $index=>$row)
|
|
|
116 |
if($row['ISBN']===$isbn)
|
|
|
117 |
$deleteIndex=$index;
|
|
|
118 |
if($deleteIndex>=0)
|
|
|
119 |
{
|
|
|
120 |
unset($this->_data[$deleteIndex]);
|
|
|
121 |
$this->saveData();
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
public function onLoad($param)
|
|
|
126 |
{
|
|
|
127 |
parent::onLoad($param);
|
|
|
128 |
if(!$this->IsPostBack)
|
|
|
129 |
{
|
|
|
130 |
$this->DataGrid->DataSource=$this->Data;
|
|
|
131 |
$this->DataGrid->dataBind();
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
public function itemCreated($sender,$param)
|
|
|
136 |
{
|
|
|
137 |
$item=$param->Item;
|
|
|
138 |
if($item->ItemType==='EditItem')
|
|
|
139 |
{
|
|
|
140 |
// set column width of textboxes
|
|
|
141 |
$item->BookTitleColumn->TextBox->Columns=40;
|
|
|
142 |
$item->PriceColumn->TextBox->Columns=5;
|
|
|
143 |
}
|
|
|
144 |
if($item->ItemType==='Item' || $item->ItemType==='AlternatingItem' || $item->ItemType==='EditItem')
|
|
|
145 |
{
|
|
|
146 |
// add an aleart dialog to delete buttons
|
|
|
147 |
$item->DeleteColumn->Button->Attributes->onclick='if(!confirm(\'Are you sure?\')) return false;';
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
public function editItem($sender,$param)
|
|
|
152 |
{
|
|
|
153 |
$this->DataGrid->EditItemIndex=$param->Item->ItemIndex;
|
|
|
154 |
$this->DataGrid->DataSource=$this->Data;
|
|
|
155 |
$this->DataGrid->dataBind();
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
public function saveItem($sender,$param)
|
|
|
159 |
{
|
|
|
160 |
$item=$param->Item;
|
|
|
161 |
$this->updateBook(
|
|
|
162 |
$this->DataGrid->DataKeys[$item->ItemIndex], // ISBN
|
|
|
163 |
$item->BookTitleColumn->TextBox->Text, // title
|
|
|
164 |
$item->PublisherColumn->TextBox->Text, // publisher
|
|
|
165 |
$item->PriceColumn->TextBox->Text, // price
|
|
|
166 |
$item->InStockColumn->CheckBox->Checked, // instock
|
|
|
167 |
$item->RatingColumn->DropDownList->SelectedValue // rating
|
|
|
168 |
);
|
|
|
169 |
$this->DataGrid->EditItemIndex=-1;
|
|
|
170 |
$this->DataGrid->DataSource=$this->Data;
|
|
|
171 |
$this->DataGrid->dataBind();
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
public function cancelItem($sender,$param)
|
|
|
175 |
{
|
|
|
176 |
$this->DataGrid->EditItemIndex=-1;
|
|
|
177 |
$this->DataGrid->DataSource=$this->Data;
|
|
|
178 |
$this->DataGrid->dataBind();
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
public function deleteItem($sender,$param)
|
|
|
182 |
{
|
|
|
183 |
$this->deleteBook($this->DataGrid->DataKeys[$param->Item->ItemIndex]);
|
|
|
184 |
$this->DataGrid->EditItemIndex=-1;
|
|
|
185 |
$this->DataGrid->DataSource=$this->Data;
|
|
|
186 |
$this->DataGrid->dataBind();
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
?>
|