我怎样才能在 sweetalert2 swal.fire 中设置 if else 条件?

How can i make if else condition inside sweetalert2 swal.fire?

我的table和其他字段table有关系(约束外键),所以点击这个删除按钮的时候。它会显示甜蜜的警报,如果他们连接,数据将无法删除。

这是我的代码

<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 中设置 if else 条件? 例如:我在这个 table 中有一个名为 "Head Management" 的部门名称,其他 table 正在使用主管管理,所以当用户想删除这个主管管理时,系统将显示错误甜蜜的警报。对不起,我的英语不好。

$.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',
            },
        })

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

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

所以你必须设置你的 url 与查询响应根据它发生

其他方法是在您的 url 中创建一个 echo 查询并取得成功,如下所示:

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

试试这个:

 $.ajax({method: 'post',
            url: baseUrl + '/ci/app/admin/department-delete'+ id,
            data: {
               id:id
            },
 })
            .then(function (response) {
                    console.log(response);
                    if (response.data.code == 'success') {
                        Swal.fire({title: 'Success', 'text': ('success message'), 'type': 'success'})
                                .then(() => {
                                    location.href = baseUrl + "/";//where you want to redirect after success
                                });
                    } else {
                        Swal.fire({title: 'Error', 'text': response.data.message, 'type': 'error'});
                    }
                })
                .catch(function (error) {
                    if ((error.response.data).hasOwnProperty('message')) {
                        Swal.fire({title: 'Error', 'text': error.response.data.message, 'type': 'error'});
                    } else {
                        Swal.fire({title: 'Error', 'text': 'Something went wrong', 'type': 'error'});
                    }
                });