Hi LIAL,
CGridView's delete function works via ajax, so it is impossible to show a flash message ... at least in the normal way.
One easy solution is something like this:
public function actionDelete($id)
{
$result = $this->loadModel($id)->delete();
// for ajax request
if (Yii::app()->request->isAjaxRequest)
{
if ($result)
{
echo "Data has been deleted.";
Yii::app()->end();
}
else
{
throw new CHttpException(500, "\nFailed to delete the data.");
}
}
// for normal post request
else
{
if ($result)
{
Yii::app()->user->setFlash('success', 'Data has been deleted.');
}
else
{
Yii::app()->user->setFlash('error', 'Failed to delete the data.');
}
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array(index));
}
}
By throwing an exception, we can notify the ajax caller about the failure. It will pop up an alert box to show the error.
The drawback of this solution is that you'll get no message when the deletion has been successfully completed (although the user will know the deletion was successful because the grid will be updated

).
If you do want to show an alert box even when the deletion was successful, you can make use of the 'afterDelete' property of CButtonColumn.
// in the controller
...
if (Yii::app()->request->isAjaxRequest)
{
if ($result)
{
echo "Data has been deleted.";
}
else
{
echo "Failed to delete the data.";
}
Yii::app()->end();
}
...
// in the view script
...
array(
'class' => 'CButtonColumn',
'afterDelete' => "function(link,success,data){ if (success) alert(data); } ",
),
...
Check the reference manual for CButtonColumn::afterDelete.
http://www.yiiframew...erDelete-detail