design.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. layui.define(['layer', 'form'], function(exports) {
  2. var layer = layui.layer,
  3. form = layui.form,
  4. $ = layui.$,
  5. key = '';
  6. delHtml()
  7. $('button').on('click', function() {
  8. var _this = $(this),
  9. size = _this.data('size'),
  10. type = _this.data('type'),
  11. html = '';
  12. key = randStrName();
  13. switch (type) {
  14. case 'text':
  15. html = input(type, size)
  16. break;
  17. case 'password':
  18. html = input(type, size)
  19. break;
  20. case 'select':
  21. html = select(size)
  22. break;
  23. case 'checkbox_a':
  24. html = checkbox_a(size)
  25. break;
  26. case 'checkbox_b':
  27. html = checkbox_b(size)
  28. break;
  29. case 'radio':
  30. html = radio(size)
  31. break;
  32. case 'textarea':
  33. html = textarea(size)
  34. break;
  35. case 'submit':
  36. html = submits(size)
  37. break;
  38. case 'del':
  39. $('form').html("\n")
  40. delHtml()
  41. $('.code-show').text('')
  42. return false
  43. break;
  44. default:
  45. layer.msg('类型错误', {
  46. icon: 2
  47. })
  48. }
  49. $('form').append(html);
  50. form.render();
  51. setHtml(html)
  52. })
  53. function delHtml() {
  54. layui.data('form_html', {
  55. key: 'html',
  56. remove: true
  57. });
  58. }
  59. function setHtml(html) {
  60. var h = layui.data('form_html');
  61. if (h && h.html) {
  62. var _d = h.html + html
  63. } else {
  64. var _d = html
  65. }
  66. layui.data('form_html', {
  67. key: 'html',
  68. value: _d
  69. })
  70. $('.code-show').text('<form class="layui-form" action="" onsubmit="return false">\n' + _d + '</form>')
  71. }
  72. function input(type, size) {
  73. var name = type === 'text' ? '输入框' : (type === 'password' ? '密码框' : '');
  74. var html = ' <div class="layui-form-item">\n' +
  75. ' <label class="layui-form-label">' + name + '</label>\n' +
  76. ' <div class="layui-input-' + size + '">\n' +
  77. ' <input type="' + type + '" name="' + key + '" required lay-verify="required" placeholder="请输入' + name +
  78. '内容" autocomplete="off" class="layui-input">\n' +
  79. ' </div>\n' +
  80. ' </div>\n';
  81. return html;
  82. }
  83. function select(size) {
  84. var html = ' <div class="layui-form-item">\n' +
  85. ' <label class="layui-form-label">选择框</label>\n' +
  86. ' <div class="layui-input-' + size + '">\n' +
  87. ' <select name="' + key + '" lay-verify="required" lay-search>\n' +
  88. ' <option value=""></option>\n' +
  89. ' <option value="0">北京</option>\n' +
  90. ' <option value="1">上海</option>\n' +
  91. ' <option value="2">广州</option>\n' +
  92. ' <option value="3">深圳</option>\n' +
  93. ' <option value="4">杭州</option>\n' +
  94. ' </select>\n' +
  95. ' </div>\n' +
  96. ' </div>\n';
  97. return html;
  98. }
  99. function checkbox_a(size) {
  100. var html = ' <div class="layui-form-item">\n' +
  101. ' <label class="layui-form-label">复选框</label>\n' +
  102. ' <div class="layui-input-' + size + '">\n' +
  103. ' <input type="checkbox" name="' + key + '[]" title="写作">\n' +
  104. ' <input type="checkbox" name="' + key + '[]" title="阅读" checked>\n' +
  105. ' <input type="checkbox" name="' + key + '[]" title="发呆">\n' +
  106. ' </div>\n' +
  107. ' </div>\n';
  108. return html;
  109. }
  110. function checkbox_b(size) {
  111. var html = ' <div class="layui-form-item">\n' +
  112. ' <label class="layui-form-label">开关</label>\n' +
  113. ' <div class="layui-input-' + size + '">\n' +
  114. ' <input type="checkbox" name="' + key + '" lay-skin="switch">\n' +
  115. ' </div>\n' +
  116. ' </div>\n';
  117. return html;
  118. }
  119. function radio(size) {
  120. var html = ' <div class="layui-form-item">\n' +
  121. ' <label class="layui-form-label">单选框</label>\n' +
  122. ' <div class="layui-input-' + size + '">\n' +
  123. ' <input type="radio" name="' + key + '" value="男" title="男">\n' +
  124. ' <input type="radio" name="' + key + '" value="女" title="女" checked>\n' +
  125. ' </div>\n' +
  126. ' </div>\n';
  127. return html;
  128. }
  129. function textarea(size) {
  130. var html = ' <div class="layui-form-item layui-form-text">\n' +
  131. ' <label class="layui-form-label">文本域</label>\n' +
  132. ' <div class="layui-input-' + size + '">\n' +
  133. ' <textarea name="' + key + '" placeholder="请输入内容" class="layui-textarea"></textarea>\n' +
  134. ' </div>\n' +
  135. ' </div>\n';
  136. return html;
  137. }
  138. function submits(size) {
  139. var html = ' <div class="layui-form-item">\n' +
  140. ' <div class="layui-input-' + size + '">\n' +
  141. ' <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>\n' +
  142. ' <button type="reset" class="layui-btn layui-btn-primary">重置</button>\n' +
  143. ' </div>\n' +
  144. ' </div>\n';
  145. return html;
  146. }
  147. function jscode() {
  148. var html = '<script>\n' +
  149. ' layui.use(\'form\', function(){\n' +
  150. ' var form = layui.form;\n' +
  151. ' form.on(\'submit(formDemo)\', function(data){\n' +
  152. ' layer.msg(JSON.stringify(data.field));\n' +
  153. ' return false;\n' +
  154. ' });\n' +
  155. ' });\n' +
  156. '</script>';
  157. return html;
  158. }
  159. function randStrName() {
  160. return Math.random().toString(36).substr(8);
  161. }
  162. var jscodehtml = jscode();
  163. $('.js-show').text(jscodehtml)
  164. form.on('submit(formDemo)', function(data) {
  165. layer.msg(JSON.stringify(data.field));
  166. return false;
  167. });
  168. exports('design', {});
  169. });