Difference between #2 and #5 of
How to show ajax delete status in CGridView like flash messages

Changes

Title unchanged

How to show ajax delete status in CGridView like flash messages

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

CGridView, Ajax flash message, ajax request, Delete Status

Content changed

[...]
when you click the delete button at first it performs the delete operation in the controller then it refreshes the grid using another request. 'afterAjaxUpdate' fires just after refreshing so you can't see any message just after the first delete operation.

Now I'll show you how you can achieve this functionality.

The main tricks l
ieays under CButtonColumn properties. There is a property called afterDelete which will fire a function just after the deleting operation. We will use ajax polling request to display the deletion status. So, In the controller file just echo the message with your flash message css display style and for non ajax request set the Flash message using setFlash().
[...]
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
}
[...]
```

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.
17 0
30 followers
Viewed: 56 075 times
Version: 1.1
Category: How-tos
Written by: hasanavi
Last updated by: adlersd
Created on: Jun 8, 2011
Last updated: 11 years ago
Update Article

Revisions

View all history