如果在wealalert2 swal.fire内部发生其他情况,我怎么做?

问题描述 投票:0回答:1

我的表与其他字段表有关系(约束外键),因此单击此删除按钮时。它会显示警告,提示如果连接了数据就无法删除。

这里是我的代码

<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'
        )
    }
})

});

如果其他条件在甜味剂中,我该如何制造?例如:我在此表中具有部门名称“ Head Management”,该Head Management正在其他表中使用,因此当用户想要删除该Head Management时,系统将显示错误并显示警告。对不起,我的英语不好。

$.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',
            },
        })
javascript php codeigniter sweetalert2
1个回答
0
投票

您可以使用ajax方法中的catch错误,就像:

$.ajax({
        url: "/ci/app/admin/department-delete/" + id,
        method: "DELETE",
        success: function () {
            //Message ok
          },
          error: function(){
            //Failure message
            },
        })

因此,您必须将带有查询的URL设置为根据发生的事件进行响应

其他方法是通过查询在网址中输入echo并获得成功,例如:

 $.ajax({
            url: "/ci/app/admin/department-delete/" + id,
            method: "DELETE",
            success: function (response) {
                if(response){//successful validation
                //Message ok
                }else{
                //Failure message 
              }
            })
© www.soinside.com 2019 - 2024. All rights reserved.