I am loading a datatable to a sweetalert2, in which I want to show the list of students that are registered in that class, otherwise do something else, the problem is that it does not load the data.
with that code it loads the data, but it gives me an error in the console that tells me that reload is not a function, try ajax.reload(); but it throws me an errorUncaught TypeError: $(...).DataTable(...).reload is not a function
"initComplete": function(settings, json) {
$("#alumnosCurso").DataTable().reload();
},
This is the complete function in which I execute the sweetalert2 with the datatable
$('body').on('click', '.eliminarCurso', function () { // Boton eliminar Datatable
var id_curso = $(this).data("id");
var view_html='';
var url = "{{ url('getAlumnosCurso') }}/"+id_curso;
$.get(url, function (r) { // hago la consula a mi tabla alumnos
var data = $.parseJSON(r);
console.log(data);
if(data==null){ // si no hay alumnos registrados con este curso
}else{ // si existe hago lo siguiente
$.each(data, function(i) {
var alumnosCurso = '<table id="alumnosCurso" class="table table-striped nowrap" width="100%"><thead><tr><th>Rut</th><th>Nombre</th><th>Apellidos</th></tr></thead><tbody></tbody></table>';
$('#alumnosCurso').DataTable({
'paging' : false,
'lengthChange': false,
'searching' : false,
'ordering' : false,
'processing' : false,
'info' : false,
'autoWidth' : true,
'responsive' : true,
'data' : data,
'columns': [
{ data: 'rut'},
{ data: 'nombre'},
{ data: 'apellidos'}
]
});
Swal.fire({
title: 'CUIDADO!',
text: 'ZY',
html: alumnosCurso,
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.value) {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
)
}
})
})
}
});
});
the format of the data is as follows
I attach this image in which you can see what I want to do, but it gives me that error in the console
SOLVED
I chose to load the datatable by ajax, but this gave me a problem when loading the datatable, since as I showed in the question, it loaded the sweetalert first before the datatable, so what I did was place the sweetalert inside the initComplete el which its function is to execute "something" after the table is loaded
here the solution
DataTable
** Delete btn function *
The sweetalert inside the initComplete
Route
Function of the route in the controller
And this is the result