这是我的gridview的CButtonColumn
array(
'header' => 'Action',
'class' => 'CButtonColumn',
'template' => '{view} {delete}',
'buttons' => array(
'delete' =>
array(
'url' => 'Yii::app()->controller->createUrl("/workorders/delete",array("id"=>$data->primaryKey))',
'label' => 'delete',
'options' => array( // this is the 'html' array but we specify the 'ajax' element
'confirm' => $alert,
'class' => 'grid_action_set1',
'ajax' => array(
'type' => 'POST',
'url' => "js:$(this).attr('href')", // ajax post will use 'url' specified above
'success' => 'function(data){
if(data == "true"){
//update the grid...
$.fn.yiiGridView.update("workorders-grid");
return false;
}else{
$("#mydialog").dialog("open"); return false;
return false;
}
}',
),
),
),
'view' => array
(
'label'=>'View workorder detail.',
'url'=>'Yii::app()->createUrl("/workorders/view", array("id"=>$data->id))',
),
),
'htmlOptions' => array('width' => '30px', 'style'=>'text-align:center;')
),
这是检查删除前的记录
protected function beforeDelete()
{
if ($this->exists("(status_id = 6 OR status_id = 7) AND parent_mac = '$this->mac_address'"))
return false;
else
return parent::beforeSave(); // prevent actual DELETE query from being run
}
这是我的控制器
public function actionDelete($id)
{
if($this->loadModel($id)->delete())
{
if(!isset($_GET['ajax']))
Yii::app()->user->setFlash('success','Normal - Deleted Successfully');
else
echo "<div class='flash-success'>Ajax - Deleted Successfully</div>";
$data = 'true';
}else{
if(!isset($_GET['ajax']))
Yii::app()->user->setFlash('error','This node is a parents of some other node. <br/>Please replace.');
else
echo "<div class='flash-error'>This node is a parents of some other node. <br/>Please replace.</div>";
$data = 'false';
}
// 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'));
}
我的问题是当用户点击删除按钮时,确认框“你确定要删除吗?”会出来的。当用户单击“是”时,检查记录是否有效删除。
如果记录有效删除,请删除记录并弹出“成功”消息框。更新gridview。
如果记录无效删除,“记录是等等等等”弹出消息框。
我该怎么办?
答案 0 :(得分:1)
public function actionDelete($id) {
$jenis = Usaha::model()->findByAttributes(array('id_jenis' => $id));
if($jenis==''){
$this->loadModel($id)->delete();
}
else {
throw new CHttpException(400, "your message alert "); //try this , will show some alert this i used when some data connected with another table in database
}
// 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'));
}