Gestione AJAX dei form

This commit is contained in:
Thomas Zilio 2019-07-05 17:10:20 +02:00
parent c4a7539334
commit c651e7c81d
8 changed files with 124 additions and 47 deletions

1
.gitignore vendored
View File

@ -66,6 +66,7 @@ node_modules/
vendor/
*.phar
*.lock
package-lock.json
# Project #
######################

28
add.php
View File

@ -80,27 +80,19 @@ $(document).ready(function(){
echo '
$("#form_'.$id_module.'-'.$id_plugin.'").find("form").ajaxForm({
url: globals.rootdir + "/actions.php",
beforeSubmit: function(arr, $form, options) {
return $form.parsley().validate();
},
data: data,
type: "post",
success: function(data){
data = data.trim();
if(data && $("#'.get('select').'").val() !== undefined ) {
result = JSON.parse(data);
$("#'.get('select').'").selectSetNew(result.id, result.text);
$("#form_'.$id_module.'-'.$id_plugin.'").find("form").submit(function () {
submitAjax(this, data, function(response) {
// Selezione automatica nuovo valore per il select
select = "#'.get('select').'";
if ($(select).val() !== undefined) {
$(select).selectSetNew(response.id, response.text);
}
$("#bs-popup2").modal("hide");
},
error: function(data) {
alert("'.tr('Errore').': " + data);
}
});
});
return false;
})
});
</script>';
}

View File

@ -107,5 +107,12 @@ switch (get('op')) {
echo json_encode($response);
break;
case 'flash':
$response = flash()->getMessages();
echo json_encode($response);
break;
}

View File

@ -130,6 +130,8 @@ if (empty($record) || !$has_access) {
form.prepend(\'<button type="submit" id="submit" class="hide"></button>\');
$("#save").click(function(){
//submitAjax(form);
$("#submit").trigger("click");
});';

View File

@ -40,6 +40,25 @@ $(document).ready(function () {
moment.locale(globals.locale);
globals.timestampFormat = moment.localeData().longDateFormat('L') + ' ' + moment.localeData().longDateFormat('LT');
// Standard per i popup
toastr.options = {
"closeButton": true,
"debug": false,
"newestOnTop": false,
"progressBar": true,
"positionClass": "toast-top-right",
"preventDuplicates": true,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "12000",
"extendedTimeOut": "8000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
// Imposta lo standard per la conversione dei numeri
numeral.register('locale', 'it', {
delimiters: {
@ -1591,3 +1610,71 @@ function executeHook(hook, length){
},
});
}
function submitAjax(form, data = {}, callback = null, errorCallback = null) {
valid = $(form).parsley().validate();
if(valid) {
$("#main_loading").show();
content_was_modified = false;
// Fix per gli id di default
data.id_module = data.id_module ? data.id_module : globals.id_module;
data.id_record = data.id_record ? data.id_record : globals.id_record;
data.id_plugin = data.id_plugin ? data.id_plugin : globals.id_plugin;
// Invio dei dati
$(form).ajaxSubmit({
url: globals.rootdir + "/actions.php",
data: data,
type: "post",
success: function (data) {
data = data.trim();
if (data) {
response = JSON.parse(data);
callback(response);
}
$("#main_loading").fadeOut();
// Visualizzazione messaggi
$.ajax({
url: globals.rootdir + '/ajax.php',
type: 'get',
data: {
op: 'flash',
},
success: function (flash) {
messages = JSON.parse(flash);
info = messages.info ? messages.info : {};
Object.keys(info).forEach(function (element) {
toastr["success"](info[element]);
});
warning = messages.warning ? messages.warning : {};
Object.keys(warning).forEach(function (element) {
toastr["warning"](warning[element]);
});
error = messages.error ? messages.error : {};
Object.keys(error).forEach(function (element) {
toastr["error"](error[element]);
});
}
});
},
error: function (data) {
$("#main_loading").fadeOut();
toastr["error"](data);
errorCallback(data);
}
});
}
return valid;
}

View File

@ -231,7 +231,7 @@ function translateTemplate()
// Annullo le notifiche (AJAX)
if (isAjaxRequest()) {
flash()->clearMessage('info');
//flash()->clearMessage('info');
}
echo $template;

View File

@ -697,34 +697,15 @@ $(".btn-sm[data-toggle=\"tooltip\"]").each(function() {
var restore = buttonLoading(btn);
valid = submitAjax(form, {}, function() {
buttonRestore(btn, restore);
}, function() {
buttonRestore(btn, restore);
});
// Procedo al salvataggio solo se tutti i campi obbligatori sono compilati, altrimenti mostro avviso
if (form.parsley().isValid()) {
content_was_modified = false;
//form.find("input:disabled, select:disabled").removeAttr("disabled");
form.find("input:disabled, select:disabled").removeAttr("disabled");
$.ajax({
url: globals.rootdir + "/actions.php?id_module=" + globals.id_module ,
cache: false,
type: "POST",
processData: false,
dataType : "html",
data: form.serialize(),
success: function(data) {
$("#main_loading").fadeOut();
buttonRestore(btn, restore);
},
error: function(data) {
$("#main_loading").fadeOut();
swal("'.tr('Errore').'", "'.tr('Errore durante il salvataggio').'", "error");
buttonRestore(btn, restore);
}
});
} else {
if(!valid) {
swal({
type: "error",
title: "'.tr('Errore').'",

View File

@ -23,6 +23,7 @@
"jquery": "^3.2.1",
"jquery-form": "^4.2.1",
"jquery-ui-touch-punch": "^0.2.3",
"jquery.shorten": "^1.0.0",
"moment": "^2.18.1",
"numeral": "^2.0.6",
"parsleyjs": "^2.7.2",
@ -33,8 +34,8 @@
"signature_pad": "^2.1.1",
"smartwizard": "^4.2.2",
"sweetalert2": "^6.11.4",
"tooltipster": "^4.2.5",
"jquery.shorten": "^1.0.0"
"toastr": "^2.1.4",
"tooltipster": "^4.2.5"
},
"devDependencies": {
"archiver": "^2.0.0",
@ -173,6 +174,12 @@
"main": [
"src/jquery.shorten.js"
]
},
"toastr": {
"main": [
"build/toastr.min.js",
"build/toastr.min.css"
]
}
}
}