1、视图文件
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'customer-grid',
'dataProvider' => $model->search(),
//'filter' => $model,
'columns' => array(
array(
'selectableRows' => 2,
'class' => 'CCheckBoxColumn',
'id' => 'customer-select-all',
'checkBoxHtmlOptions' => ['name' => 'ids[]'],
'headerHtmlOptions' => ['width' => '60px'],
'headerTemplate' => '全选{item}',
'footer' => '<button type="button" onclick="doBatchDelete();">批量删除</button>',
),
'id',
'name',
'age',
array(
'class' => 'CButtonColumn',
),
),
));
2、JS文件
Yii::app()->clientScript->registerScript('_selectAll', '
var doBatchDelete = function () {
var ids = new Array();
$("input:checkbox[name=\'ids[]\']").each(function () {
var o = $(this);
if (o.is(":checked"))
ids.push(o.val());
});
if (0 == ids.length) {
alert("请选择要删除的数据!");
return;
}
$.post("' . $this->createUrl('/customer/deleteAll') . '", {"ids[]": ids}, function (resp) {
var data = $.parseJSON(resp);
if (data.result == "ok") {
alert("删除成功!");
$.fn.yiiGridView.update("customer-grid");
}
});
}
', CClientScript::POS_HEAD);
3、action文件
/**
* 批量删除
*/
public function actionDeleteAll()
{
$request = Yii::app()->request;
if (!$request->isAjaxRequest || !$request->isPostRequest)
return;
$ids = $_POST['ids'];
$criteria = new CDbCriteria;
$criteria->addInCondition('id', $ids);
if (Customer::model()->deleteAll($criteria))
echo CJSON::encode(['result' => 'ok']);
}
本文介绍如何使用Yii框架实现批量删除功能,包括视图文件设置、JavaScript交互代码编写及后端action处理流程。通过示例代码展示了批量选择、批量删除按钮触发及服务器端批量删除逻辑。
152

被折叠的 条评论
为什么被折叠?



