city.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. layui.use(['laypage','layer', 'table', 'element','form','treetable'], function(){
  2. var laypage = layui.laypage //分页
  3. ,$ = layui.jquery
  4. ,layer = layui.layer //弹层
  5. ,table = layui.table //表格
  6. ,form = layui.form
  7. ,treetable = layui.treetable;
  8. var tableId = 'citylist';
  9. if($('#citylist').attr('lay-filter') == 'citylist'){
  10. var renderTable = function (wheres = {}) {
  11. layer.load(2);
  12. treetable.render({
  13. treeColIndex: 1,
  14. treeSpid:0,
  15. treeIdName: 'id',
  16. treePidName: 'pid',
  17. treeDefaultClose: true,
  18. treeLinkage: false,
  19. elem: '#citylist',
  20. url: '/categorycity/cityhomeform',
  21. page: false,
  22. where:wheres,
  23. id:'tablerReload',
  24. cols: [[
  25. {type:'numbers'}
  26. // {field: 'id', title: 'ID', width:80, sort: true,align:'center'}
  27. ,{field: 'city_name', title: '区域名称',align:'center'},
  28. {field: 'sort', title: '排序',width:80, edit: 'text',align:'center',sort: true,templet:"#sortBox"},
  29. // ,{field: 'sort', title: '排序',align:'center',sort: true},
  30. {field: 'state', title: '审核',width:100, align:'center', templet:"#Toexamine",templet:function(d){
  31. if(d.state == 2){
  32. return '<input type="checkbox" value="'+d.id+'" name="'+d.state+'" lay-skin="switch" lay-filter="switchTest" lay-text="|">'
  33. } else {
  34. return '<input type="checkbox" checked value="'+d.id+'" name="'+d.state+'" lay-skin="switch" lay-filter="switchTest" lay-text="|">'
  35. }
  36. }}
  37. ,{field: 'create_at', title: '添加时间', width:200,align:'center'}
  38. ,{fixed: 'right',title:'操作', width: 200, align:'center', toolbar: '#operate'}
  39. ]],
  40. done: function () {
  41. layer.closeAll('loading');
  42. }
  43. });
  44. };
  45. renderTable();
  46. }
  47. //列表操作
  48. table.on('tool('+tableId+')', function(obj){
  49. var layEvent = obj.event,
  50. data = obj.data;
  51. switch (layEvent){
  52. case 'edit':
  53. layer.open({
  54. type: 2,
  55. title: '修改区域',
  56. closeBtn: 1, //不显示关闭按钮
  57. shade: [0],
  58. area: ['780px', '550px'],
  59. anim: 2,
  60. content: ['/categorycity/edithome?id='+data.id, 'yes'],
  61. end:function(){
  62. renderTable();
  63. }
  64. });
  65. break;
  66. case 'del':
  67. // layer.confirm('您确定要删除吗?',{ btn: ['确定', '取消']},function () {
  68. // $.post('/categorycity/editall',{'id':data.id,'del':2},function (data) {
  69. // if(data.data != null){
  70. // layer.msg(ReturnInfo(data.data),{icon:5});
  71. // return false;
  72. // }
  73. // layer.msg(data.msg);
  74. // renderTable();
  75. // });
  76. // });
  77. layer.confirm('您确定要删除吗?',{ btn: ['确定', '取消']},function () {
  78. $.post('/categorycity/delcity',{id:data.id,type:'del'},function (data) {
  79. if(data.data != null){
  80. layer.msg(ReturnInfo(data.data),{icon:5});
  81. return false;
  82. }
  83. layer.msg(data.msg);
  84. renderTable();
  85. })
  86. });
  87. break;
  88. case 'addson':
  89. layer.open({
  90. type: 2,
  91. title: '添加区域',
  92. closeBtn: 1, //不显示关闭按钮
  93. shade: [0],
  94. area: ['780px', '550px'],
  95. anim: 2,
  96. content: '/categorycity/addhome?id='+data.id+'&name='+data.city_name,
  97. });
  98. break;
  99. }
  100. });
  101. var $ = layui.$, active = {
  102. addData: function(){ //获取选中数据
  103. layer.open({
  104. type: 2,
  105. title: '添加区域',
  106. closeBtn: 1, //不显示关闭按钮
  107. shade: [0],
  108. area: ['780px', '550px'],
  109. anim: 2,
  110. content: '/categorycity/addhome?son=1',
  111. });
  112. },
  113. reload:function () {
  114. var form = pfgs_serialize($('form'));
  115. var city = '';
  116. if(form.province != ''){
  117. city = form.province;
  118. }
  119. if(form.city != ''){
  120. city = form.city;
  121. }
  122. renderTable({id:city});
  123. }
  124. };
  125. $('.demoTable .layui-btn').on('click', function(){
  126. var type = $(this).data('type');
  127. active[type] ? active[type].call(this) : '';
  128. });
  129. //显示或隐藏
  130. form.on('switch(switchTest)', function(data){
  131. var state = '';
  132. if(data.elem.name == 1){
  133. state = 2;
  134. }else if(data.elem.name == 2){
  135. state = 1;
  136. }
  137. layer.confirm('确定要修改此操作吗?',{btn: ['确定', '取消']},function () {
  138. $.post('/categorycity/editall',{id:data.value,state:state},function (data) {
  139. if(data.data != null){
  140. layer.msg(ReturnInfo(data.data),{icon:5});
  141. return false;
  142. }
  143. layer.msg(data.msg);
  144. renderTable();
  145. })
  146. },function (aa) {
  147. renderTable();
  148. })
  149. });
  150. //修改排序
  151. table.on('edit('+tableId+')', function(obj){
  152. var data = obj.data;
  153. $.post('/categorycity/editall',{id:data.id,sort:data.sort},function (data) {
  154. if(data.data != null){
  155. layer.msg(ReturnInfo(data.data),{icon:5});
  156. return false;
  157. }
  158. layer.msg(data.msg);
  159. })
  160. })
  161. form.on('submit(editcity)', function(data){
  162. var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8}); //数据提交提示
  163. var formData = new FormData($('form')[0]);
  164. $.ajax({
  165. cache : true,
  166. type : "post",
  167. url : '/categorycity/edithomeform',
  168. data : formData, // 你的formid
  169. contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
  170. processData: false, //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
  171. error : function(request) {
  172. layer.close(index); //关闭数据提交信息
  173. layer.alert("网络超时");
  174. },
  175. success : function(data) {
  176. if(data.data != null){
  177. layer.msg(ReturnInfo(data.data),{icon:5});
  178. return ;
  179. }
  180. layer.msg(data.msg);
  181. renderTable();
  182. }
  183. });
  184. return false;
  185. });
  186. //添加
  187. form.on('submit(addcity)', function(data){
  188. var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8}); //数据提交提示
  189. $.post('/categorycity/addcityform',{'data':data.field},function (data) {
  190. if(data.data != null){
  191. layer.msg(ReturnInfo(data.data),{icon:5});
  192. return ;
  193. }
  194. layer.msg(data.msg);
  195. renderTable();
  196. })
  197. return false;
  198. });
  199. //加载省数据
  200. function loadProvince() {
  201. $.ajax({
  202. url : '/categorycity/citypid',
  203. type : 'post',
  204. async: false,//使用同步的方式,true为异步方式
  205. data : {'pid':0},//这里使用json对象
  206. success : function(data){
  207. // console.log(data);
  208. var areaData = data.data;
  209. var proHtml = '';
  210. for (var i = 0; i < areaData.length; i++) {
  211. proHtml += '<option value="' + areaData[i].id +'">' + areaData[i].city_name + '</option>';
  212. }
  213. //初始化省数据
  214. $('select[name=province]').append(proHtml);
  215. form.render();
  216. },
  217. fail:function(){
  218. }
  219. });
  220. }
  221. loadProvince();
  222. form.on('select(province)',function (data) {
  223. $.post('/categorycity/citypid',{'pid':data.value},function (data) {
  224. var areaData = data.data;
  225. var proHtml = '';
  226. $('select[name=city]').html('');
  227. proHtml = '<option value="">请选择市/区/县</option>';
  228. for (var i = 0; i < areaData.length; i++) {
  229. proHtml += '<option value="' + areaData[i].id +'">' + areaData[i].city_name + '</option>';
  230. }
  231. //初始化省数据
  232. $('select[name=city]').append(proHtml);
  233. form.render();
  234. })
  235. })
  236. // 转换拼音
  237. $('.layui_w_name').on('keyup ',function(){
  238. var _txt = $(this).val();
  239. var _convert = pinyinUtil.getPinyin(_txt,' ',false).replace(/\s/g, "");
  240. $('.layui_w_spell').val(_convert)
  241. })
  242. });