Guest User

Untitled

a guest
Apr 26th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. function generateForm(selector, data) {
  2. var $editform = $('<form method="POST"></form>');
  3. $editform.html('');
  4. $.each(data, function(key, elm) {
  5. if (elm.label || elm.is_remote) {
  6. var label = elm.label ? elm.label : key;
  7. var value = typeof elm.value == 'undefined' ? elm.default : elm.value;
  8. var description = elm.description ? '<p>' + elm.description + '</p>' : '';
  9. var $input;
  10. switch (elm.type) {
  11. case 'checkbox':
  12. case 'truefalse':
  13. var checked = value ? 'checked="checked"' : '';
  14. $input = '<label class="checkbox"><input type="checkbox" ' + checked + ' name="' + key + '" value="' + value + '"><b>' + label + '</b>' + description + '</label>';
  15. break;
  16. case 'textarea':
  17. $input = '<label><b>' + label + '</b>' + description + '<textarea name="' + key + '">' + value + '</textarea></label>';
  18. break;
  19. case 'radio':
  20. $input = '<div class="options ' + key + '"><label><b>' + label + '</b>' + description + '</label>';
  21. $.each(elm.values, function(i, option) {
  22. if (typeof option == 'object') {
  23. $input += '<label class="option' + (option.class ? ' ' + option.class : '') + '">';
  24. var checked = (option.value == value ? 'checked="checked"' : '');
  25. $input += '<input type="radio" ' + checked + ' name="' + key + '" value="' + option.value + '"/><b>' + option.label + '</b>';
  26. $input += '</label>';
  27. } else {
  28. $input += '<label class="option' + (option.class ? ' ' + option.class : '') + '">';
  29. var checked = (option == value ? 'checked="checked"' : '');
  30. $input += '<input type="radio" ' + checked + ' name="' + key + '" value="' + option + '"/><b>' + option + '</b>';
  31. $input += '</label>';
  32. }
  33. });
  34. $input += '</div>';
  35. break;
  36. case 'select':
  37. $input = '<div class="select"><label><b>' + label + '</b>' + description + '<select name="' + key + '">';
  38. $.each(elm.values, function(i, option) {
  39. var selected = (option == value ? 'selected' : '');
  40. $input += '<option value="' + option + '" ' + selected + '>' + option + '</option>';
  41. });
  42. $input += '</select></label>';
  43. break;
  44. case 'hidden':
  45. $input = '<input name="' + key + '" value="' + value + '">';
  46. break;
  47. default:
  48. $input = '<label><b>' + label + '</b>' + description + '<input type="text" value="' + value + '" name="' + key + '" value="value"></label>';
  49. }
  50. $editform.append($input);
  51. }
  52. });
  53. $editform.append('<div class="save"><input class="submit" type="submit" value="Salva" /></div>');
  54. $(selector).append($editform);
  55. }
  56.  
  57. generateForm('body', {
  58. radio: {
  59. values: ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio"],
  60. type: 'radio',
  61. disabled: true,
  62. label: 'Radio',
  63. },
  64. textarea: {
  65. default: 'Lorem ipsum dolor sit ament ipsum dolor sit ament ipsum dolor sit ament ipsum dolor sit ament ipsum dolor sit ament',
  66. type: 'textarea',
  67. disabled: true,
  68. label: 'Textarea',
  69. },
  70. select: {
  71. values: ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio"],
  72. type: 'select',
  73. disabled: true,
  74. label: 'Select'
  75. },
  76. lastupdate: {
  77. value: new Date().getTime(),
  78. type: 'hidden'
  79. },
  80. enable_gps_tracing: {
  81. label: 'Tracciamento GPS',
  82. default: true,
  83. type: 'truefalse',
  84. disabled: false,
  85. description: 'Abilita il monitoraggio della posizione GPS',
  86. is_remote: false
  87. },
  88. map_update_interval: {
  89. default: 10,
  90. type: 'input:number',
  91. disabled: true,
  92. label: 'Frequenza aggiornamento',
  93. description: 'Intervallo in secondi per ogni aggiornamento',
  94. is_remote: false
  95. },
  96. emergency_call: {
  97. default: '0656030596',
  98. placeholder: "+39 333 333 33 33",
  99. type: 'input:number',
  100. disabled: true,
  101. label: 'Numero emergenza',
  102. description: 'Numero da chiamare dopo avere premuto "Emergenza"',
  103. is_remote: true
  104. }
  105. });
Add Comment
Please, Sign In to add comment