Col_Admin.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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: 'Status',
  113. title: '状态',
  114. width: '100px',
  115. align: 'center',
  116. },
  117. {
  118. title: '操作',
  119. width: '350px',
  120. align: 'center',
  121. template: function (item) {
  122. var tmp = '';
  123. tmp += '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-filter="addsub">添加子栏目</a>';
  124. tmp += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-filter="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>';
  125. tmp += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-filter="del"><i class="layui-icon layui-icon-delete"></i>删除</a>';
  126. // tmp += '<a class="layui-btn layui-btn-normal layui-btn-xs" href="'+item.url+'" target="_blank">预览</a>';
  127. // tmp += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-href="/Admin/ColFields/Index?ColId='+item.id+'&right='+right+'">字段管理</a>';
  128. return tmp;
  129. }
  130. }
  131. ]
  132. });
  133. treeTable.on('tree(addsub)', function (res) {
  134. var data = res.item;
  135. if (data.ColId.length >= 12) {
  136. layer.msg("请勿创建超过3级栏目,如有需要请联系管理员");
  137. return;
  138. }
  139. var perContent = layer.open({
  140. type: 2
  141. , title: '分类设置-添加子栏目'
  142. , content: 'Add?CurColId=' + CurColId + '&PColId=' + data.ColId
  143. , maxmin: false
  144. , area: ['500px', '450px']
  145. , btn: ['确定', '取消']
  146. , yes: function (index, layero) {
  147. var iframeWindow = window['layui-layer-iframe' + index]
  148. , submitID = 'LAY-list-front-submit'
  149. , submit = layero.find('iframe').contents().find('#' + submitID);
  150. //详细内容
  151. if(iframeWindow.Contentsedit) iframeWindow.Contentsedit.sync();
  152. //监听提交
  153. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  154. var field = data.field; //获取提交的字段
  155. var userdata = "";
  156. for (var prop in field) {
  157. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  158. }
  159. //提交 Ajax 成功后,静态更新表格中的数据
  160. //$.ajax({});
  161. $.ajax({
  162. type: "POST",
  163. url: "/Admin/Col/Add?r=" + Math.random(1),
  164. data: userdata,
  165. dataType: "text",
  166. success: function (data) {
  167. layer.close(index); //关闭弹层
  168. if (data == "success") {
  169. treeTable.render(re);
  170. } else {
  171. layer.msg(data);
  172. }
  173. }
  174. });
  175. });
  176. submit.trigger('click');
  177. }
  178. });
  179. layer.full(perContent);
  180. });
  181. treeTable.on('tree(edit)', function (res) {
  182. var data = res.item;
  183. var perContent = layer.open({
  184. type: 2
  185. , title: '分类设置-编辑'
  186. , content: 'Edit?CurColId=' + CurColId + '&PColId=' + data.ColId.substring(0, data.ColId.length - 3) + '&ColId=' + data.ColId
  187. , maxmin: false
  188. , area: ['500px', '450px']
  189. , btn: ['确定', '取消']
  190. , yes: function (index, layero) {
  191. var iframeWindow = window['layui-layer-iframe' + index]
  192. , submitID = 'LAY-list-front-submit'
  193. , submit = layero.find('iframe').contents().find('#' + submitID);
  194. //详细内容
  195. if(iframeWindow.Contentsedit) iframeWindow.Contentsedit.sync();
  196. //监听提交
  197. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  198. var field = data.field; //获取提交的字段
  199. var userdata = "";
  200. for (var prop in field) {
  201. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  202. }
  203. //提交 Ajax 成功后,静态更新表格中的数据
  204. //$.ajax({});
  205. $.ajax({
  206. type: "POST",
  207. url: "/Admin/Col/Edit?r=" + Math.random(1),
  208. data: userdata,
  209. dataType: "text",
  210. success: function (data) {
  211. layer.close(index); //关闭弹层
  212. if (data == "success") {
  213. treeTable.render(re);
  214. } else {
  215. layer.msg(data);
  216. }
  217. }
  218. });
  219. });
  220. submit.trigger('click');
  221. }
  222. , success: function (layero, index) {
  223. }
  224. });
  225. layer.full(perContent);
  226. });
  227. treeTable.on('tree(del)', function (res) {
  228. var data = res.item;
  229. layer.confirm('确定要删除吗?删除后不能恢复!', function (index) {
  230. $.ajax({
  231. type: "POST",
  232. url: "/Admin/Col/Delete?r=" + Math.random(1),
  233. data: "Id=" + data.id,
  234. dataType: "text",
  235. success: function (data) {
  236. if (data == "success") {
  237. treeTable.render(re);
  238. layer.close(index);
  239. } else {
  240. parent.layer.msg(data);
  241. }
  242. }
  243. });
  244. });
  245. });
  246. treeTable.on('tree(status)', function (res) {
  247. console.log(res);
  248. });
  249. //事件
  250. var active = {
  251. batchdel: function () {
  252. var ids = treeTable.checked(re).join(',');
  253. if (ids == '') {
  254. parent.layer.msg("请选择要删除的项");
  255. } else {
  256. var index = layer.confirm('确定要删除吗?删除后不能恢复!', function (index) {
  257. $.ajax({
  258. type: "POST",
  259. url: "/Admin/Col/Delete?r=" + Math.random(1),
  260. data: "Id=" + ids,
  261. dataType: "text",
  262. success: function (data) {
  263. layer.close(index);
  264. if (data == "success") {
  265. treeTable.render(re);
  266. } else {
  267. layer.msg(data);
  268. }
  269. }
  270. });
  271. });
  272. }
  273. }
  274. , add: function () {
  275. var perContent = layer.open({
  276. type: 2
  277. , title: '分类设置-添加'
  278. , content: 'Add?CurColId=' + CurColId
  279. , maxmin: true
  280. , area: ['500px', '450px']
  281. , btn: ['确定', '取消']
  282. , yes: function (index, layero) {
  283. var iframeWindow = window['layui-layer-iframe' + index]
  284. , submitID = 'LAY-list-front-submit'
  285. , submit = layero.find('iframe').contents().find('#' + submitID);
  286. if (CurColId.substring(1, 3) == '001' || CurColId.substring(1, 3) == '002' || CurColId.substring(1, 3) == '003') {
  287. //详细内容
  288. iframeWindow.Contentsedit.sync();
  289. }
  290. //监听提交
  291. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  292. var field = data.field; //获取提交的字段
  293. var userdata = "";
  294. for (var prop in field) {
  295. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  296. }
  297. //提交 Ajax 成功后,静态更新表格中的数据
  298. //$.ajax({});
  299. $.ajax({
  300. type: "POST",
  301. url: "/Admin/Col/Add?r=" + Math.random(1),
  302. data: userdata,
  303. dataType: "text",
  304. success: function (data) {
  305. layer.close(index); //关闭弹层
  306. if (data == "success") {
  307. treeTable.render(re);
  308. } else {
  309. layer.msg(data);
  310. }
  311. }
  312. });
  313. });
  314. submit.trigger('click');
  315. }
  316. });
  317. layer.full(perContent);
  318. }
  319. , ImportData: function () {
  320. layer.open({
  321. type: 2,
  322. title: '导入',
  323. maxmin: false,
  324. area: ['460px', '180px'],
  325. content: $('#excelForm'),
  326. cancel: function () {
  327. }
  328. });
  329. }
  330. , ExportExcel: function () {
  331. var userdata = '';
  332. $(".layuiadmin-card-header-auto input").each(function (i) {
  333. userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
  334. });
  335. $(".layuiadmin-card-header-auto select").each(function (i) {
  336. userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
  337. });
  338. $.ajax({
  339. type: "GET",
  340. url: "/Admin/Col/ExportExcel?r=" + Math.random(1),
  341. data: userdata,
  342. dataType: "json",
  343. success: function (data) {
  344. data.Obj.unshift(data.Fields);
  345. excel.exportExcel(data.Obj, data.Info, 'xlsx');
  346. }
  347. });
  348. }
  349. , Open: function () {
  350. var ids = treeTable.checked(re).join(',');
  351. if (ids == '') {
  352. parent.layer.msg("请选择要开启的项");
  353. } else {
  354. var index = layer.confirm('确定要开启吗?', function (index) {
  355. $.ajax({
  356. type: "POST",
  357. url: "/Admin/Col/Open?r=" + Math.random(1),
  358. data: "Id=" + ids,
  359. dataType: "text",
  360. success: function (data) {
  361. layer.close(index);
  362. if (data == "success") {
  363. treeTable.render(re);
  364. } else {
  365. layer.msg(data);
  366. }
  367. }
  368. });
  369. });
  370. }
  371. }
  372. , Close: function () {
  373. var ids = treeTable.checked(re).join(',');
  374. if (ids == '') {
  375. parent.layer.msg("请选择要关闭的项");
  376. } else {
  377. var index = layer.confirm('确定要关闭吗?', function (index) {
  378. $.ajax({
  379. type: "POST",
  380. url: "/Admin/Col/Close?r=" + Math.random(1),
  381. data: "Id=" + ids,
  382. dataType: "text",
  383. success: function (data) {
  384. layer.close(index);
  385. if (data == "success") {
  386. treeTable.render(re);
  387. } else {
  388. layer.msg(data);
  389. }
  390. }
  391. });
  392. });
  393. }
  394. }
  395. , Unfold: function () {
  396. treeTable.openAll(re);
  397. }
  398. , PackUp: function () {
  399. treeTable.closeAll(re);
  400. }
  401. };
  402. $('.layui-btn').on('click', function () {
  403. var type = $(this).data('type');
  404. active[type] ? active[type].call(this) : '';
  405. });
  406. });