1.在視圖中 CGridView中的columns添加javascript
array( 'selectableRows' => 2, 'footer' => '<button type="button" onclick="GetCheckbox();" style="width:76px">批量刪除</button>', 'class' => 'CCheckBoxColumn', 'headerHtmlOptions' => array('width'=>'33px'), 'checkBoxHtmlOptions' => array('name' => 'selectdel[]'), ),
做用是添加多選框php
2.js代碼java
<script type="text/javascript"> function GetCheckbox(){ var data=new Array(); $("input:checkbox[name='selectdel[]']").each(function (){ if($(this).attr("checked")==true){ data.push($(this).val()); } }); if(data.length > 0){ $.post("index.php?r=ip/delall",{'selectdel[]':data}, function (data) { if (data=='ok') { alert('刪除成功!'); window.open('index.php?r=ip/admin','indexFrame');; } }); }else{ alert("請選擇要刪除的選項!"); } } </script>
3.Actionapp
/* * 做用:批量刪除 */ public function actionDelall() { if (Yii::app()->request->isPostRequest) { $criteria= new CDbCriteria; $criteria->addInCondition('ip_id', $_POST['selectdel']); Ip::model()->deleteAll($criteria);//Words換成你的模型 if(isset(Yii::app()->request->isAjaxRequest)) { echo 'ok'; } else $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index')); } else throw new CHttpException(400,'Invalid request. Please do not repeat this request again.'); }