hw11_07_ .html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <head>_____</head>
  2. <body>
  3. <script>
  4. function getSetForm(parent, getSet) {
  5. const typesMap = { 'boolean': 'checkbox', 'number': 'number', 'string': 'text' };
  6. const inputs = {} //реестр
  7. const updateInputs = () => {
  8. for (key in inputs) {
  9. getName = "get" + key;
  10. getFunc = getSet[getName];
  11. if (getFunc) {
  12. inputs[key].value = getFunc();
  13. }
  14. }//функция обновления полей ввода согласно всяким get....
  15. //тут должен быть перебор
  16. }
  17. for (const getSetName in getSet) {
  18. const isGet = getSetName.startsWith("get");//первые три буквы переменной getSetName. Так же можно использовать флаг isGet, который будет равен true или false
  19. const fieldName = getSetName.substring(3);//остальные буквы getSetName - типа "Name" или "FullName"
  20. //const setKey = `set` + fieldName
  21. //const getKey = `get` + fieldName
  22. input = inputs[fieldName];
  23. if (!input) {
  24. input = document.createElement("input");
  25. inputs[fieldName] = input;
  26. input.placeholder = fieldName;
  27. input.disabled = true;
  28. parent.append(input);
  29. }
  30. if (isGet) {
  31. value = getSet[getSetName]();
  32. type = typeof value;
  33. htmlType = typesMap[type];
  34. input.type = htmlType;
  35. input.value = value;
  36. }
  37. else {
  38. input.disabled = false;
  39. input.oninput = () => {
  40. getSet[getSetName](input.value);
  41. updateInputs();
  42. }
  43. }
  44. console.log(input);
  45. /* const typesMap = { 'boolean': 'checkbox', 'number': 'number', 'string': 'text' };
  46. let str = "<form>\n";
  47. for (var name in obj) {
  48. value = obj[name];
  49. type = typeof value;
  50. htmlType = typesMap[type];
  51. str += ` <label>${name}: <input type="${htmlType}" value="${value}" /></label>\n`;
  52. }*/
  53. //допишите тут все шо надо, и не только тут
  54. }
  55. updateInputs()
  56. }
  57. let car;
  58. {
  59. let brand = 'BMW', model = 'X5', volume = 2.4
  60. car = {
  61. getBrand() {
  62. return brand
  63. },
  64. setBrand(newBrand) {
  65. if (newBrand && typeof newBrand === 'string') {
  66. brand = newBrand
  67. }
  68. return brand
  69. },
  70. getModel() {
  71. return model
  72. },
  73. setModel(newModel) {
  74. if (newModel && typeof newModel === 'string') {
  75. model = newModel
  76. }
  77. return model
  78. },
  79. getVolume() {
  80. return volume
  81. },
  82. setVolume(newVolume) {
  83. if (newVolume && typeof newVolume === 'number' && newVolume > 0 && newVolume < 20) {
  84. volume = newVolume
  85. }
  86. return volume
  87. },
  88. getTax() {
  89. return volume * 100
  90. }
  91. }
  92. }
  93. getSetForm(document.body, car);
  94. </script>
  95. </body>