Comparing
#3 with
#4
Revision #4 was created by Artur Oliveira on Jun 9, 2011, 6:43:31 PM.
Added getFlash in view, Added some verifications in controller to check if ajax request
« Previous (#3) Next (#5) »
Content
[...]
```php
try{
$this->loadModel($id)->delete();
if(!isset($_GET['ajax']))
Yii::app()->user->setFlash('
deleteStatus','success','Normal - Deleted Successfully');
else
echo "<div class='flash-success'>
Ajax - Deleted Successfully</div>";
//for ajax
}catch(CDbException $e){
if(!isset($_GET['ajax']))
Yii::app()->user->setFlash('
deleteStatus','error','Normal - error message');
else
echo "<div class='flash-error'>
Ajax - error message</div>"; //for ajax
}
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser [...]
```
And in your admin (or other where you have the {delete} in GridView) view, show the echoed message in the statusMsg place holder using data variable.
```php
<
spandiv id=
'" statusMsg
'></span">
<?php if(Yii::app()->user->hasFlash('success')):?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('success'); ?>
</div>
<?php endif; ?>
<?php if(Yii::app()->user->hasFlash('error')):?>
<div class="flash-error">
<?php echo Yii::app()->user->getFlash('error'); ?>
</div>
<?php endif; ?>
</div >
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'region-grid', [...]
```
This way everything works even if you disable ajax requests in GridView using:
```php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'region-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'ajaxUpdate'=>false,
'columns'=>array(
...
```
Hope this will be helpful.