I expect that the problem you’re having with using the default Gii CRUD actionDelete is that when your action succeeds your AJAX call is sent a redirect when what you actually need is the content of the message… Well, I’ve never seen any AJAX system to handle a redirect the same way a browser might, loading the URL the user is redirected to, and jQuery’s implementation is no exception to this.
My suggestion would be to, as redirects are useless on an AJAX call anyway, modify your actionDelete as follows:
if(Yii::app()->request->getIsAjaxRequest()) {
echo Yii::app()->user->getFlash(...);
} else {
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array(...));
}
At-least this way your user’s browser is sent the message. You’ll need to make a couple more changes to ensure it’s shown, however.
Firstly, in the code that performs your AJAX call, run the following function:
gotFlash(message)
{
$('#Flash').html(message).fadeIn().animate({opacity: 1.0}, 3000).fadeOut("slow");
}
Now, this sets the content of the element with ID ‘success’, and animates it in and out, but it doesn’t do anything unless there’s an element with that ID on the page already… It’s notable that the code in the cookbook only places its element with class ‘info’ if there was a flash message when the page was loaded. I’d thus suggest altering their code as follows.
<?php $HasFlash = Yii::app()->user->hasFlash('Flash'); ?>
<div class="info" id='Flash' <?php if(!$HasFlash) echo "style='display: none'"?>>
<?php if($HasFlash) echo Yii::app()->user->getFlash('Flash'); ?>
</div>
Now you should see your flash appearing as you want it… Note that I switched to using an ID rather than a class. Otherwise, if you had several info boxes, you’d end-up setting the contents of all of them with the $(’.info’).html() code. IDs should be unique.
Another, more complex, way to do it would be to check if the desired div existed and to use jQuery’s .append() function to create the div if it weren’t there… I think the above works fine, however.