【发布时间】:2020-04-11 02:48:40
【问题描述】:
我的表与其他字段表有关系(约束外键),所以当这个删除按钮点击时。它会显示甜蜜的警报,如果数据连接,则无法删除。
这是我的代码
<tbody>
<?php
$no = 1;
foreach ($data as $row){?>
<tr id="<?php echo $row->id; ?>">
<th scope="row"><?php echo $no ?></th>
<td><?php echo $row->id?></td>
<td><?php echo $row->name?></td>
<td>
<button type="submit"
class="btn btn-icon btn-flat-danger mb-1 remove-department"
data-toggle="tooltip" data-placement="top" title="Delete"><i
class="feather icon-trash-2"></i></button>
</td>
</tr>
<?php $no++; } ?>
</tbody>
这里是我的 sweetalert.js :
$(".remove-department").click(function () {
var id= $(this).parents("tr").attr("id");
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.value) {
$.ajax({
url: "/ci/app/admin/department-delete/" + id,
method: "DELETE",
success: function () {
Swal.fire(
'DELETE',
'SUCCESSFULLY DELETED',
'success',
).then(function () {
location.href = "/ci/app/admin/department";
});
},
})
} else if (result.dismiss === swal.DismissReason.cancel) {
swal.fire(
'Canceled',
'Your data not deleted',
'error'
)
}
})
});
如果在 sweetalert 内有其他条件,我该如何设置? 例如:我在这个表中有一个部门名称叫做“主管管理”,这个主管正在其他表中使用,所以当用户想删除这个主管时,系统会显示错误并带有甜蜜警报。对不起,我的英语不好。
$.ajax({
url: "/ci/app/admin/department-delete/" + id,
method: "DELETE",
success: function () {
if( .... ==TRUE){
Swal.fire(
'DELETE',
'SUCCESSFULLY DELETED',
'success',
).then(function () {
location.href = "/ci/app/admin/department";
});
}else{
Swal.fire(
'Error',
'Fail DELETED',
'error',
},
})
【问题讨论】:
-
它可以简单地创建删除函数并将参数传递给该函数并用于您的条件。如果您不想创建函数,那么您可以在按钮中使用数据属性并通过 j 查询获取此值,以便您可以将其用于条件。例如
<button data-condition = "<?php echo $row->name?>" >Delete</button>在您的条件下您可以像$(this).data("condition").一样获得它跨度>
标签: javascript php codeigniter sweetalert2