city.js 12 KB

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