Código GURUDEV:
Estoy trabajando en una aplicación utilizando jQuery ajax mathod y tratando de eliminar fila de la tabla haciendo clic en el botón de borrar. La parte de back-end de php está bien, pero lo que estoy tratando de aplicar es ocultar o método jQuery fadeOut () en el evento clic, pero no puede obtener el resultado. Véase el siguiente código:
$('.deletePageBtn').on('click', function(e) {
e.preventDefault();
if (confirm('Do you really want to delete this page?')) {
var pageId = $(this).data("pageid");
$.ajax({
url: "<?= base_url('admin/pagemanagerSavePage')?>",
type: 'POST',
data: {
action: "delete-page",
pageId: pageId
},
success: function(data) {
if (data == 'success') {
$(this).parent().parent().remove();
}
console.log(data);
},
error: function(data) {
console.log(data);
}
});
};
});
<tr id="rowId_26" class="rowData odd" role="row">
<td class="text-center sorting_1">3</td>
<td class="bg-success text-light text-center">
<i class="fas fa-eye"></i>
</td>
<td>
Pawan Mall - Internet Geek 123
<div class="btn-group float-right">
<a href="#" class="btn btn-sm btn-primary" target="_blank">
<i class="fas fa-eye"></i> View Page
</a>
<a href="" id="editPage" class="btn btn-sm btn-warning">
<i class="fas fa-edit"></i> Edit Page
</a>
<a href="" data-pageid="26" class="btn btn-sm btn-danger deletePageBtn">
<i class="fas fa-trash"></i> Delete Page
</a>
</div>
</td>
</tr>
palaѕn:
La cuestión aquí es dentro éxito ajax:
success: function(data) {
if (data == 'success') {
$(this).parent().parent().remove();
}
console.log(data);
},
this
se refiere a la jqXHR
objeto de la llamada Ajax. No es mismo $('.deletePageBtn')
elemento más. Para realizar este trabajo se puede almacenar en caché this
en el arranque dentro clic evento como:
var $this = $(this);
y luego usarlo dentro de éxito como:
success: function(data) {
if (data == 'success') {
$this.parent().parent().remove();
}
console.log(data);
},
o mejor:
success: function(data) {
if (data == 'success') {
$this.closest('tr').remove();
}
console.log(data);
},