32 lines
1.3 KiB
JavaScript
32 lines
1.3 KiB
JavaScript
$(document).ready(function() {
|
|
$('#category_id').change(function() {
|
|
if ($(this).val() === 'nuovo') {
|
|
$('#nuovo_valore').show();
|
|
$('#nuovo_aggiungi').show();
|
|
} else {
|
|
$('#nuovo_valore').hide();
|
|
$('#nuovo_aggiungi').hide();
|
|
}
|
|
});
|
|
|
|
$('#nuovo_aggiungi').click(function() {
|
|
var selectedValue = $('#category_id').val();
|
|
var nuovoValore = $('#nuovo_valore').val().trim();
|
|
|
|
if (selectedValue === 'nuovo' && nuovoValore !== '') {
|
|
$.post('/api/categorie', { cat_name: nuovoValore,cat_entrata: 1,cat_uscita: 1, _token: $('meta[name="csrf-token"]').attr('content') }, function(data) {
|
|
if (data.success) {
|
|
// Aggiungi la nuova categoria alla select e selezionala
|
|
$('#category_id').append(new Option(nuovoValore, data.id, true, true)).trigger('change');
|
|
$('#nuovo_valore').val('').hide();
|
|
$('#nuovo_aggiungi').hide();
|
|
} else {
|
|
alert('Errore durante l\'aggiunta della categoria: ' + data.message);
|
|
}
|
|
}, 'json');
|
|
} else {
|
|
alert('Per favore, inserisci un nome valido per la nuova categoria.');
|
|
}
|
|
});
|
|
});
|