本文实例讲述了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程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!