remove label input / with AJAX

2

I have several inputs that I charge with a foreach with Razor.

<table class="table">
            @foreach (var item in Model.Municipalities)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(x => item.Name)
                    </td>
                    <td>                            
                        <input class="btn btn-danger" type="button" value="Delete" onclick="SweetAlert(@item.MunicipalityId)" />
                    </td>
                </tr>
            }
        </table>

As you can see, it has an onclick event and I need that when it does succeed, it is deleted without loading the page and this is what I have but it does not work for me.

function () {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("DeleteMunicipality")',
                    data: { MunicipalityId: id },
                    datatype: "html",
                    //success: function (data) {
                    //    //codigo
                    //}
                    success: function () {
                        swal('Deleted!',
                             'Your imaginary file has been deleted.',
                             'success'
                             );
                        $(this).remove();
                    },
                    error: function () {
                        swal("Oops", "We couldn't connect to the server! or The record can't be delete.(has related records)", "error");
                    }
                });
                return false;
            }
    
asked by Kmiilo Berrio Montoya 14.06.2016 в 17:06
source

4 answers

1

You have to go to the <tr> tag to remove the row

success: function () {
            swal('Deleted!',
                 'Your imaginary file has been deleted.',
                 'success'
                 );
            var tr = $('#'+id).parent().parent();
            tr.remove();
        },

using the parent () you go up to the previous tag to who throws the event that in this case would be the button, or you should ensure that this is the button.

If the structure is dynamic, you could use

var tr = $('#'+id).closest('tr');
tr.remove();

jquery .closest ()

    
answered by 14.06.2016 / 17:21
source
1

In the case you raise, this (used within the success option) refers to the object of the ajax request, not the element that triggered the request.

If you want to use this to refer to the button in the callback, you must add it in the context option of $.ajax() :

$.ajax({
  // las demás opciones de configuración ...
  context: this,
  success: function(response) {
    // aquí 'this' se referirá a lo que esté en 'context'
  }
});

You can see more information about context in jQuery's ajax documentation: link

    
answered by 14.06.2016 в 17:19
0

What you can do is remove the this and put a selector type css your success would be this way

success: function () {
  swal('Deleted!',
    'Your imaginary file has been deleted.',
    'success'
  );
  $('input[value="Delete"]').remove();
}  

Since as you have it with the this it is as if you were removing an element from another fragment page

    
answered by 14.06.2016 в 17:16
0

I already solve it, I have to do this, is it correct and is it good practice to do it?

 <table class="table">
            @foreach (var item in Model.Municipalities)
            {
                <tr id="@item.MunicipalityId">
                    <td>
                        @Html.DisplayFor(x => item.Name)
                    </td>
                    <td>
                        @Html.ActionLink("Edit", "EditMunicipality", new { id = item.MunicipalityId }, new { @class = "btn btn-warning" }) |
                        @* @Html.ActionLink("Delete", "DeleteMunicipality", new { id = item.MunicipalityId }, new { @class = "btn btn-danger", onClick = "swal({   title: 'Are you sure?',   text: 'You will not be able to recover this imaginary file!',   type: 'warning',   showCancelButton: true,   confirmButtonColor: '#DD6B55',   confirmButtonText: 'Yes, delete it!',   closeOnConfirm: false }, function(){   swal('Deleted!', 'Your imaginary file has been deleted.', 'success');});" })*@
                        <input class="btn btn-danger" type="button" value="Delete" onclick="SweetAlert(@item.MunicipalityId)" />
                    </td>
                </tr>
            }
        </table>

<script>
    //$('.link1').click(
    function SweetAlert(id) {
        swal({
            title: 'Are you sure?',
            text: 'You will not be able to recover this imaginary file!',
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#DD6B55',
            confirmButtonText: 'Yes, delete it!',
            closeOnConfirm: false
        },
            function () {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("DeleteMunicipality")',
                    data: { MunicipalityId: id },
                    datatype: "html",
                    //success: function (data) {
                    //    //codigo
                    //}
                    success: function () {
                        swal('Deleted!',
                             'Your imaginary file has been deleted.',
                             'success'
                             );
                        $('#'+id).remove();
                    },
                    error: function () {
                        swal("Oops", "We couldn't connect to the server! or The record can't be delete.(has related records)", "error");
                    }
                });
                return false;
            }

        );
    }
</script>

}

    
answered by 14.06.2016 в 17:59