当前位置: 移动技术网 > IT编程>开发语言>PHP > Yii中CGridView实现批量删除的方法

Yii中CGridView实现批量删除的方法

2018年04月20日  | 移动技术网IT编程  | 我要评论

本文实例讲述了yii中cgridview实现批量删除的方法。分享给大家供大家参考,具体如下:

1. cgridview中的columns添加

array(
 'selectablerows' => 2,
 'footer' => '<button type="button" onclick="getcheckbox();" style="width:76px">批量删除</button>',
 'class' => 'ccheckboxcolumn',
 'headerhtmloptions' => array('width'=>'33px'),
 'checkboxhtmloptions' => array('name' => 'selectdel[]'),
),

作用是添加多选框

2.js代码

<script type="text/javascript">
/*<![cdata[*/
var getcheckbox = function (){
 var data=new array();
 $("input:checkbox[name='selectdel[]']").each(function (){
  if($(this).attr("checked")==true){
    data.push($(this).val());
  }
 });
 if(data.length > 0){
  $.post('<?php echo chtml::normalizeurl(array('/admin/words/delall/'));?>',{'selectdel[]':data}, function (data) {
   var ret = $.parsejson(data);
   if (ret != null && ret.success != null && ret.success) {
    $.fn.yiigridview.update('yw1');
   }
  });
 }else{
  alert("请选择要删除的关键字!");
 }
}
/*]]>*/
</script>

3.action

public function actiondelall()
{
 if (yii::app()->request->ispostrequest)
 {
  $criteria= new cdbcriteria;
  $criteria->addincondition('id', $_post['selectdel']);
  words::model()->deleteall($criteria);//words换成你的模型
  if(isset(yii::app()->request->isajaxrequest)) {
   echo cjson::encode(array('success' => true));
  } else {
   $this->redirect(isset($_post['returnurl']) ? $_post['returnurl'] : array('index'));
  }
 }
 else
  throw new chttpexception(400,'invalid request. please do not repeat this request again.');
}

希望本文所述对大家基于yii框架的php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网