Home >php教程 >php手册 >Yii中CGridView实现批量删除的方法,yiicgridview

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

WBOY
WBOYOriginal
2016-06-13 08:48:531135browse

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

本文实例讲述了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('<&#63;php echo CHtml::normalizeUrl(array('/admin/words/delall/'));&#63;>',{'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']) &#63; $_POST['returnUrl'] : array('index'));
  }
 }
 else
  throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}

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

您可能感兴趣的文章:

  • YiiFramework入门知识点总结(图文教程)
  • Yii入门教程之目录结构、入口文件及路由设置
  • Yii入门教程之Yii安装及hello world
  • Yii PHP Framework实用入门教程(详细介绍)
  • Yii查询生成器(Query Builder)用法实例教程
  • Yii实现单用户博客系统文章详情页插入评论表单的方法
  • YII使用url组件美化管理的方法
  • Yii快速入门经典教程
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn