Part2(Form).html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Form</title>
  6. </head>
  7. <body>
  8. <div id="formContainer">
  9. </div>
  10. <script>
  11. function Form(el, data, okCallback, cancelCallback){
  12. let formBody = document.createElement('div')
  13. let okButton = document.createElement('button')
  14. okButton.innerHTML = 'OK'
  15. let cancelButton = document.createElement('button')
  16. cancelButton.innerHTML = 'Cancel'
  17. formBody.innerHTML = '<h1>тут будет форма после перервы</h1>';
  18. let inputCreators = {
  19. String(key, value, oninput){
  20. let input = document.createElement('input')
  21. input.type = 'text'
  22. input.placeholder = key
  23. input.value = value
  24. input.oninput = () => oninput(input.value)
  25. return input;
  26. },
  27. Boolean(key, value, oninput){
  28. //label for с input type='checkbox' внутри
  29. let input = document.createElement('input')
  30. input.type = 'text'
  31. input.placeholder = key
  32. input.value = value
  33. input.oninput = () => oninput(input.value)
  34. return input;
  35. },
  36. Date(key, value, oninput){
  37. //используйте datetime-local
  38. let input = document.createElement('input')
  39. input.type = 'text'
  40. input.placeholder = key
  41. input.value = value
  42. input.oninput = () => oninput(input.value)
  43. return input;
  44. },
  45. }
  46. for(let item in data){
  47. const table=document.createElement('table');
  48. let tr=document.createElement('tr');
  49. let th=document.createElement('th');
  50. let p=document.createElement('p');
  51. p.innerText=item+":";
  52. th.append(p);
  53. tr.append(th);
  54. let td=document.createElement('td');
  55. let input=inputCreators[data[item].constructor.name](item,data[item],value=>{
  56. data[item]=value;
  57. })
  58. td.append(input);
  59. tr.append(td);
  60. table.append(tr);
  61. formBody.append(table);
  62. }
  63. if(typeof okCallback === 'function'){
  64. formBody.appendChild(okButton);
  65. okButton.onclick = (e) => {
  66. console.log(this);
  67. this.okCallback(e);
  68. }
  69. }
  70. if (typeof cancelCallback === 'function'){
  71. formBody.appendChild(cancelButton);
  72. cancelButton.onclick = cancelCallback;
  73. }
  74. el.appendChild(formBody);
  75. this.okCallback = okCallback;
  76. this.cancelCallback = cancelCallback;
  77. this.data = data;
  78. this.validators = {};
  79. }
  80. let form = new Form(formContainer, {
  81. name: 'Anakin',
  82. surname: 'Skywalker',
  83. married: true,
  84. birthday: new Date((new Date).getTime() - 86400000 * 30*365)
  85. }, () => console.log('ok'),() => console.log('cancel') );
  86. form.okCallback = () => console.log('ok2');
  87. /*form.validators.surname = (value, key, data, input) => value.length > 2 && value[0].toUpperCase() == value[0] && !value.includes(' ') ? true : 'Wrong name';*/
  88. /*if(item=='married'){
  89. let check=document.createElement('input');
  90. check.type='checkbox';
  91. if(data[item]==true){
  92. check.checked=true;
  93. }else{
  94. check.checked=false;
  95. }
  96. td.append(check);
  97. }else{*/
  98. </script>
  99. </body>
  100. </html>