frontend.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. layui.use([ 'laypage', 'layer', 'table', 'element','form','treetable','upload'], function(){
  2. var laypage = layui.laypage //分页
  3. ,$ = layui.jquery
  4. ,layer = layui.layer //弹层
  5. ,table = layui.table //表格
  6. ,element = layui.element //元素操作
  7. ,form = layui.form
  8. ,upload = layui.upload
  9. ,treetable = layui.treetable;
  10. if($('#menulist').attr('lay-filter') == 'menulist'){
  11. var renderTable = function (wheres = {}) {
  12. layer.load(2);
  13. treetable.render({
  14. treeColIndex:2,
  15. treeSpid:0,
  16. treeIdName: 'id',
  17. treePidName: 'pid',
  18. treeDefaultClose: true,
  19. treeLinkage: false,
  20. elem: '#menulist',
  21. url: '/frontend/indexfrom',
  22. page: false,
  23. where:wheres,
  24. id:'tablerReload',
  25. cols: [[
  26. {type:'numbers',title:'序号'}
  27. ,{field: 'id', title: 'ID', width:80, sort: true, align:'center'}
  28. ,{field: 'name', title: '菜单名称', align:'center'}
  29. ,{field: 'url', title: '地址', width:300,align:'center'}
  30. ,{field: 'icon', title: '图标', width:120,align:'center',templet:function(d){
  31. return '<img class="layui_magnify" src="'+d.icon+'"/>'
  32. }}
  33. ,{field: 'sort', title: '排序',width:80,edit: 'text',align:'center',sort: true,templet:"#sortBox"}
  34. ,{field: 'status', title: '状态',width:70, align:'center', templet:"#Toexamine",templet:function(d){
  35. if(d.status === '2'){
  36. return '<input type="checkbox" value="'+d.id+'" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="|">'
  37. } else {
  38. return '<input type="checkbox" checked value="'+d.id+'" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="|">'
  39. }
  40. }}
  41. ,{fixed: 'right',title:'操作', width: 200, align:'center', toolbar: '#operate'}
  42. ]],
  43. done: function () {
  44. layer.closeAll('loading');
  45. }
  46. });
  47. };
  48. renderTable();
  49. }
  50. // table.reload('tablerReload'); //重新加载
  51. //列表操作
  52. table.on('tool(menulist)', function(obj){
  53. var layEvent = obj.event,
  54. data = obj.data;
  55. switch (layEvent){
  56. case 'edit':
  57. layer.open({
  58. type: 2,
  59. title: '编辑',
  60. closeBtn: 1, //不显示关闭按钮
  61. shade: [0],
  62. area: ['600px', '500px'],
  63. anim: 2,
  64. content: '/frontend/edit?id='+data.id, //iframe的url,no代表不显示滚动条
  65. end:function () {
  66. renderTable();
  67. }
  68. });
  69. break;
  70. case 'del':
  71. layer.confirm('您确认要删除吗?',{btn:['确认','取消']},function () {
  72. $.post('/frontend/frontendmenusortedit',{'id':data.id,del:data.del,type:'del'},function (data) {
  73. if(data.code == 200)
  74. {
  75. layer.msg(data.msg);
  76. // table.reload('menulist');
  77. } else if(data.code == 300 ) {
  78. layer.msg(data.msg);
  79. }
  80. })
  81. })
  82. break;
  83. case 'addson':
  84. layer.open({
  85. type: 2,
  86. title: '添加子菜单',
  87. closeBtn: 1, //不显示关闭按钮
  88. shade: [0],
  89. area: ['600px', '100%'],
  90. anim: 2,
  91. content: '/frontend/create?id='+data.id+'&name='+data.name, //iframe的url,no代表不显示滚动条
  92. end:function () {
  93. renderTable();
  94. }
  95. });
  96. break;
  97. case 'produce':
  98. layer.open({
  99. type: 2,
  100. title: '生成['+data.name+']控制模板代码',
  101. closeBtn: 1, //不显示关闭按钮
  102. shade: [0],
  103. area: ['600px', '500px'],
  104. anim: 2,
  105. content: '/frontend/produce?id='+data.id+'&name='+data.name, //iframe的url,no代表不显示滚动条
  106. end:function () {
  107. renderTable();
  108. }
  109. });
  110. break;
  111. }
  112. });
  113. var $ = layui.$, active = {
  114. addData: function(){ //获取选中数据
  115. layer.open({
  116. type: 2,
  117. title: '添加导航菜单',
  118. closeBtn: 1, //不显示关闭按钮
  119. shade: [0],
  120. area: ['600px', '70%'],
  121. anim: 2,
  122. content: '/frontend/parentadd', //iframe的url,no代表不显示滚动条
  123. end:function () {
  124. renderTable();
  125. }
  126. });
  127. }
  128. };
  129. $('.demoTable .layui-btn').on('click', function(){
  130. var type = $(this).data('type');
  131. active[type] ? active[type].call(this) : '';
  132. });
  133. form.on('submit(editMenu)', function(data){
  134. var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8}); //数据提交提示
  135. var formData = new FormData($('form')[0]);
  136. $.ajax({
  137. cache : false,
  138. type : "post",
  139. url : "/frontend/editfrom",
  140. data : formData, // 你的formid
  141. contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
  142. processData: false, //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
  143. error : function(request) {
  144. layer.close(index);
  145. layer.msg("请联系管理员");
  146. },
  147. success : function(data) {
  148. if(data.data != null){
  149. layer.msg(ReturnInfo(data.data),{icon:5});
  150. return ;
  151. }
  152. layer.msg(data.msg);
  153. parent.layer.close(oindex); //再执行关闭
  154. //layer.close(index);
  155. }
  156. });
  157. return false;
  158. });
  159. //添加页面提交数据
  160. form.on('submit(addmenu)', function(data){
  161. layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8}); //数据提交提示
  162. var formData = new FormData($('form')[0]);
  163. /*if(formData.getAll('img')[0].size == 0){
  164. layer.msg('请选择图片');
  165. return false;
  166. }*/
  167. $.ajax({
  168. type : "post",
  169. url : "/frontend/createfrom",
  170. data : formData,
  171. contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
  172. processData: false, //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
  173. error : function(request) {
  174. layer.msg("请联系管理员");
  175. },
  176. success : function(data) {
  177. if(data.data != null){
  178. layer.msg(ReturnInfo(data.data),{icon:5});
  179. return ;
  180. }
  181. layer.msg(data.msg);
  182. parent.layer.close(oindex); //再执行关闭
  183. }
  184. });
  185. return false;
  186. });
  187. //监听指定开关
  188. form.on('switch(switchTest)', function(data){
  189. layer.confirm('确定要修改此操作吗?',{btn: ['确定', '取消']},function () {
  190. $.post('/frontend/frontendmenusortedit',{id:data.value,examineid:1,type:'status'},function (data) {
  191. if(data.code == 200)
  192. {
  193. layer.msg(data.msg);
  194. //table.reload('tablerReload');
  195. parent.layui.table.reload('menulist');
  196. } else if(data.code == 300 ) {
  197. layer.msg(data.msg);
  198. }
  199. })
  200. },function (aa) {
  201. // table.reload('tablerReload');
  202. parent.layui.table.reload('menulist'); //重新加载
  203. })
  204. });
  205. //添加图片上传
  206. upload.render({
  207. elem: '#test1'
  208. ,url: '/house/createimg'
  209. ,data:{token:csrfToken}
  210. ,field:'img'
  211. ,size:1000 //限制上传图片的大小,单位为KB
  212. ,exts: 'png|jpg|jpeg' //只允许上传压缩文件
  213. ,auto:false
  214. ,bindAction:'#addcity'
  215. ,choose: function(obj){
  216. obj.preview(function(index, file, result){
  217. $('#breviary_img').attr('src', result); //图片链接(base64)
  218. });
  219. }
  220. });
  221. /* 列表操作 edit 修改后执行 */
  222. table.on('edit(menulist)', function(obj){
  223. var data = obj.data;
  224. if(obj.field="sort"){
  225. $.post('/frontend/frontendmenusortedit',{id:data.id,sort:data.sort,type:'sort'},function (data) {
  226. if(data.code == 200)
  227. {
  228. layer.msg(data.msg);
  229. } else if(data.code == 300 ) {
  230. layer.msg(data.msg);
  231. }
  232. })
  233. }
  234. })
  235. });