function batchDelete() {
var rows = $('#dg').datagrid('getSelections');
if (rows.length === 0) {
alert('請至少選擇一行!');
return;
}
// 提取 id 字段(假設主鍵為 id)
var ids = rows.map(function(row) { return row.id; }).join(',');
$.ajax({
url: 'batch_process.php',
type: 'POST',
data: { ids: ids, action: 'delete' },
dataType: 'json',
success: function(res) {
if (res.success) {
$('#dg').datagrid('reload');
alert('操作成功:' + res.msg);
} else {
alert('失敗:' + res.msg);
}
}
});
}
false, 'msg' => '無 ID']);
exit;
}
$idArray = array_map('intval', explode(',', $ids)); // 防注入
$placeholders = implode(',', array_fill(0, count($idArray), '?'));
// 示例:批量刪除
$pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
$stmt = $pdo->prepare("DELETE FROM data_table WHERE id IN ($placeholders)");
$stmt->execute($idArray);
echo json_encode(['success' => true, 'msg' => "已處理 {$stmt->rowCount()} 條"]);
?>
若需更新,前端用 getChanges('modified') 獲取修改行,構造 [{id:1, field:'val'}, ...],後端 json_decode 後逐條預處理語句更新。