Delete Flash Message When Cgridview Filtered

I don’t know why, but flash message works when deleting from cgridview, from view, but doesn’t work when deleting from cgridview that was filtered…

Any idea why flash messages don’t work in this way?

Edited: Works only when ‘ajaxUpdate’ for cgridview is set to false, but I don’t like, cause it needs to reload page every filter…

You should implement an afterAjaxUpdate function that extract the message from the data and displays the flash messages.

Thank you, but can you tell me how to implement a function that extract the message from the data and displays the flash messages, please?

Hi make sure in your file add a flash message to display the flash message


<?php

    foreach(Yii::app()->user->getFlashes() as $key => $message) {

        echo '<div class="flash-' . $key . '">' . $message . "</div>\n";

    }

?>

Thanks for your reply.

In view (admin.php) I have:


<div id="statusMsg">

    <?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>

and it works when data is NOT FILTERED. In other case it doesn’t…

I want to use it with cgridview ajaxUpdate possibility.

Hi first you can create the afterAjaxUpdate on admin gridview code


	'afterAjaxUpdate'=>'function(){

		fancy();

	}',

in the bottom of page create the fancy function usig Jquery like




<script type="text/javascript">

	function fancy(){

		//code here..

	}

</script>

I hope it’s help :rolleyes:

Ok, but how should the code of fancy function look like?

In afterDelete I have


'afterDelete' => 'function(link,success,data){ if(success) $("#statusMsg").html(data); }',

but it doesn’t work in afterAjaxUpdate. Any ideas?

EDIT:

I’ve tried with this code:


    'afterAjaxUpdate'=>'function(id, data){ $("#statusMsg").html(data);   }',

and it’s also not working - loads to statusMsg div whole page…

Generaly my CGridView code in view admin.php looks now like:


$this->widget('zii.widgets.grid.CGridView', array(

    'id' => 'users-grid',

    //'ajaxUpdate' => false,

    'dataProvider' => $model->search(),

    'filter' => $model,

    'afterAjaxUpdate'=>'function(id, data){ $("#statusMsg").html(data);   }',

    'columns' => array(

        'login',

        array(

            'name' => 'rola',

            'filter' => $this->getRoles(),

        ),

        /* array(

          'class' => 'CButtonColumn',

          'header' => 'Akcja',

          'afterDelete' => 'function(link,success,data){ if(success) $("#statusMsg").html(data); }',

          ), */

        array(

            'class' => 'EButtonColumnWithClearFilters',

            'header' => 'Akcja',

            'afterDelete' => 'function(link,success,data){ if(success) $("#statusMsg").html(data); }',

            //'clearVisible'=>true,

            //'onClick_BeforeClear'=>'alert('this js fragment executes before clear');',

            //'onClick_AfterClear'=>'alert('this js fragment executes after clear');',

            //'clearHtmlOptions'=>array('class'=>'custom-clear'),

            //'imageUrl'=>'/path/to/custom/image/delete.png',

            //'url'=>'Yii::app()->controller->createUrl(Yii::app()->controller->action->ID,array("clearFilters"=>1))',

            'label' => 'Wyczyść wszystkie filtry',

        ),

    ),

));

and action delete in controller:


    public function actionDelete($id) {

        try {

            $this->loadModel($id)->delete();

            if (!isset($_GET['ajax']))

                Yii::app()->user->setFlash('success', 'Item deleted.');

            else

                echo "<div class='flash-success'>Item deleted.</div>";

        } catch (CDbException $e) {

            if (!isset($_GET['ajax']))

                Yii::app()->user->setFlash('error', 'Item cannot be deleted.');

            else

                echo "<div class='flash-error'>Item cannot be deleted.</div>";

        }

        if (!isset($_GET['ajax']))

            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

    }

And of course div code:


<div id="statusMsg">

    <?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>

Thank you in advance for any help to solve my problem.

I see the problem is after more then two ajax actions:

When I don’t try to delete to filter and I fill in search criteria, then try to delete - the message is shown. When I change criteria, it doesn’t work again.

I don’t know where is the problem, but if there is any other way to show messages about deleting status and if they work in many ways, I will be thankfully.

Maybe after many filtering (ajax gridview preloads), it can’t access to div?

try this


<script type="text/javascript">

        function fancy(data){

               $("#statusMsg").html(data);

        }

</script>


'afterAjaxUpdate'=>'function(data){

                fancy(data);

        }',

Thank you su much for your help, but…

there’s still no change :(

btw. your code displays CGridView’s id in flash message.

why you can not use ajax on delete button?

please see wiki

I hope it’s some help.