最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can i make if else condition inside sweetalert2 swal.fire? - Stack Overflow

programmeradmin2浏览0评论

My table has relationship with other field table(Constraint foreign key), so when this delete button click. it will show sweet alert that the data cannot be delete if they connected.

Here my code

<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>

and here my 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'
        )
    }
})

});

how can i make if else condition inside the sweetalert ? For example : i have department name called "Head Management" in this table, the head management is being used in other table, so when user wanna delette this Head Management, the system will show error with sweet alert. Sorry for my bad english.

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

My table has relationship with other field table(Constraint foreign key), so when this delete button click. it will show sweet alert that the data cannot be delete if they connected.

Here my code

<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>

and here my 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'
        )
    }
})

});

how can i make if else condition inside the sweetalert ? For example : i have department name called "Head Management" in this table, the head management is being used in other table, so when user wanna delette this Head Management, the system will show error with sweet alert. Sorry for my bad english.

$.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',
            },
        })
Share Improve this question asked Apr 11, 2020 at 2:48 pohon gemspohon gems 371 gold badge1 silver badge6 bronze badges 1
  • It simple make function for delete and pass argument to that function and use for your condition. If you don't want to make function then you can use data attribute in button and get this value by j query so you can use it for condition.for example <button data-condition = "<?php echo $row->name?>" >Delete</button> in you condition you can get it like $(this).data("condition"). – PHP_only Commented Apr 11, 2020 at 3:10
Add a ment  | 

2 Answers 2

Reset to default 3

you can use the catch error from ajax method, It´s like:

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

So you have to set your url with query to response according with it happened

Other way is make an echo in your url with query and getting in your success, like this:

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

try this:

 $.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'});
                    }
                });
发布评论

评论列表(0)

  1. 暂无评论