modal.blade.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <script id="tools_tpl" type="text/html">
  2. <div class="layui-form-item @{{if type=='textarea' }} layui-form-text @{{/if}}" @{{if type=="hidden"}}
  3. style="display:none" @{{/if}}>
  4. <label class="layui-form-label tool_label"> ${introwords}: </label>
  5. <div class="layui-input-inline tool_input">@{{html chosehtml}}</div>
  6. </div>
  7. </script>
  8. <script type="text/javascript">
  9. /**
  10. * 所有页面中的ajax请求都走这里(table除外),不要自定义
  11. * tools_add 弹出一个模态框,做新增,修改
  12. * 修改页面在modules中加入一个hidden字段,作为编辑的key
  13. * form_jump 请求后 执行跳转(可以不跳)
  14. * form_func 请求后 执行func函数
  15. *
  16. * confirm_opt 确认提示框
  17. * 传入2个函数作为 确定 和 取消后的动作(取消可以不传)
  18. */
  19. function tools_add(title, modules, url, func) {
  20. swal({
  21. title: title,
  22. focusConfirm: false,
  23. showCancelButton: true,
  24. confirmButtonText:'确认',
  25. cancelButtonText: '取消',
  26. width: 480,
  27. html: '<form class="layui-form" id="tools_form">' + '</form>',
  28. onOpen: () => {
  29. var dataItems = parse_module(modules);
  30. $('#tools_form').html($("#tools_tpl").tmpl(dataItems));
  31. layui.use(['table', 'layer', 'laydate', 'form'], function () {
  32. var table = layui.table,
  33. laydate = layui.laydate,
  34. form = layui.form;
  35. laydate.render({
  36. elem: '#date'
  37. });
  38. laydate.render({
  39. elem: '#datetime',type:'datetime'
  40. });
  41. //全选
  42. form.render();
  43. for (var i in modules) {
  44. var moduel = modules[i];
  45. if (moduel[1] == 'checkbox') {
  46. form.on('checkbox(all)', function (obj) {
  47. console.log(i);
  48. $('input[name="' + i + '[]" ]').prop('checked', obj.elem.checked);
  49. form.render('checkbox');
  50. });
  51. form.on('checkbox(check_item)', function (obj) {
  52. var total = $(obj.elem).parent().find('input[name="' + i + '[]"]').length;
  53. var checked_total = $(obj.elem).parent().find('input[name="' + i + '[]"]:checked').length;
  54. if (total == checked_total) {
  55. $("#all").prop('checked', true);
  56. form.render();
  57. } else {
  58. $("#all").prop('checked', false);
  59. form.render();
  60. }
  61. });
  62. break;
  63. }
  64. }
  65. });
  66. },
  67. preConfirm: () => {
  68. var arr = $('#tools_form').serialize();
  69. return arr;
  70. }
  71. }).then(function (res) {
  72. if (res.value) {
  73. form_func(url, res.value, func);
  74. }
  75. }).catch(() => {
  76. });
  77. }
  78. function tools_html(title, html, url, func) {
  79. $.ajax({
  80. url: html,
  81. type: "POST",
  82. dataType: "html",
  83. success: function (result) {
  84. swal({
  85. title: title,
  86. focusConfirm: false,
  87. showCancelButton: true,
  88. cancelButtonText: 'cancel',
  89. width: 450,
  90. html: result,
  91. onOpen: () => {
  92. layui.use(['table', 'layer', 'laydate', 'form'], function () {
  93. var table = layui.table,
  94. laydate = layui.laydate,
  95. form = layui.form;
  96. laydate.render({
  97. elem: '#date'
  98. });
  99. //全选
  100. form.render();
  101. });
  102. },
  103. preConfirm: () => {
  104. var arr = $('#tools_form').serialize();
  105. return arr;
  106. }
  107. }).then(function (res) {
  108. if (res.value) {
  109. form_func(url, res.value, func);
  110. // console.log(res.value);
  111. }
  112. }).catch(() => {
  113. });
  114. }
  115. });
  116. }
  117. function tools_download(title, modules, url) {
  118. swal({
  119. title: title,
  120. focusConfirm: false,
  121. showCancelButton: true,
  122. cancelButtonText: 'cancel',
  123. width: 450,
  124. html: '<form class="layui-form" id="tools_form">' + '</form>',
  125. onOpen: () => {
  126. var dataItems = parse_module(modules);
  127. $('#tools_form').html($("#tools_tpl").tmpl(dataItems));
  128. layui.use(['table', 'layer', 'laydate', 'form'], function () {
  129. var table = layui.table,
  130. laydate = layui.laydate,
  131. form = layui.form;
  132. laydate.render({
  133. elem: '#date'
  134. });
  135. laydate.render({
  136. elem: '#datetime',type:'datetime'
  137. });
  138. //全选
  139. form.render();
  140. for (var i in modules) {
  141. var moduel = modules[i];
  142. if (moduel[1] == 'checkbox') {
  143. form.on('checkbox(all)', function (obj) {
  144. console.log(i);
  145. $('input[name="' + i + '[]" ]').prop('checked', obj.elem.checked);
  146. form.render('checkbox');
  147. });
  148. form.on('checkbox(check_item)', function (obj) {
  149. var total = $(obj.elem).parent().find('input[name="' + i + '[]"]').length;
  150. var checked_total = $(obj.elem).parent().find('input[name="' + i + '[]"]:checked').length;
  151. if (total == checked_total) {
  152. $("#all").prop('checked', true);
  153. form.render();
  154. } else {
  155. $("#all").prop('checked', false);
  156. form.render();
  157. }
  158. });
  159. break;
  160. }
  161. }
  162. });
  163. },
  164. preConfirm: () => {
  165. var arr = $('#tools_form').serialize();
  166. return arr;
  167. }
  168. }).then(function (res) {
  169. if (res.value) {
  170. var href = url+'?'+res.value;
  171. console.log(href);
  172. window.location.href = href;
  173. }
  174. }).catch(() => {
  175. });
  176. }
  177. function parse_module(modules) {
  178. var dataItems = [];
  179. for (var index in modules) {
  180. var chosehtml = '';
  181. var module = modules[index];
  182. switch (module[1]) {
  183. case 'radio':
  184. var i = 0;
  185. for (var key in module[2][0]) {
  186. if (module[3] == key) {
  187. checkhtml = 'checked';
  188. } else {
  189. var checkhtml = i == 0 ? 'checked' : '';
  190. }
  191. var disable = module[4]?'disabled':'';
  192. chosehtml += '<input '+disable+' class="layui-input" ' + checkhtml + ' name="' + index + '" type="radio" value="' + key + '" title="' + module[2][0][key] + '" />';
  193. i++;
  194. }
  195. break;
  196. case 'checkbox':
  197. chosehtml = '<input type="checkbox" lay-skin="primary" id="all" title="全选" lay-filter="all">';
  198. var i = 0;
  199. for (var key in module[2][0]) {
  200. var checkboxhtml = '';
  201. for (var j in module[3]) {
  202. if (module[3][j]!=key){
  203. continue
  204. }else {
  205. checkboxhtml = 'checked';
  206. }
  207. }
  208. chosehtml += '<input type="checkbox" ' + checkboxhtml + ' name="' + index + '[]" value="' + key + '" lay-skin="primary" lay-filter="check_item" title="' + module[2][0][key] + '" />';
  209. i++;
  210. }
  211. break;
  212. case 'switch':
  213. chosehtml = '<input type="checkbox" name="' + index + '" value="' + module[2] + '" lay-skin="switch" lay-text="' + module[3] + '">';
  214. break;
  215. case 'select':
  216. chosehtml = '<select name="' + index + '">';
  217. if (module[4]) {
  218. console.log('disable')
  219. chosehtml = '<select name="' + index + ' " disabled>';
  220. }
  221. chosehtml +='<option value="">请选择</option>';
  222. for (var key in module[2][0]) {
  223. if (module[3] == key) {
  224. var selectedhtml = 'selected="selected"';
  225. } else {
  226. var selectedhtml = '';
  227. }
  228. chosehtml += '<option ' + selectedhtml + ' value="' + key + '">' + module[2][0][key] + '</option>';
  229. i++;
  230. }
  231. chosehtml += '</select>';
  232. break;
  233. case 'date':
  234. if(module[2] == 'null'){
  235. module[2] = '';
  236. }
  237. chosehtml = '<input type="text" name="' + index + '" value="' + module[2] + '" id="date" lay-verify="date" placeholder="请输入时间" autocomplete="off" class="layui-input">';
  238. break;
  239. case 'datetime':
  240. if(module[2] == 'null'){
  241. module[2] = '';
  242. }
  243. chosehtml = '<input type="text" name="' + index + '" value="' + module[2] + '" id="datetime" lay-verify="date" placeholder="请输入时间" autocomplete="off" class="layui-input">';
  244. break;
  245. case 'text':
  246. if (module[3]){
  247. chosehtml = '<input class="layui-input" name="' + index + '" value="' + module[2] + '" placeholder="'+ module[3] +'" autocomplete="off"/>';
  248. }else{
  249. chosehtml = '<input class="layui-input" name="' + index + '" value="' + module[2] + '" />';
  250. }
  251. break;
  252. case 'textarea':
  253. if(module[2] == 'null'){
  254. module[2] = '';
  255. }
  256. chosehtml = '<textarea name="' + index + '" placeholder="' + module[3] + '" class="layui-textarea">' + module[2] + '</textarea>';
  257. break;
  258. case 'password':
  259. chosehtml = '<input type="password" name="' + index + '" value="" class="layui-input" placeholder="' + module[3] + '" autocomplete="new-password"/>';
  260. break;
  261. case 'hidden':
  262. chosehtml = '<input type="hidden" name="' + index + '" value="' + module[2] + '" />';
  263. break;
  264. case 'span':
  265. chosehtml = '<span style="display: block;margin:9px 10px;font-size: 14px;">' + module[2] + '</span>';
  266. break;
  267. }
  268. var dataItem = {'introwords': module[0], 'chosehtml': chosehtml, 'type': module[1]};
  269. dataItems.push(dataItem);
  270. }
  271. return dataItems;
  272. }
  273. </script>