SweetAlert confirm delete

2

I am currently working on this code from an example of a search table, deletion and editing of fields, I want to use Sweetalert to make javascript alerts more visible and although I have managed to display them in my javascript code, I can not get rid of the field , now if I use the confirmDel function it eliminates without any problems.

How should the javascript code be left for the sweet alert to delete?

var script = document.createElement('script');
    script.src = "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.10.1/sweetalert2.all.min.js";
	script.src = "https://cdnjs.cloudflare.com/ajax/libs/sweetalert/0.4.2/sweet-alert.min.js";
	script.src = "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.10.1/sweetalert2.all.min.js";
    document.getElementsByTagName('script')[0].parentNode.appendChild(script);
    //for CSS file
    $('head').append('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/0.4.2/sweet-alert.css" />');



function lista_libros(valor){
	$.ajax({
		url:'../Controllers/libros.php',
		type:'POST',
		data:'valor='+valor+'&boton=buscar'
	}).done(function(resp){
		//alert(resp);
		var valores = eval(resp);
		html="<table class='rwd-table'><thead><tr><th>#</th><th>ID Solicitud</th><th>Usuario</th><th>Miniauditorio</th><th>Equipo</th><th>Fecha-Solicitud</th><th>Fecha-Evento</th><th>Evento-Culmina</th><th>Hora-Inicio-Evento</th><th>Hora-Fin-Evento</th><th>Detalles</th><th>Estatus</th></tr></thead><tbody>";
		for(i=0;i<valores.length;i++){
			datos=valores[i][0]+"*"+valores[i][0]+"*"+valores[i][2]+"*"+valores[i][3]+"*"+valores[i][4]+"*"+valores[i][5]+"*"+valores[i][6]+"*"+valores[i][7]+"*"+valores[i][8]+"*"+valores[i][9]+"*"+valores[i][10]+"*"+valores[i][11];
			html+="<tr><td>"+(i+1)+"</td><td>"+valores[i][0]+"</td><td>"+valores[i][1]+"</td><td>"+valores[i][2]+"</td><td>"+valores[i][4]+"</td><td>"+valores[i][5]+"</td><td>"+valores[i][6]+"</td><td>"+valores[i][7]+"</td><td>"+valores[i][8]+"</td><td>"+valores[i][9]+"</td><td>"+valores[i][10]+"</td><td>"+valores[i][11]+"</td><td><button class='btn btn-success' data-toggle='modal' data-target='#modallibros' onclick='mostrar("+'"'+datos+'"'+");'><span class='glyphicon glyphicon-pencil'></span></button><button class='btn btn-danger' onclick='sweetDelete("+'"'+valores[i][0]+'"'+")'><span class='glyphicon glyphicon-remove'></span></button></td></tr>";
		}
		html+="</tbody></table>"
		$("#lista").html(html);
	});
}
function guardar(){
	var datosform=$("#formLibro").serialize();
	$.ajax({
		url:'../Controllers/libros.php',
		type:'POST',
		data:datosform+"&boton=actualizar"
	}).done(function(resp){
		if(resp==='exito'){
			$('#exito').show();
			lista_libros('');
		}
		else{
			alert(resp);
		}
		
	});
	
}
function mostrar(datos){
	//alert(datos);
	var d=datos.split("*");
	//alert(d.length);
	$("#idusuario").val(d[0]);
	$("#miniauditorio").val(d[2]);
	$("#equipo").val(d[4]);
	$("#fechaevento").val(d[6]);
	$("#eventoculmina").val(d[7]);
	$("#horainicio").val(d[8]);
	$("#horafin").val(d[9]);
	$("#status").val(d[11]);
	$("#detalles").val(d[10]);
	

	
}

function eliminar(id){
	//alert(id);
	$.ajax({
		url:'../Controllers/libros.php',
		type:'POST',
		data:'idusuario='+id+'&boton=eliminar'
	}).done(function(resp){
		alert(resp);
		lista_libros('');
	});
	
}
//



//
/*
function confirmDel(id)
{
  var agree=confirm("¿Realmente desea eliminarlo? Los datos se eliminarán permanentemente");
  if (agree==true){
     eliminar(id);
  }else{ 
   return false;
  }
}
*/
function eliminar(id){
  $.ajax({
  url:'../Controllers/libros.php',
  type:'POST',
  data:'idusuario='+id+'&boton=eliminar'
 }).done(function(resp){
  alert(resp);
  lista_libros('');
 });
}

/*sweet alert*/
function sweetDelete(id){
swal({
  title: '¿Estás Seguro?',
  text: 'No podrás recuperar esta información!',
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Sí, eliminalo!',
  cancelButtonText:  'Cancelar!',
  closeOnConfirm: false
},
function(){
  swal(
    'Deleted!',
    'Your file has been deleted.',
    'success'
  );
});
}
/* Tratado de realizar una confirmacion para eliminar, hace la pregunta pero al confirmar no elimina
function confirmDel()
{
  var agree=confirm("¿Realmente desea eliminarlo? ");
  if (agree) function eliminar("+'"'+valores[i][0]+'"'+");
  return false;
}

*/
    
asked by Luis Alfredo Serrano Díaz 02.10.2017 в 04:13
source

1 answer

2

If you are using the recent versions of SweetAlert, you should use the promises syntax, and simply add the call to the code you delete or the delete function in then() :

function sweetDelete(id){
swal({
  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!',
  cancelButtonText: 'No, cancel!',
  confirmButtonClass: 'btn btn-success',
  cancelButtonClass: 'btn btn-danger',
  buttonsStyling: false
}).then(function () {
  // código que elimina
  $.ajax({
    url:'../Controllers/libros.php',
    type:'POST',
    data:'idusuario='+id+'&boton=eliminar'
   }).done(function(resp){
    lista_libros('');
   });
  swal(
    'Deleted!',
    'Your file has been deleted.',
    'success'
  )
}, function (dismiss) {
  // dismiss can be 'cancel', 'overlay',
  // 'close', and 'timer'
  if (dismiss === 'cancel') {
    swal(
      'Cancelled',
      'Your imaginary file is safe :)',
      'error'
    )
  }
}

)
}
    
answered by 02.10.2017 / 04:30
source