I delete a row in my CGridView using the default delete button. However, I have some checking on the delete button and the controller return echo "success" if deletion is successful and echo "failed" when the checking is failed.
How could I display the "success" or "failed" on the div I provided, <div id="info"></div>?
I see the ajaxUpdate parameter on CGridView can have ID not just true/false, or using afterAjaxUpdate. But, I do not know how to use it. Can someone show me some working example?
I also tried the "click" option on the button, but I cannot pass the ID of the data to my controller.
At the moment, I used a link on a column to replace the button "Delete".
FYI, my CGridView is only contained "Delete" button.
make sure you use renderPartial for the response. Also, I have not included any data filtering so to find out whether is a delete, or page, or edit command. I use JSON responses with objects and I check what action as been performed.
I have tried afterAjaxUpdate, but nothing happened. Probably I missed something here. What do you mean by "* make sure you use renderPartial for the response." Does it mean that I should put the CGridView and the div in the renderPartial? Or just the div?
About renderPartial, is that if you make an ajax call to a controller’s action and you just drop a ‘render’, it will flush full pages and not content. If you just use ‘echo’ or ‘renderPartial’ then no worries.
Try again what I said and do this to debug:
open your firebug console (or chrome console I do not know what you are using)
monitor if its making an AJAX call or not and also in your console if there is no error.
I found my self in the past through really silly problems when developing with AJAX, the proper tools should be used all the time. I truly like the script debugger of Chrome. Do that and show here what is happening.
Also, view your HTML response and paste here the automated code of CGridView to make use of afterAjaxUpdate and I will be able to track the error a bit better.
Apologise for long response. I was in business travel last week. No time to try your suggestion. I am still not quite understand with your suggestion. I tried like this but not quite correct. Could you help me?
this is my Controller
public function actionDelete($id)
{
if(Yii::app()->request->isPostRequest)
{
if( $id % 2 == 1 )
{
// we only allow deletion via POST request
$this->loadModel($id)->delete();
echo "success";
}
else
{
echo "failed";
}
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
...
public function actionAdmin()
{
$model=new Master('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Master']))
$model->attributes=$_GET['Master'];
$this->render('admin',array(
'model'=>$model,
));
}
this is my view, admin.php
...
<h1>Manage Masters</h1>
<p>
You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php echo $this->renderPartial('_grid', array('model'=>$model)); ?>
try{
$this->loadModel($id)->delete();
Yii::app()->user->setFlash('deleteStatus','Deleted Successfully');
echo "<div class='flash-success'>Deleted Successfully</div>"; //for ajax
}catch(CDbException $e){
Yii::app()->user->setFlash('deleteStatus','One or more town is related with this region');
echo "<div class='flash-error'>One or more town is related with this region</div>"; //for ajax
}
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));