1.首先,写button按钮:
视图中,columns下面声明checkbox代码:
array( 'class' => 'CCheckBoxColumn', 'selectableRows' => 2, 'footer' => '<button type="button" οnclick="GetCheckbox();" style="width:76px">批量删除</button>', 'checkBoxHtmlOptions' => array('name' => 'id[]'), //name在js中会用到 ),例子如下:
<?php $this->widget('admin.widgets.GridView', array(
'id'=>'app-comment-grid','dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'selectableRows' => 2,
'footer' => '<button type="button" οnclick="GetCheckbox();" style="width:72px">批量删除</button>',
'class' => 'CCheckBoxColumn',
'headerHtmlOptions' => array('width'=>'33px'),
'checkBoxHtmlOptions' => array('name' => 'selectdel[]'),
),
array(
'header'=>'App图标',
'type'=>'raw',
'value'=>'CHtml::link(CHtml::image($data->app->getIcon57x57()), $data->app->getQianUrl(),array("target"=>"_blank"))',
'htmlOptions'=>array(
'width'=>'57',
'height'=>'57',
'style'=>'text-align:center',
),
array(
'header'=>'操作',
'class'=>'CButtonColumn',
'headerHtmlOptions'=>array(
'width'=>'40',
),
'template'=>'{update}{delete}', //默认显示的三个按钮,要去掉某个按钮,删除相应项即可
),
),
)); ?>
<script type="text/javascript">
/*<![CDATA[*/
var GetCheckbox = function (){
var data=new Array();
$("input:checkbox[name='selectdel[]']").each(function (){
if($(this).is(":checked")){
data.push($(this).val());
}
});
if(data.length > 0){
if(confirm('确定要删除选中的留言吗?')){
$.post('<?php echo CHtml::normalizeUrl(array('/admin/appComment/delall/'));?>',{'selectdel[]':data}, function (data) {
var ret = $.parseJSON(data);
if (ret != null && ret.success != null && ret.success) {
$.fn.yiiGridView.update('app-comment-grid');
}
});
}else
return false;
}else{
alert("请选择要删除的留言!");
}
}
/*]]>*/
</script>
2.js中post请求的控制器中写逻辑代码:
public function actionDelall() {
if (Yii::app()->request->isPostRequest) {
$criteria = new CDbCriteria;
$criteria->addInCondition('id', $_POST['id']);
News::model()->deleteAll($criteria); //News换成你的模型
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.');
}