Yii中CGridView实现批量删除的方法
本文实例讲述了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程序设计有所帮助。
相关推荐
WasteLand 2020-10-18
Allinputs 2020-08-30
Ashes 2020-06-14
caiyiii 2020-06-14
kxguan 2020-06-14
daillo 2020-06-14
一粒沙里的世界 2020-06-14
ruxingli 2020-06-14
csssy00 2020-06-14
阿佐 2020-06-14
NameWFY 2020-05-28
NameWFY 2020-05-26
Robin罗兵 2020-05-16
caiyiii 2020-04-29
wmsjlihuan 2020-04-26
cbao 2020-04-26
csssy00 2020-04-19
igogo00 2020-03-09