brand.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. layui.use(['laypage', 'layer', 'table', 'element', 'form', '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. var tableIds = 'brandlist'; //表格ID
  10. //第一个实例
  11. table.render({
  12. elem: '#' + tableIds
  13. , url: '/brand/brandform' //数据接口
  14. , where: {token: csrfToken}
  15. , method: 'post'
  16. , page: true //开启分页
  17. , text: '数据加载中'
  18. , id: 'tablerReload'
  19. , limit: 20
  20. , cols: [[ //表头
  21. {type: 'numbers', title: '序号', width: 50},
  22. {field: 'name', title: '品牌', align: 'center'},
  23. {
  24. field: 'img', title: '品牌图片', width: 300, align: 'center', templet: function (d) {
  25. return '<img class="layui_magnify" src="' + d.img + '"/>'
  26. }
  27. },
  28. {field: 'create_at', title: '添加时间', width: 150, align: 'center'},
  29. {
  30. field: 'sort',
  31. title: '排序',
  32. width: 100,
  33. event: 'sortData',
  34. edit: 'text',
  35. align: 'center',
  36. sort: true,
  37. templet: "#sortBox"
  38. },
  39. {
  40. field: 'is_show', title: '审核', align: 'center', width: 100, templet: function (d) {
  41. if (d.is_show == 2) {
  42. return '<input type="checkbox" value="' + d.id + '" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="|">'
  43. } else {
  44. return '<input type="checkbox" checked value="' + d.id + '" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="|">'
  45. }
  46. }
  47. },
  48. {fixed: 'right', title: '操作', width: 300, align: 'center', toolbar: '#operate'}
  49. ]]
  50. });
  51. // 列表点击放大图片
  52. $('.layui-table-main').on('click', '.layui_magnify', function () {
  53. var _src = $(this).attr('src');
  54. layui.layer.open({
  55. type: 1,
  56. title: false, //是否显示标题
  57. area: ['95%', '55%'],
  58. shade: 0.6, //透明度
  59. closeBtn: 1, //按钮 1有,0无
  60. shadeClose: true,
  61. content: '<img style="width:100%;height:100%" src="' + _src + '">',
  62. });
  63. });
  64. //监听指定开关
  65. form.on('switch(switchTest)', function (data) {
  66. layer.confirm('确定要修改此操作吗?', {btn: ['确定', '取消']}, function () {
  67. $.post('/brand/brandoperation', {id: data.value, type: 'show'}, function (data) {
  68. layer.msg(data.msg);
  69. table.reload('tablerReload');
  70. })
  71. }, function (aa) {
  72. table.reload('tablerReload'); //重新加载
  73. })
  74. });
  75. //列表操作
  76. table.on('tool(' + tableIds + ')', function (obj) {
  77. var layEvent = obj.event,
  78. data = obj.data;
  79. if (layEvent === 'sortData') { //修改排序
  80. table.on('edit(' + tableIds + ')', function (obj) {
  81. var data = obj.data;
  82. $.post('/brand/brandoperation', {id: data.id, type: 'sort', sort: data.sort}, function (data) {
  83. if (data.code == 200) {
  84. layer.msg(data.msg);
  85. } else if (data.code == 300) {
  86. layer.msg(data.msg);
  87. }
  88. })
  89. })
  90. } else if (layEvent === 'del') {
  91. layer.confirm('您确定要删除该品牌以及所有相关楼盘吗?', {btn: ['确定', '取消']}, function () {
  92. $.post('/brand/brandoperation', {id: data.id, type: 'del'}, function (data) {
  93. layer.msg(data.msg);
  94. table.reload('tablerReload');
  95. })
  96. })
  97. } else if (layEvent === 'edit') {
  98. var index = layer.open({
  99. type: 2,
  100. title: '编辑品牌',
  101. area: ['700px', '500px'],
  102. content: '/brand/brandedit?id=' + data.id,
  103. end: function () {
  104. table.reload('tablerReload'); //重新加载
  105. }
  106. });
  107. } else if (layEvent === 'relation') {
  108. var index = layui.layer.open({
  109. title: '<span style="color:red" >[' + data.name + ']</span>品牌相关楼盘',
  110. type: 2,
  111. area: ['', '100%'],
  112. maxmin: true,
  113. content: "/brand/house?bid=" + data.id,
  114. end: function () {
  115. table.reload('tablerReload');
  116. }
  117. })
  118. layui.layer.full(index);
  119. //改变窗口大小时,重置弹窗的宽高,防止超出可视区域(如F12调出debug的操作)
  120. $(window).on("resize", function () {
  121. layui.layer.full(index);
  122. })
  123. } else if (layEvent === 'set') {
  124. layer.confirm('是否将相关楼盘关联至该品牌下?', {btn: ['确定', '取消']}, function () {
  125. $.post('/brand/brandoperation', {id: data.id, type: 'set'}, function (data) {
  126. layer.msg(data.msg);
  127. table.reload('tablerReload');
  128. })
  129. })
  130. }
  131. });
  132. //添加图片上传
  133. upload.render({
  134. elem: '#test1'
  135. , url: '/house/createimg'
  136. , data: {token: csrfToken}
  137. , field: 'img'
  138. , size: 400 //限制上传图片的大小,单位为KB
  139. , exts: 'png|jpg|jpeg' //只允许上传压缩文件
  140. , auto: false
  141. , bindAction: '#addcity'
  142. , choose: function (obj) {
  143. obj.preview(function (index, file, result) {
  144. $('#breviary_img').attr('src', result); //图片链接(base64)
  145. });
  146. }
  147. });
  148. // 图片放大浏览
  149. $('.layui_magnify').click(function () {
  150. var _src = $(this).attr('src');
  151. layui.layer.open({
  152. type: 1,
  153. title: false, //是否显示标题
  154. area: ['60%', '80%'],
  155. shade: 0.6, //透明度
  156. closeBtn: 1, //按钮 1有,0无
  157. shadeClose: true,
  158. content: '<img style="width:100%;height:100%" src="' + _src + '">',
  159. });
  160. })
  161. // 新增
  162. var $ = layui.$, active = {
  163. addData: function () { //获取选中数据
  164. var index = layui.layer.open({
  165. type: 2,
  166. title: '新增品牌',
  167. closeBtn: 1, //不显示关闭按钮
  168. area: ['700px', '500px'],
  169. content: '/brand/brandadd' //iframe的url,no代表不显示滚动条
  170. , success: function (layero, index) {
  171. },
  172. end: function () {
  173. table.reload('tablerReload');
  174. }
  175. });
  176. },
  177. reload: function () {//搜索
  178. var form = pfgs_serialize($('form'));
  179. var name = '';
  180. table.reload('tablerReload', {
  181. where: {
  182. name: form.input_name_text,
  183. },
  184. page: {
  185. curr: 1
  186. }
  187. })
  188. },
  189. };
  190. $('.demoTable .layui-btn').on('click', function () {
  191. var type = $(this).data('type');
  192. active[type] ? active[type].call(this) : '';
  193. });
  194. //添加页面提交数据
  195. form.on('submit(add_brand)', function (data) {
  196. var formData = new FormData($('form')[0]);
  197. layer.confirm('您确定添加该品牌吗?', {btn: ['确定', '取消']}, function () {
  198. layer.msg('数据提交中,请稍候', {icon: 16, time: false, shade: 0.8}); //数据提交提示
  199. $.ajax({
  200. type: "post",
  201. url: "/brand/brandaddform",
  202. data: formData,
  203. contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
  204. processData: false, //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
  205. error: function (request) {
  206. layer.close(index);
  207. layer.msg("请联系管理员");
  208. },
  209. success: function (data) {
  210. if (data.data != null) {
  211. layer.msg(ReturnInfo(data.data), {icon: 5});
  212. return;
  213. }
  214. layer.msg(data.msg);
  215. parent.layer.close(oindex); //再执行关闭
  216. }
  217. });
  218. })
  219. return false;
  220. });
  221. form.on('submit(edit_brand)', function (data) {
  222. var index = layer.msg('数据提交中,请稍候', {icon: 16, time: false, shade: 0.8}); //数据提交提示
  223. var formData = new FormData($('form')[0]);
  224. $.ajax({
  225. type: "post",
  226. url: "/brand/brandeditform",
  227. data: formData,
  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.msg("请联系管理员");
  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. parent.layer.close(oindex); //再执行关闭
  241. }
  242. });
  243. return false;
  244. });
  245. form.on('select(position)', function (data) {
  246. console.log(data.value);
  247. var type = data.value;
  248. table.reload('tablerReload', {
  249. url: 'brandform'
  250. , where: {token: csrfToken, abroad: type}
  251. , method: 'post'
  252. });
  253. });
  254. })