【问题标题】:How can i make if else condition inside sweetalert2 swal.fire?如果在 sweetalert2 swal.fire 中出现其他条件,我该如何制作?
【发布时间】: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 查询获取此值,以便您可以将其用于条件。例如&lt;button data-condition = "&lt;?php echo $row-&gt;name?&gt;" &gt;Delete&lt;/button&gt; 在您的条件下您可以像$(this).data("condition"). 一样获得它跨度>

标签: javascript php codeigniter sweetalert2


【解决方案1】:

你可以使用 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 
              }
            })

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2015-03-22
      • 1970-01-01
      • 2021-06-09
      • 2017-07-12
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 2016-02-23
      相关资源
      最近更新 更多