Col_Admin.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. var ExcelData;
  2. function ConfirmImport() {
  3. $.ajax({
  4. type: "POST",
  5. url: "/Admin/Col/Import?r=" + Math.random(1),
  6. data: "ExcelData=" + encodeURIComponent(JSON.stringify(ExcelData)),
  7. dataType: "text",
  8. success: function (data) {
  9. if (data == "success") {
  10. layer.msg("导入成功", { time: 2000 }, function () {
  11. window.location.reload();
  12. });
  13. } else {
  14. layer.msg(data);
  15. }
  16. }
  17. });
  18. }
  19. layui.config({
  20. base: '/layuiadmin/' //静态资源所在路径
  21. }).extend({
  22. myexcel: 'layui/lay/modules/excel',
  23. index: 'lib/index' //主入口模块
  24. }).use(['index', 'table', 'excel', 'laydate', 'treeTable'], function () {
  25. var $ = layui.$
  26. , form = layui.form
  27. , table = layui.table
  28. , treeTable = layui.treeTable;
  29. //- 筛选条件-日期
  30. var laydate = layui.laydate;
  31. //excel导入
  32. var excel = layui.excel;
  33. $('#ExcelFile').change(function (e) {
  34. var files = e.target.files;
  35. excel.importExcel(files, {}, function (data) {
  36. ExcelData = data[0].Sheet1;
  37. });
  38. });
  39. //监听单元格编辑
  40. table.on('edit(LAY-list-manage)', function (obj) {
  41. var value = obj.value //得到修改后的值
  42. , data = obj.data //得到所在行所有键值
  43. , field = obj.field; //得到字段
  44. if (field == "Sort") {
  45. $.ajax({
  46. type: "POST",
  47. url: "/Admin/Col/Sort?r=" + Math.random(1),
  48. data: "Id=" + data.Id + "&Sort=" + value,
  49. dataType: "text",
  50. success: function (data) {
  51. }
  52. });
  53. }
  54. });
  55. //列表数据
  56. // table.render({
  57. // elem: '#LAY-list-manage'
  58. // , url: '/Admin/Col/IndexData' //模拟接口
  59. // , where: { CurColId: CurColId }
  60. // , cols: [[
  61. // { type: 'checkbox', fixed: 'left' }
  62. // , { field: 'Id', fixed: 'left', title: 'ID', width: 80, sort: true, unresize: true }
  63. // , { field: 'ColId', title: '编码' }
  64. // , { field: 'ColName', title: '名称' }
  65. // , { field: 'Status', title: '状态' }
  66. // , { field: 'Sort', fixed: 'right', title: '排序', width: 80, edit: 'text' }
  67. // , { title: '操作', width: 370, align: 'center', fixed: 'right', toolbar: '#table-list-tools' }
  68. // ]]
  69. // , page: true
  70. // , limit: 3000
  71. // , height: 'full'
  72. // , text: '对不起,加载出现异常!'
  73. // , done: function (res, curr, count) {
  74. // $(".layui-none").text("无数据");
  75. // }
  76. // });
  77. var re = treeTable.render({
  78. elem: '#LAY-list-manage',
  79. url: '/Admin/Col/IndexData?CurColId=' + CurColId,
  80. icon_key: 'title',
  81. is_checkbox: true,
  82. end: function (e) {
  83. form.render();
  84. },
  85. cols: [
  86. {
  87. key: 'title',
  88. title: '名称',
  89. template: function (item) {
  90. return '<span style="color:#333;">' + item.title + '</span>';
  91. }
  92. },
  93. // {
  94. // key: 'id',
  95. // title: 'ID',
  96. // width: '100px',
  97. // align: 'center',
  98. // },
  99. // {
  100. // key: 'pid',
  101. // title: '父ID',
  102. // width: '100px',
  103. // align: 'center',
  104. // },
  105. {
  106. key: 'ColId',
  107. title: '分类编码',
  108. width: '100px',
  109. align: 'center',
  110. },
  111. {
  112. key: 'Sort',
  113. title: '排序',
  114. width: '100px',
  115. edit: 'text'
  116. },
  117. // {
  118. // key: 'Status',
  119. // title: '状态',
  120. // width: '100px',
  121. // align: 'center',
  122. // },
  123. {
  124. title: '操作',
  125. width: '350px',
  126. align: 'center',
  127. template: function (item) {
  128. var tmp = '';
  129. tmp += '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-filter="addsub">添加子栏目</a>';
  130. tmp += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-filter="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>';
  131. tmp += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-filter="del"><i class="layui-icon layui-icon-delete"></i>删除</a>';
  132. // tmp += '<a class="layui-btn layui-btn-normal layui-btn-xs" href="'+item.url+'" target="_blank">预览</a>';
  133. // tmp += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-href="/Admin/ColFields/Index?ColId='+item.id+'&right='+right+'">字段管理</a>';
  134. return tmp;
  135. }
  136. }
  137. ]
  138. });
  139. treeTable.on('tree(addsub)', function (res) {
  140. var data = res.item;
  141. if (data.ColId.length >= 12) {
  142. layer.msg("请勿创建超过3级栏目,如有需要请联系管理员");
  143. return;
  144. }
  145. var perContent = layer.open({
  146. type: 2
  147. , title: '分类设置-添加子栏目'
  148. , content: 'Add?CurColId=' + CurColId + '&PColId=' + data.ColId
  149. , maxmin: false
  150. , area: ['500px', '450px']
  151. , btn: ['确定', '取消']
  152. , yes: function (index, layero) {
  153. var iframeWindow = window['layui-layer-iframe' + index]
  154. , submitID = 'LAY-list-front-submit'
  155. , submit = layero.find('iframe').contents().find('#' + submitID);
  156. //详细内容
  157. if (iframeWindow.Contentsedit) iframeWindow.Contentsedit.sync();
  158. //监听提交
  159. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  160. var field = data.field; //获取提交的字段
  161. var userdata = "";
  162. for (var prop in field) {
  163. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  164. }
  165. //提交 Ajax 成功后,静态更新表格中的数据
  166. //$.ajax({});
  167. $.ajax({
  168. type: "POST",
  169. url: "/Admin/Col/Add?r=" + Math.random(1),
  170. data: userdata,
  171. dataType: "text",
  172. success: function (data) {
  173. layer.close(index); //关闭弹层
  174. if (data == "success") {
  175. treeTable.render(re);
  176. } else {
  177. layer.msg(data);
  178. }
  179. }
  180. });
  181. });
  182. submit.trigger('click');
  183. }
  184. });
  185. layer.full(perContent);
  186. });
  187. treeTable.on('tree(edit)', function (res) {
  188. var data = res.item;
  189. var perContent = layer.open({
  190. type: 2
  191. , title: '分类设置-编辑'
  192. , content: 'Edit?CurColId=' + CurColId + '&PColId=' + data.ColId.substring(0, data.ColId.length - 3) + '&ColId=' + data.ColId
  193. , maxmin: false
  194. , area: ['500px', '450px']
  195. , btn: ['确定', '取消']
  196. , yes: function (index, layero) {
  197. var iframeWindow = window['layui-layer-iframe' + index]
  198. , submitID = 'LAY-list-front-submit'
  199. , submit = layero.find('iframe').contents().find('#' + submitID);
  200. //详细内容
  201. if (iframeWindow.Contentsedit) iframeWindow.Contentsedit.sync();
  202. //监听提交
  203. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  204. var field = data.field; //获取提交的字段
  205. var userdata = "";
  206. for (var prop in field) {
  207. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  208. }
  209. //提交 Ajax 成功后,静态更新表格中的数据
  210. //$.ajax({});
  211. $.ajax({
  212. type: "POST",
  213. url: "/Admin/Col/Edit?r=" + Math.random(1),
  214. data: userdata,
  215. dataType: "text",
  216. success: function (data) {
  217. layer.close(index); //关闭弹层
  218. if (data == "success") {
  219. treeTable.render(re);
  220. } else {
  221. layer.msg(data);
  222. }
  223. }
  224. });
  225. });
  226. submit.trigger('click');
  227. }
  228. , success: function (layero, index) {
  229. }
  230. });
  231. layer.full(perContent);
  232. });
  233. treeTable.on('tree(del)', function (res) {
  234. var data = res.item;
  235. layer.confirm('确定要删除吗?删除后不能恢复!', function (index) {
  236. $.ajax({
  237. type: "POST",
  238. url: "/Admin/Col/Delete?r=" + Math.random(1),
  239. data: "Id=" + data.id,
  240. dataType: "text",
  241. success: function (data) {
  242. if (data == "success") {
  243. treeTable.render(re);
  244. layer.close(index);
  245. } else {
  246. parent.layer.msg(data);
  247. }
  248. }
  249. });
  250. });
  251. });
  252. treeTable.on('tree(status)', function (res) {
  253. console.log(res);
  254. });
  255. //事件
  256. var active = {
  257. batchdel: function () {
  258. var ids = treeTable.checked(re).join(',');
  259. if (ids == '') {
  260. parent.layer.msg("请选择要删除的项");
  261. } else {
  262. var index = layer.confirm('确定要删除吗?删除后不能恢复!', function (index) {
  263. $.ajax({
  264. type: "POST",
  265. url: "/Admin/Col/Delete?r=" + Math.random(1),
  266. data: "Id=" + ids,
  267. dataType: "text",
  268. success: function (data) {
  269. layer.close(index);
  270. if (data == "success") {
  271. treeTable.render(re);
  272. } else {
  273. layer.msg(data);
  274. }
  275. }
  276. });
  277. });
  278. }
  279. }
  280. , add: function () {
  281. var perContent = layer.open({
  282. type: 2
  283. , title: '分类设置-添加'
  284. , content: 'Add?CurColId=' + CurColId
  285. , maxmin: true
  286. , area: ['500px', '450px']
  287. , btn: ['确定', '取消']
  288. , yes: function (index, layero) {
  289. var iframeWindow = window['layui-layer-iframe' + index]
  290. , submitID = 'LAY-list-front-submit'
  291. , submit = layero.find('iframe').contents().find('#' + submitID);
  292. if (CurColId.substring(1, 3) == '001' || CurColId.substring(1, 3) == '002' || CurColId.substring(1, 3) == '003') {
  293. //详细内容
  294. iframeWindow.Contentsedit.sync();
  295. }
  296. //监听提交
  297. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  298. var field = data.field; //获取提交的字段
  299. var userdata = "";
  300. for (var prop in field) {
  301. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  302. }
  303. //提交 Ajax 成功后,静态更新表格中的数据
  304. //$.ajax({});
  305. $.ajax({
  306. type: "POST",
  307. url: "/Admin/Col/Add?r=" + Math.random(1),
  308. data: userdata,
  309. dataType: "text",
  310. success: function (data) {
  311. layer.close(index); //关闭弹层
  312. if (data == "success") {
  313. treeTable.render(re);
  314. } else {
  315. layer.msg(data);
  316. }
  317. }
  318. });
  319. });
  320. submit.trigger('click');
  321. }
  322. });
  323. layer.full(perContent);
  324. }
  325. , ImportData: function () {
  326. layer.open({
  327. type: 2,
  328. title: '导入',
  329. maxmin: false,
  330. area: ['460px', '180px'],
  331. content: $('#excelForm'),
  332. cancel: function () {
  333. }
  334. });
  335. }
  336. , ExportExcel: function () {
  337. var userdata = '';
  338. $(".layuiadmin-card-header-auto input").each(function (i) {
  339. userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
  340. });
  341. $(".layuiadmin-card-header-auto select").each(function (i) {
  342. userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
  343. });
  344. $.ajax({
  345. type: "GET",
  346. url: "/Admin/Col/ExportExcel?r=" + Math.random(1),
  347. data: userdata,
  348. dataType: "json",
  349. success: function (data) {
  350. data.Obj.unshift(data.Fields);
  351. excel.exportExcel(data.Obj, data.Info, 'xlsx');
  352. }
  353. });
  354. }
  355. , Open: function () {
  356. var ids = treeTable.checked(re).join(',');
  357. if (ids == '') {
  358. parent.layer.msg("请选择要开启的项");
  359. } else {
  360. var index = layer.confirm('确定要开启吗?', function (index) {
  361. $.ajax({
  362. type: "POST",
  363. url: "/Admin/Col/Open?r=" + Math.random(1),
  364. data: "Id=" + ids,
  365. dataType: "text",
  366. success: function (data) {
  367. layer.close(index);
  368. if (data == "success") {
  369. treeTable.render(re);
  370. } else {
  371. layer.msg(data);
  372. }
  373. }
  374. });
  375. });
  376. }
  377. }
  378. , Close: function () {
  379. var ids = treeTable.checked(re).join(',');
  380. if (ids == '') {
  381. parent.layer.msg("请选择要关闭的项");
  382. } else {
  383. var index = layer.confirm('确定要关闭吗?', function (index) {
  384. $.ajax({
  385. type: "POST",
  386. url: "/Admin/Col/Close?r=" + Math.random(1),
  387. data: "Id=" + ids,
  388. dataType: "text",
  389. success: function (data) {
  390. layer.close(index);
  391. if (data == "success") {
  392. treeTable.render(re);
  393. } else {
  394. layer.msg(data);
  395. }
  396. }
  397. });
  398. });
  399. }
  400. }
  401. , Unfold: function () {
  402. treeTable.openAll(re);
  403. }
  404. , PackUp: function () {
  405. treeTable.closeAll(re);
  406. }
  407. };
  408. $('.layui-btn').on('click', function () {
  409. var type = $(this).data('type');
  410. active[type] ? active[type].call(this) : '';
  411. });
  412. });