Created
April 18, 2018 20:23
-
-
Save KeySeeDev/6f81e3ab526c126caeeb5504c7ad839b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generateForm(selector, data) { | |
var $editform = $('<form method="POST"></form>'); | |
$editform.html(''); | |
$.each(data, function(key, elm) { | |
if (elm.label || elm.is_remote) { | |
var label = elm.label ? elm.label : key; | |
var value = typeof elm.value == 'undefined' ? elm.default : elm.value; | |
var description = elm.description ? '<p>' + elm.description + '</p>' : ''; | |
var $input; | |
switch (elm.type) { | |
case 'checkbox': | |
case 'truefalse': | |
var checked = value ? 'checked="checked"' : ''; | |
$input = '<label class="checkbox"><input type="checkbox" ' + checked + ' name="' + key + '" value="' + value + '"><b>' + label + '</b>' + description + '</label>'; | |
break; | |
case 'textarea': | |
$input = '<label><b>' + label + '</b>' + description + '<textarea name="' + key + '">' + value + '</textarea></label>'; | |
break; | |
case 'radio': | |
$input = '<div class="options ' + key + '"><label><b>' + label + '</b>' + description + '</label>'; | |
$.each(elm.values, function(i, option) { | |
if (typeof option == 'object') { | |
$input += '<label class="option' + (option.class ? ' ' + option.class : '') + '">'; | |
var checked = (option.value == value ? 'checked="checked"' : ''); | |
$input += '<input type="radio" ' + checked + ' name="' + key + '" value="' + option.value + '"/><b>' + option.label + '</b>'; | |
$input += '</label>'; | |
} else { | |
$input += '<label class="option' + (option.class ? ' ' + option.class : '') + '">'; | |
var checked = (option == value ? 'checked="checked"' : ''); | |
$input += '<input type="radio" ' + checked + ' name="' + key + '" value="' + option + '"/><b>' + option + '</b>'; | |
$input += '</label>'; | |
} | |
}); | |
$input += '</div>'; | |
break; | |
case 'select': | |
$input = '<div class="select"><label><b>' + label + '</b>' + description + '<select name="' + key + '">'; | |
$.each(elm.values, function(i, option) { | |
var selected = (option == value ? 'selected' : ''); | |
$input += '<option value="' + option + '" ' + selected + '>' + option + '</option>'; | |
}); | |
$input += '</select></label>'; | |
break; | |
case 'hidden': | |
$input = '<input name="' + key + '" value="' + value + '">'; | |
break; | |
default: | |
$input = '<label><b>' + label + '</b>' + description + '<input type="text" value="' + value + '" name="' + key + '" value="value"></label>'; | |
} | |
$editform.append($input); | |
} | |
}); | |
$editform.append('<div class="save"><input class="submit" type="submit" value="Salva" /></div>'); | |
$(selector).append($editform); | |
} | |
generateForm('body', { | |
radio: { | |
values: ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio"], | |
type: 'radio', | |
disabled: true, | |
label: 'Radio', | |
}, | |
textarea: { | |
default: 'Lorem ipsum dolor sit ament ipsum dolor sit ament ipsum dolor sit ament ipsum dolor sit ament ipsum dolor sit ament', | |
type: 'textarea', | |
disabled: true, | |
label: 'Textarea', | |
}, | |
select: { | |
values: ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio"], | |
type: 'select', | |
disabled: true, | |
label: 'Select' | |
}, | |
lastupdate: { | |
value: new Date().getTime(), | |
type: 'hidden' | |
}, | |
enable_gps_tracing: { | |
label: 'Tracciamento GPS', | |
default: true, | |
type: 'truefalse', | |
disabled: false, | |
description: 'Abilita il monitoraggio della posizione GPS', | |
is_remote: false | |
}, | |
map_update_interval: { | |
default: 10, | |
type: 'input:number', | |
disabled: true, | |
label: 'Frequenza aggiornamento', | |
description: 'Intervallo in secondi per ogni aggiornamento', | |
is_remote: false | |
}, | |
emergency_call: { | |
default: '0656030596', | |
placeholder: "+39 333 333 33 33", | |
type: 'input:number', | |
disabled: true, | |
label: 'Numero emergenza', | |
description: 'Numero da chiamare dopo avere premuto "Emergenza"', | |
is_remote: true | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment