From e7622f08814c6c7a1166addf9bc23c5780bd74b0 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sat, 4 Feb 2006 05:05:03 +0000 Subject: Fixed a bug about performing databind with an empty dataset. --- framework/Web/UI/WebControls/TDataBoundControl.php | 5 +- framework/Web/UI/WebControls/TDataGrid.php | 103 ++++++++++++--------- framework/Web/UI/WebControls/TDataList.php | 45 ++++----- .../Web/UI/WebControls/TEditCommandColumn.php | 4 +- framework/Web/UI/WebControls/TListControl.php | 2 + framework/Web/UI/WebControls/TRepeater.php | 27 +++--- 6 files changed, 101 insertions(+), 85 deletions(-) (limited to 'framework/Web') diff --git a/framework/Web/UI/WebControls/TDataBoundControl.php b/framework/Web/UI/WebControls/TDataBoundControl.php index 026d2e86..b7294383 100644 --- a/framework/Web/UI/WebControls/TDataBoundControl.php +++ b/framework/Web/UI/WebControls/TDataBoundControl.php @@ -196,10 +196,7 @@ abstract class TDataBoundControl extends TWebControl $this->dataBindProperties(); $this->onDataBinding(null); $data=$this->getData(); - if($data instanceof Traversable) - $this->performDataBinding($data); - else if($data!==null) - throw new TInvalidDataTypeException('databoundcontrol_data_nontraversable'); + $this->performDataBinding($data); $this->setIsDataBound(true); $this->onDataBound(null); } diff --git a/framework/Web/UI/WebControls/TDataGrid.php b/framework/Web/UI/WebControls/TDataGrid.php index f7f51e04..ac22fb9c 100644 --- a/framework/Web/UI/WebControls/TDataGrid.php +++ b/framework/Web/UI/WebControls/TDataGrid.php @@ -825,6 +825,7 @@ class TDataGrid extends TBaseDataList $columns->mergeWith($this->_autoColumns); $items=$this->getItems(); + $items->clear(); if($columns->getCount()>0) { @@ -869,64 +870,74 @@ class TDataGrid extends TBaseDataList $this->reset(); $keys=$this->getDataKeys(); $keys->clear(); - $keyField=$this->getDataKeyField(); - $this->_pagedDataSource=$ds=$this->createPagedDataSource(); - $ds->setDataSource($data); - $allowPaging=$ds->getAllowPaging(); - if($allowPaging && $ds->getCurrentPageIndex()>=$ds->getPageCount()) - throw new TInvalidDataValueException('datagrid_currentpageindex_invalid'); - // get all columns - $columns=new TList($this->getColumns()); - if($this->getAutoGenerateColumns()) + if($data instanceof Traversable) { - $autoColumns=$this->createAutoColumns($ds); - $columns->mergeWith($autoColumns); - } + $keyField=$this->getDataKeyField(); + $this->_pagedDataSource=$ds=$this->createPagedDataSource(); + $ds->setDataSource($data); + $allowPaging=$ds->getAllowPaging(); + if($allowPaging && $ds->getCurrentPageIndex()>=$ds->getPageCount()) + throw new TInvalidDataValueException('datagrid_currentpageindex_invalid'); + // get all columns + $columns=new TList($this->getColumns()); + if($this->getAutoGenerateColumns()) + { + $autoColumns=$this->createAutoColumns($ds); + $columns->mergeWith($autoColumns); + } - $items=$this->getItems(); + $items=$this->getItems(); - if(($columnCount=$columns->getCount())>0) - { - foreach($columns as $column) - $column->initialize(); - $allowPaging=$ds->getAllowPaging(); - if($allowPaging) - $this->createPager(-1,-1,$columnCount,$ds); - $this->createItemInternal(-1,-1,'Header',true,null,$columns); - $selectedIndex=$this->getSelectedItemIndex(); - $editIndex=$this->getEditItemIndex(); - $index=0; - $dsIndex=$ds->getAllowPaging()?$ds->getFirstIndexInPage():0; - foreach($ds as $data) + if(($columnCount=$columns->getCount())>0) { - if($keyField!=='') - $keys->add($this->getDataFieldValue($data,$keyField)); - if($index===$editIndex) - $itemType='EditItem'; - else if($index===$selectedIndex) - $itemType='SelectedItem'; - else if($index % 2) - $itemType='AlternatingItem'; - else - $itemType='Item'; - $items->add($this->createItemInternal($index,$dsIndex,$itemType,true,$data,$columns)); - $index++; - $dsIndex++; + foreach($columns as $column) + $column->initialize(); + $allowPaging=$ds->getAllowPaging(); + if($allowPaging) + $this->createPager(-1,-1,$columnCount,$ds); + $this->createItemInternal(-1,-1,'Header',true,null,$columns); + $selectedIndex=$this->getSelectedItemIndex(); + $editIndex=$this->getEditItemIndex(); + $index=0; + $dsIndex=$ds->getAllowPaging()?$ds->getFirstIndexInPage():0; + foreach($ds as $data) + { + if($keyField!=='') + $keys->add($this->getDataFieldValue($data,$keyField)); + if($index===$editIndex) + $itemType='EditItem'; + else if($index===$selectedIndex) + $itemType='SelectedItem'; + else if($index % 2) + $itemType='AlternatingItem'; + else + $itemType='Item'; + $items->add($this->createItemInternal($index,$dsIndex,$itemType,true,$data,$columns)); + $index++; + $dsIndex++; + } + $this->createItemInternal(-1,-1,'Footer',true,null,$columns); + if($allowPaging) + $this->createPager(-1,-1,$columnCount,$ds); + $this->setViewState('ItemCount',$index,0); + $this->setViewState('PageCount',$ds->getPageCount(),0); + $this->setViewState('DataSourceCount',$ds->getDataSourceCount(),0); } - $this->createItemInternal(-1,-1,'Footer',true,null,$columns); - if($allowPaging) - $this->createPager(-1,-1,$columnCount,$ds); - $this->setViewState('ItemCount',$index,0); - $this->setViewState('PageCount',$ds->getPageCount(),0); - $this->setViewState('DataSourceCount',$ds->getDataSourceCount(),0); + else + { + $this->clearViewState('ItemCount'); + $this->clearViewState('PageCount'); + $this->clearViewState('DataSourceCount'); + } + $this->_pagedDataSource=null; } else { + $this->_pagedDataSource=null; $this->clearViewState('ItemCount'); $this->clearViewState('PageCount'); $this->clearViewState('DataSourceCount'); } - $this->_pagedDataSource=null; } /** diff --git a/framework/Web/UI/WebControls/TDataList.php b/framework/Web/UI/WebControls/TDataList.php index 0bdeb4a4..825bb9db 100644 --- a/framework/Web/UI/WebControls/TDataList.php +++ b/framework/Web/UI/WebControls/TDataList.php @@ -1107,29 +1107,32 @@ class TDataList extends TBaseDataList implements INamingContainer, IRepeatInfoUs $keys->clear(); $keyField=$this->getDataKeyField(); $itemIndex=0; - $items=$this->getItems(); - $hasSeparator=$this->_separatorTemplate!==null; - $selectedIndex=$this->getSelectedItemIndex(); - $editIndex=$this->getEditItemIndex(); - foreach($data as $dataItem) + if($data instanceof Traversable) { - if($keyField!=='') - $keys->add($this->getDataFieldValue($dataItem,$keyField)); - if($itemIndex===0 && $this->_headerTemplate!==null) - $this->_header=$this->createItemInternal(-1,'Header',true,null); - if($hasSeparator && $itemIndex>0) - $this->createItemInternal($itemIndex-1,'Separator',true,null); - if($itemIndex===$editIndex) - $itemType='EditItem'; - else if($itemIndex===$selectedIndex) - $itemType='SelectedItem'; - else - $itemType=$itemIndex%2?'AlternatingItem':'Item'; - $items->add($this->createItemInternal($itemIndex,$itemType,true,$dataItem)); - $itemIndex++; + $items=$this->getItems(); + $hasSeparator=$this->_separatorTemplate!==null; + $selectedIndex=$this->getSelectedItemIndex(); + $editIndex=$this->getEditItemIndex(); + foreach($data as $dataItem) + { + if($keyField!=='') + $keys->add($this->getDataFieldValue($dataItem,$keyField)); + if($itemIndex===0 && $this->_headerTemplate!==null) + $this->_header=$this->createItemInternal(-1,'Header',true,null); + if($hasSeparator && $itemIndex>0) + $this->createItemInternal($itemIndex-1,'Separator',true,null); + if($itemIndex===$editIndex) + $itemType='EditItem'; + else if($itemIndex===$selectedIndex) + $itemType='SelectedItem'; + else + $itemType=$itemIndex%2?'AlternatingItem':'Item'; + $items->add($this->createItemInternal($itemIndex,$itemType,true,$dataItem)); + $itemIndex++; + } + if($itemIndex>0 && $this->_footerTemplate!==null) + $this->_footer=$this->createItemInternal(-1,'Footer',true,null); } - if($itemIndex>0 && $this->_footerTemplate!==null) - $this->_footer=$this->createItemInternal(-1,'Footer',true,null); $this->setViewState('ItemCount',$itemIndex,0); } diff --git a/framework/Web/UI/WebControls/TEditCommandColumn.php b/framework/Web/UI/WebControls/TEditCommandColumn.php index b0814a1a..8244981b 100644 --- a/framework/Web/UI/WebControls/TEditCommandColumn.php +++ b/framework/Web/UI/WebControls/TEditCommandColumn.php @@ -156,12 +156,12 @@ class TEditCommandColumn extends TDataGridColumn parent::initializeCell($cell,$columnIndex,$itemType); $buttonType=$this->getButtonType()=='LinkButton'?'TLinkButton':'TButton'; if($itemType==='Item' || $itemType==='AlternatingItem' || $itemType==='SelectedItem') - $this->addButtonToCell($cell,'Edit',$this->getUpdateText(),false,''); + $this->addButtonToCell($cell,'Edit',$this->getEditText(),false,''); else if($itemType==='EditItem') { $this->addButtonToCell($cell,'Update',$this->getUpdateText(),$this->getCausesValidation(),$this->getValidationGroup()); $cell->getControls()->add(' '); - $this->addButtonToCell($cell,'Cancel',$this->getUpdateText(),false,''); + $this->addButtonToCell($cell,'Cancel',$this->getCancelText(),false,''); } } diff --git a/framework/Web/UI/WebControls/TListControl.php b/framework/Web/UI/WebControls/TListControl.php index 169cc72c..00381890 100644 --- a/framework/Web/UI/WebControls/TListControl.php +++ b/framework/Web/UI/WebControls/TListControl.php @@ -151,6 +151,8 @@ abstract class TListControl extends TDataBoundControl $items=$this->getItems(); if(!$this->getAppendDataBoundItems()) $items->clear(); + if(!($data instanceof Traversable)) + return; $textField=$this->getDataTextField(); if($textField==='') $textField=0; diff --git a/framework/Web/UI/WebControls/TRepeater.php b/framework/Web/UI/WebControls/TRepeater.php index fc997323..65b3106e 100644 --- a/framework/Web/UI/WebControls/TRepeater.php +++ b/framework/Web/UI/WebControls/TRepeater.php @@ -372,21 +372,24 @@ class TRepeater extends TDataBoundControl implements INamingContainer protected function performDataBinding($data) { $this->reset(); - $itemIndex=0; $items=$this->getItems(); - $hasSeparator=$this->_separatorTemplate!==null; - foreach($data as $dataItem) + $itemIndex=0; + if($data instanceof Traversable) { - if($itemIndex===0 && $this->_headerTemplate!==null) - $this->_header=$this->createItemInternal(-1,'Header',true,null); - if($hasSeparator && $itemIndex>0) - $this->createItemInternal($itemIndex-1,'Separator',true,null); - $itemType=$itemIndex%2==0?'Item':'AlternatingItem'; - $items->add($this->createItemInternal($itemIndex,$itemType,true,$dataItem)); - $itemIndex++; + $hasSeparator=$this->_separatorTemplate!==null; + foreach($data as $dataItem) + { + if($itemIndex===0 && $this->_headerTemplate!==null) + $this->_header=$this->createItemInternal(-1,'Header',true,null); + if($hasSeparator && $itemIndex>0) + $this->createItemInternal($itemIndex-1,'Separator',true,null); + $itemType=$itemIndex%2==0?'Item':'AlternatingItem'; + $items->add($this->createItemInternal($itemIndex,$itemType,true,$dataItem)); + $itemIndex++; + } + if($itemIndex>0 && $this->_footerTemplate!==null) + $this->_footer=$this->createItemInternal(-1,'Footer',true,null); } - if($itemIndex>0 && $this->_footerTemplate!==null) - $this->_footer=$this->createItemInternal(-1,'Footer',true,null); $this->setViewState('ItemCount',$itemIndex,0); } -- cgit v1.2.3