Home  >  Article  >  Backend Development  >  How to implement batch deletion of CGridView in Yii, yiicgridview_PHP tutorial

How to implement batch deletion of CGridView in Yii, yiicgridview_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:20931browse

How to implement batch deletion of CGridView in Yii, yiicgridview

This article describes the method of implementing batch deletion of CGridView in Yii. Share it with everyone for your reference, the details are as follows:

1. Adding columns in CGridView

array(
 'selectableRows' => 2,
 'footer' => '<button type="button" onclick="GetCheckbox();" style="width:76px">批量删除</button>',
 'class' => 'CCheckBoxColumn',
 'headerHtmlOptions' => array('width'=>'33px'),
 'checkBoxHtmlOptions' => array('name' => 'selectdel[]'),
),

The function is to add a multi-select box

2.js code

<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.');
}

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

Articles you may be interested in:

  • Summary of YiiFramework introductory knowledge points (graphic tutorial)
  • Yii introductory tutorial directory structure, entry file and routing settings
  • Yii introductory tutorial - Yii installation and hello world
  • Yii PHP Framework practical introductory tutorial (detailed introduction)
  • Yii query builder (Query Builder) usage example tutorial
  • Yii’s method of inserting a comment form into the article details page of a single-user blog system
  • YII’s method of using url components to beautify management
  • Yii Quick Start Classic Tutorial

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1085883.htmlTechArticleHow to implement batch deletion with CGridView in Yii, yiicgridview This article describes the method of implementing batch deletion with CGridView in Yii. Share it with everyone for your reference, the details are as follows: 1. Cgri...
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