ajax_form.blade.php
3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<div class="row">
<div class="col-md-12">
<span class="pesan"></span>
<div id="output"></div>
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
<div id="statustxt">0%</div>
</div>
</div>
</div>
</div>
<script src="{{ URL::asset('resources/vendor/jquery/jquery.enc.js') }}"></script>
<script src="{{ URL::asset('resources/vendor/jquery/jquery.form.js') }}"></script>
<script type="text/javascript">
$(document).ready(function() {
var progressbox = $('.progress'); //progress bar wrapper
var progressbar = $('.progress-bar'); //progress bar element
var statustxt = $('#statustxt'); //status text element
var myform = $('#formAptika'); //upload form
var output = $('#output'); //ajax result output element
var completed = '0%'; //initial progressbar value
var nextUrl = $('#url').val();
var modalfooter = $('.modal-footer');
progressbox.hide();
$('.kirim-modal').click(function() {
$(myform).submit();
});
$(myform).ajaxForm({
cache: false,
beforeSend: function() { //before sending form
$(".pesan").html('<div class="alert alert-info" role="alert"><center><i class="fa fa-spinner fa-spin"></i> Loading...</center></div>');
myform.prop("disabled", true);
statustxt.empty();
progressbox.show(); //show progressbar
progressbar.width(completed); //initial value 0% of progressbar
statustxt.html(completed); //set status text
modalfooter.hide();
},
uploadProgress: function(event, position, total, percentComplete) { //on progress
progressbar.width(percentComplete + '%') //update progressbar percent complete
statustxt.html((percentComplete-1) + '%'); //update status text
if(percentComplete == 100) {
$('.pesan').html('<div class="haraptunggu alert alert-info text-center" role="alert">Harap tunggu, proses penyimpanan file sedang berlangsung!</div>');
}
},
complete: function(response, x , e) { // on complete
if (!response.hasOwnProperty('responseJSON')) {
console.log(response);
} else {
var data = response.responseJSON;
// var data = $.parseJSON(response.responseText);
if (data.status == true) {
$('.modal').modal('hide');
Swal.fire({
title: 'Berhasil',
text: data.pesan.msg,
icon: 'success',
timer: 2500
});
if (data.hasOwnProperty('next_url')) {
location.href= data.next_url;
} else {
if ($('#datatable').length) {
$('#datatable').DataTable().ajax.reload();
}
}
} else {
$('.pesan').html('');
progressbox.hide();
modalfooter.show();
if (data.pesan.hasOwnProperty('msg')) {
$('#datatable').DataTable().ajax.reload();
$('.modal').modal('hide');
Swal.fire({
title: 'Gagal',
text: data.pesan.msg,
icon: 'error'
});
} else {
$.each(data.pesan, function(i, item) {
$('.invalid-feedback').show();
$('#'+i).addClass('is-invalid');
$('#'+i+'-error').html('<strong>' + item[0] + '<strong>');
//$(".pesan").html('<div class="alert alert-danger" role="alert">' + item[0] +'</div>');
});
}
}
}
}
});
$('.form-control').keyup(function() {
var id = $(this).attr('id');
$('#'+id).removeClass('is-invalid');
$('#'+id+'-error').hide();
$('#'+id+'-error').html('');
});
});
</script>