Skip to content

Instantly share code, notes, and snippets.

@MartelliEnrico
Created July 12, 2016 09:15
Show Gist options
  • Save MartelliEnrico/96919ba5343734ffe45dfa8cdec8da79 to your computer and use it in GitHub Desktop.
Save MartelliEnrico/96919ba5343734ffe45dfa8cdec8da79 to your computer and use it in GitHub Desktop.
TW - Appello 18 febbraio 2012
function restringiMenu(restrictions, menu) {
var finalMenu = { primi: [], secondi: [], dessert: [] };
for(var type in menu) {
plate:
for(var plate in menu[type]) {
for(var ingredient in menu[type][plate].ingredienti) {
for(var restriction in restrictions) {
if(!puoMangiare(restriction, ingredient)) continue plate;
}
}
finalMenu[type].push(plate);
}
}
return finalMenu;
}
function mostraScelte(menu) {
var getHtml = function(plates, type) {
var getInfo = function(plate) {
return {
code: plate.cod,
text: plate.nome + ' (' + plate.quantita + ')',
subt: plate.ingredienti.join(', ')
};
};
if(plates.count == 1) {
var info = getInfo(plates[0]);
return '<input type="hidden" name="' + type + '" value="' + info.code + '"><span title="' + info.subt + '">' + info.text + '</span>';
}
if(plates.count <= 3) {
var html = [];
for(var plate in plates) {
var info = getInfo(plate);
html.push('<input type="radio" name="' + type + '" value="' + info.code + '" id="' + info.code + '"><label for="' + info.code + '" title="' + info.subt + '">' + info.text + '</label>');
}
return html.join(' ');
}
var html = ['<select name="' + type + '">'];
for(var plate in plates) {
var info = getInfo(plate);
html.push('<option value="' + info.code + '" title="' + info.subt + '">' + info.text + '</option>');
}
html.push('</select>');
return html.join('\n');
};
// Suppongo che ogni opzione abbia uno span/div/p con id assegnato
$('#primo').html(getHtml(menu.primo, 'primo'));
$('#secondo').html(getHtml(menu.secondo, 'secondo'));
$('#dessert').html(getHtml(menu.dessert, 'dessert'));
}
function spedisciMenu(event) {
event.preventDefault();
$.ajax({
method: 'POST',
url: 'http://www.menuscolastico.it/mandamenu.php',
data: $(event.target).serialize(),
success: function() {
alert("Indicazioni del menù ricevute correttamente");
},
error: function() {
alert("C'è stato un errore nella spedizione delle indicazioni di menù. \nPer cortesia riprova");
}
});
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment