newscategory.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. layui.use([ 'laypage', 'layer', 'table', 'element','form','upload'], function(){
  2. var laypage = layui.laypage //分页
  3. ,layer = layui.layer //弹层
  4. ,table = layui.table //表格
  5. ,element = layui.element //元素操作
  6. ,form = layui.form
  7. ,upload = layui.upload;
  8. var tableIds = 'newsCategoryList';
  9. //第一个实例
  10. table.render({
  11. elem: '#'+tableIds
  12. ,url: '/dictionary/newsform' //数据接口
  13. ,where:{token:csrfToken}
  14. ,method:'post'
  15. ,page: true //开启分页
  16. ,text:'数据加载中'
  17. ,cols: [[ //表头
  18. // {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left',align:'center'},
  19. {field: 'news_name', title: '资讯名称',align:'center'}
  20. , {field: 'sort', title: '排序', width: 80, edit: 'text', align: 'center', sort: true, templet: "#sortBox"}
  21. ,{field: 'state', title: '审核',align:'center',width:200, templet:function (d) {
  22. if(d.state == 2){
  23. return '<input type="checkbox" value="'+d.id+'" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="|">'
  24. } else {
  25. return '<input type="checkbox" checked value="'+d.id+'" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="|">'
  26. }
  27. }}
  28. ,{field: 'img', title: '图片',width:200, align:'center',templet:function(d){
  29. return '<img class="layui_magnify" src="'+d.img+'"/>'
  30. }}
  31. // ,{field: 'recommend', title: '是否推荐', width:120,align:'center'}
  32. ,{field: 'update_at', title: '更新时间', width:150,align:'center'}
  33. ,{field: 'create_at', title: '添加时间', width:150,align:'center'}
  34. ,{fixed: 'right',title:'操作', width: 165, align:'center', toolbar: '#operate'}
  35. ]]
  36. });
  37. //列表操作
  38. table.on('tool('+tableIds+')', function(obj){
  39. var layEvent = obj.event,
  40. data = obj.data;
  41. if(layEvent === 'edit'){
  42. layer.open({
  43. type: 2,
  44. title: '修改户型',
  45. closeBtn: 1, //不显示关闭按钮
  46. shade: [0],
  47. area: ['600px', '500px'],
  48. anim: 2,
  49. content: ['/dictionary/newsedit?id='+data.id, 'yes'], //iframe的url,no代表不显示滚动条
  50. });
  51. } else if(layEvent === 'del') {
  52. layer.confirm('您确定要删除吗?',{ btn: ['确定', '取消']},function () {
  53. $.post('/dictionary/newsdel',{'id':data.id},function (data) {
  54. if(data.code == 200)
  55. {
  56. layer.msg(data.msg);
  57. table.reload(tableIds);
  58. } else if(data.code == 300 ) {
  59. layer.msg(data.msg);
  60. }
  61. })
  62. })
  63. }
  64. });
  65. //监听指定开关
  66. form.on('switch(switchTest)', function(data){
  67. layer.confirm('确定要修改此操作吗?',{btn: ['确定', '取消']},function () {
  68. $.post('/dictionary/newsstate',{id:data.value,type:'state'},function (data) {
  69. layer.msg(data.msg);
  70. table.reload('tablerReload');
  71. })
  72. },function (aa) {
  73. indexTtable.reload('tablerReload'); //重新加载
  74. })
  75. });
  76. //修改排序
  77. table.on('edit('+tableIds+')', function(obj){
  78. var data = obj.data;
  79. $.post('/dictionary/newssetsort',{id:data.id,sort:data.sort},function (data) {
  80. if(data.data != null){
  81. layer.msg(ReturnInfo(data.data),{icon:5});
  82. return false;
  83. }
  84. layer.msg(data.msg);
  85. })
  86. })
  87. var $ = layui.$, active = {
  88. addData: function(){ //获取选中数据
  89. layer.open({
  90. type: 2,
  91. title: '添加资讯分类',
  92. closeBtn: 1, //不显示关闭按钮
  93. shade: [0],
  94. area: ['600px', '400px'],
  95. anim: 2,
  96. content: ['/dictionary/newsadd', 'yes'], //iframe的url,no代表不显示滚动条
  97. });
  98. // var checkStatus = table.checkStatus('idTest')
  99. // ,data = checkStatus.data;
  100. // layer.alert(JSON.stringify(data));
  101. }
  102. };
  103. $('.demoTable .layui-btn').on('click', function(){
  104. var type = $(this).data('type');
  105. active[type] ? active[type].call(this) : '';
  106. });
  107. //修改
  108. form.on('submit(editnews)', function(data){
  109. // $.post('/dictionary/newseditform',{'data':data.field,'token':csrfToken},function (data) {
  110. // if(data.code == 200)
  111. // {
  112. // layer.msg(data.msg);
  113. // parent.layui.table.reload(tableIds);
  114. // TableClose();
  115. // } else if(data.code == 300 ) {
  116. // layer.msg(data.msg);
  117. // }
  118. // })
  119. // return false;
  120. var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8}); //数据提交提示
  121. // var oindex = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
  122. var formData = new FormData($('form')[0]);
  123. $.ajax({
  124. type : "post",
  125. url : "/dictionary/newseditform",
  126. data : formData,
  127. contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
  128. processData: false, //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
  129. error : function(request) {
  130. layer.close(index);
  131. layer.msg("请联系管理员");
  132. },
  133. success : function(data) {
  134. if(data.data != null){
  135. layer.msg(ReturnInfo(data.data),{icon:5});
  136. return ;
  137. }
  138. layer.msg(data.msg);
  139. parent.layer.close(oindex); //再执行关闭
  140. }
  141. });
  142. return false;
  143. });
  144. //添加图片上传
  145. upload.render({
  146. elem: '#test1'
  147. ,url: '/house/createimg'
  148. ,data:{token:csrfToken}
  149. ,field:'img'
  150. ,size:500 //限制上传图片的大小,单位为KB
  151. ,exts: 'png|jpg|jpeg' //只允许上传压缩文件
  152. ,auto:false
  153. ,bindAction:'#addcity'
  154. ,choose: function(obj){
  155. obj.preview(function(index, file, result){
  156. $('#breviary_img').attr('src', result); //图片链接(base64)
  157. });
  158. }
  159. });
  160. // 图片放大浏览
  161. $('.layui_magnify').click(function(){
  162. var _src = $(this).attr('src');
  163. layui.layer.open({
  164. type: 1,
  165. title: false, //是否显示标题
  166. area: ['60%', '80%'],
  167. shade: 0.6, //透明度
  168. closeBtn: 1, //按钮 1有,0无
  169. shadeClose: true,
  170. content: '<img style="width:100%;height:100%" src="'+_src+'">',
  171. });
  172. })
  173. //添加
  174. form.on('submit(addnews)', function(data){
  175. // $.post('/dictionary/newsaddform',{'data':data.field,'token':csrfToken},function (data) {
  176. // if(data.code == 200)
  177. // {
  178. // layer.msg(data.msg);
  179. // parent.layui.table.reload(tableIds);
  180. // } else if(data.code == 300 ) {
  181. // layer.msg(data.msg);
  182. // }
  183. // })
  184. // return false;
  185. var formData = new FormData($('form')[0]);
  186. var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8}); //数据提交提示
  187. $.ajax({
  188. cache : false,
  189. type : "post",
  190. url : "/dictionary/newsaddform",
  191. data : formData, // 你的formid
  192. contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
  193. processData: false, //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
  194. error : function(request) {
  195. layer.close(index);
  196. layer.msg("请联系管理员");
  197. },
  198. success : function(data) {
  199. if(data.data != null){
  200. layer.msg(ReturnInfo(data.data),{icon:5});
  201. return ;
  202. }
  203. layer.msg(data.msg);
  204. }
  205. });
  206. return false;
  207. });
  208. });