[ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 3 class Sample2 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 'id' 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('id'=>'ITN001','name'=>'Motherboard','quantity'=>1,'price'=>100.00,'imported'=>true), 24 array('id'=>'ITN002','name'=>'CPU','quantity'=>1,'price'=>150.00,'imported'=>true), 25 array('id'=>'ITN003','name'=>'Harddrive','quantity'=>2,'price'=>80.00,'imported'=>false), 26 array('id'=>'ITN004','name'=>'Sound card','quantity'=>1,'price'=>40.00,'imported'=>false), 27 array('id'=>'ITN005','name'=>'Video card','quantity'=>1,'price'=>150.00,'imported'=>true), 28 array('id'=>'ITN006','name'=>'Keyboard','quantity'=>1,'price'=>20.00,'imported'=>true), 29 array('id'=>'ITN007','name'=>'Monitor','quantity'=>2,'price'=>300.00,'imported'=>false), 30 ); 31 $this->saveData(); 32 } 33 } 34 35 protected function saveData() 36 { 37 $this->setViewState('Data',$this->_data); 38 } 39 40 protected function updateProduct($id,$name,$quantity,$price,$imported) 41 { 42 // In real applications, data should be saved to database using an SQL UPDATE statement 43 if($this->_data===null) 44 $this->loadData(); 45 $updateRow=null; 46 foreach($this->_data as $index=>$row) 47 if($row['id']===$id) 48 $updateRow=&$this->_data[$index]; 49 if($updateRow!==null) 50 { 51 $updateRow['name']=$name; 52 $updateRow['quantity']=TPropertyValue::ensureInteger($quantity); 53 $updateRow['price']=TPropertyValue::ensureFloat($price); 54 $updateRow['imported']=TPropertyValue::ensureBoolean($imported); 55 $this->saveData(); 56 } 57 } 58 59 protected function deleteProduct($id) 60 { 61 // In real applications, data should be saved to database using an SQL DELETE statement 62 if($this->_data===null) 63 $this->loadData(); 64 $deleteIndex=-1; 65 foreach($this->_data as $index=>$row) 66 if($row['id']===$id) 67 $deleteIndex=$index; 68 if($deleteIndex>=0) 69 { 70 unset($this->_data[$deleteIndex]); 71 $this->saveData(); 72 } 73 } 74 75 public function onLoad($param) 76 { 77 parent::onLoad($param); 78 if(!$this->IsPostBack) 79 { 80 $this->DataList->DataSource=$this->Data; 81 $this->DataList->dataBind(); 82 } 83 } 84 85 public function editItem($sender,$param) 86 { 87 $this->DataList->SelectedItemIndex=-1; 88 $this->DataList->EditItemIndex=$param->Item->ItemIndex; 89 $this->DataList->DataSource=$this->Data; 90 $this->DataList->dataBind(); 91 } 92 93 public function cancelItem($sender,$param) 94 { 95 $this->DataList->SelectedItemIndex=-1; 96 $this->DataList->EditItemIndex=-1; 97 $this->DataList->DataSource=$this->Data; 98 $this->DataList->dataBind(); 99 } 100 101 public function updateItem($sender,$param) 102 { 103 $item=$param->Item; 104 $this->updateProduct( 105 $this->DataList->DataKeys[$item->ItemIndex], 106 $item->ProductName->Text, 107 $item->ProductQuantity->Text, 108 $item->ProductPrice->Text, 109 $item->ProductImported->Checked); 110 $this->DataList->EditItemIndex=-1; 111 $this->DataList->DataSource=$this->Data; 112 $this->DataList->dataBind(); 113 } 114 115 public function deleteItem($sender,$param) 116 { 117 $this->deleteProduct($this->DataList->DataKeys[$param->Item->ItemIndex]); 118 $this->DataList->SelectedItemIndex=-1; 119 $this->DataList->EditItemIndex=-1; 120 $this->DataList->DataSource=$this->Data; 121 $this->DataList->dataBind(); 122 } 123 124 public function selectItem($sender,$param) 125 { 126 $this->DataList->EditItemIndex=-1; 127 $this->DataList->DataSource=$this->Data; 128 $this->DataList->dataBind(); 129 } 130 } 131 132 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |