SysAdmin_Admin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. var ExcelData;
  2. function ConfirmImport() {
  3. $.ajax({
  4. type: "POST",
  5. url: "/Admin/SysAdmin/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'], function () {
  25. var $ = layui.$
  26. , form = layui.form
  27. , table = layui.table;
  28. //- 筛选条件-日期
  29. var laydate = layui.laydate;
  30. laydate.render({
  31. elem: '#sLastLoginDate',
  32. type: 'datetime'
  33. });
  34. laydate.render({
  35. elem: '#eLastLoginDate',
  36. type: 'datetime'
  37. });
  38. //excel导入
  39. var excel = layui.excel;
  40. $('#ExcelFile').change(function (e) {
  41. var files = e.target.files;
  42. excel.importExcel(files, { }, function (data) {
  43. ExcelData = data[0].Sheet1;
  44. });
  45. });
  46. //监听单元格编辑
  47. table.on('edit(table1)', function(obj){
  48. var value = obj.value //得到修改后的值
  49. ,data = obj.data //得到所在行所有键值
  50. ,field = obj.field; //得到字段
  51. if(field == "Sort"){
  52. $.ajax({
  53. type: "POST",
  54. url: "/Admin/SysAdmin/Sort?r=" + Math.random(1),
  55. data: "Id=" + data.Id + "&Sort=" + value,
  56. dataType: "text",
  57. success: function (data) {
  58. }
  59. });
  60. }
  61. });
  62. //列表数据
  63. table.render({
  64. elem: '#LAY-list-manage'
  65. , url: '/Admin/SysAdmin/IndexData' //模拟接口
  66. , cols: [[
  67. { type: 'checkbox', fixed: 'left' }
  68. , {field:'Id', fixed: 'left', title:'ID', width:80, sort: true, unresize: true}
  69. ,{field:'AdminName', title:'用户名', sort: true}
  70. ,{field:'RealName', title:'名称', sort: true}
  71. ,{field:'Role', title:'角色', sort: true}
  72. ,{field:'LastLoginDate', title:'最后登录时间', sort: true}
  73. , {field:'Sort', fixed: 'right', title:'排序', width:80, edit: 'text'}
  74. , { title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-list-tools' }
  75. ]]
  76. , page: true
  77. , limit: 30
  78. , height: 'full-220'
  79. , text: '对不起,加载出现异常!'
  80. , done: function (res, curr, count) {
  81. $(".layui-none").text("无数据");
  82. }
  83. });
  84. //监听工具条
  85. table.on('tool(LAY-list-manage)', function (obj) {
  86. var data = obj.data;
  87. if (obj.event === 'del') {
  88. var index = layer.confirm('确定要删除吗?删除后不能恢复!', function (index) {
  89. $.ajax({
  90. type: "POST",
  91. url: "/Admin/SysAdmin/Delete?r=" + Math.random(1),
  92. data: "Id=" + data.Id,
  93. dataType: "text",
  94. success: function (data) {
  95. if (data == "success") {
  96. obj.del();
  97. layer.close(index);
  98. } else {
  99. parent.layer.msg(data);
  100. }
  101. }
  102. });
  103. });
  104. } else if (obj.event === 'edit') {
  105. var tr = $(obj.tr);
  106. layer.open({
  107. type: 2
  108. , title: '后台管理员-编辑'
  109. , content: 'Edit?Id='+data.Id
  110. , maxmin: true
  111. , area: ['500px', '450px']
  112. , btn: ['确定', '取消']
  113. , yes: function (index, layero) {
  114. var iframeWindow = window['layui-layer-iframe' + index]
  115. , submitID = 'LAY-list-front-submit'
  116. , submit = layero.find('iframe').contents().find('#' + submitID);
  117. //监听提交
  118. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  119. var field = data.field; //获取提交的字段
  120. var userdata = "";
  121. for (var prop in field) {
  122. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  123. }
  124. //提交 Ajax 成功后,静态更新表格中的数据
  125. //$.ajax({});
  126. $.ajax({
  127. type: "POST",
  128. url: "/Admin/SysAdmin/Edit?r=" + Math.random(1),
  129. data: userdata,
  130. dataType: "text",
  131. success: function (data) {
  132. layer.close(index); //关闭弹层
  133. if (data == "success") {
  134. table.reload('LAY-list-manage'); //数据刷新
  135. } else {
  136. layer.msg(data);
  137. }
  138. }
  139. });
  140. });
  141. submit.trigger('click');
  142. }
  143. , success: function (layero, index) {
  144. }
  145. });
  146. }
  147. });
  148. //监听搜索
  149. form.on('submit(LAY-list-front-search)', function (data) {
  150. var field = data.field;
  151. //执行重载
  152. table.reload('LAY-list-manage', {
  153. where: field
  154. });
  155. });
  156. form.on('submit(LAY-list-front-searchall)', function (data) {
  157. table.reload('LAY-list-manage', {
  158. where: null
  159. });
  160. });
  161. //事件
  162. var active = {
  163. batchdel: function () {
  164. var checkStatus = table.checkStatus('LAY-list-manage')
  165. , data = checkStatus.data; //得到选中的数据
  166. if (data.length < 1) {
  167. parent.layer.msg("请选择要删除的项");
  168. } else {
  169. var ids = "";
  170. $.each(data, function (index, value) {
  171. ids += data[index].Id + ",";
  172. });
  173. ids = ids.substring(0, ids.length - 1);
  174. var index = layer.confirm('确定要删除吗?删除后不能恢复!', function (index) {
  175. $.ajax({
  176. type: "POST",
  177. url: "/Admin/SysAdmin/Delete?r=" + Math.random(1),
  178. data: "Id=" + ids,
  179. dataType: "text",
  180. success: function (data) {
  181. layer.close(index);
  182. if (data == "success") {
  183. table.reload('LAY-list-manage');
  184. } else {
  185. layer.msg(data);
  186. }
  187. }
  188. });
  189. });
  190. }
  191. }
  192. , add: function () {
  193. layer.open({
  194. type: 2
  195. , title: '后台管理员-添加'
  196. , content: 'Add'
  197. , maxmin: true
  198. , area: ['500px', '450px']
  199. , btn: ['确定', '取消']
  200. , yes: function (index, layero) {
  201. var iframeWindow = window['layui-layer-iframe' + index]
  202. , submitID = 'LAY-list-front-submit'
  203. , submit = layero.find('iframe').contents().find('#' + submitID);
  204. //监听提交
  205. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  206. var field = data.field; //获取提交的字段
  207. var userdata = "";
  208. for (var prop in field) {
  209. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  210. }
  211. //提交 Ajax 成功后,静态更新表格中的数据
  212. //$.ajax({});
  213. $.ajax({
  214. type: "POST",
  215. url: "/Admin/SysAdmin/Add?r=" + Math.random(1),
  216. data: userdata,
  217. dataType: "text",
  218. success: function (data) {
  219. layer.close(index); //关闭弹层
  220. if (data == "success") {
  221. table.reload('LAY-list-manage'); //数据刷新
  222. } else {
  223. layer.msg(data);
  224. }
  225. }
  226. });
  227. });
  228. submit.trigger('click');
  229. }
  230. });
  231. }
  232. , ImportData: function () {
  233. layer.open({
  234. type: 2,
  235. title: '导入',
  236. maxmin: false,
  237. area: ['460px', '180px'],
  238. content: $('#excelForm'),
  239. cancel: function () {
  240. }
  241. });
  242. }
  243. , ExportExcel: function () {
  244. var userdata = '';
  245. $(".layuiadmin-card-header-auto input").each(function (i) {
  246. userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
  247. });
  248. $(".layuiadmin-card-header-auto select").each(function (i) {
  249. userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
  250. });
  251. $.ajax({
  252. type: "GET",
  253. url: "/Admin/SysAdmin/ExportExcel?r=" + Math.random(1),
  254. data: userdata,
  255. dataType: "json",
  256. success: function (data) {
  257. data.Obj.unshift(data.Fields);
  258. excel.exportExcel(data.Obj, data.Info, 'xlsx');
  259. }
  260. });
  261. }
  262. , Open: function () {
  263. var checkStatus = table.checkStatus('LAY-list-manage')
  264. , data = checkStatus.data; //得到选中的数据
  265. if(data.length < 1){
  266. parent.layer.msg("请选择要开启的项");
  267. }else{
  268. var ids = "";
  269. $.each(data, function (index, value) {
  270. ids += data[index].Id + ",";
  271. });
  272. ids = ids.substring(0, ids.length - 1);
  273. var index = layer.confirm('确定要开启吗?', function (index) {
  274. $.ajax({
  275. type: "POST",
  276. url: "/Admin/SysAdmin/Open?r=" + Math.random(1),
  277. data: "Id=" + ids,
  278. dataType: "text",
  279. success: function (data) {
  280. layer.close(index);
  281. if (data == "success") {
  282. table.reload('LAY-list-manage');
  283. } else {
  284. layer.msg(data);
  285. }
  286. }
  287. });
  288. });
  289. }
  290. }
  291. , Close: function () {
  292. var checkStatus = table.checkStatus('LAY-list-manage')
  293. , data = checkStatus.data; //得到选中的数据
  294. if(data.length < 1){
  295. parent.layer.msg("请选择要关闭的项");
  296. }else{
  297. var ids = "";
  298. $.each(data, function (index, value) {
  299. ids += data[index].Id + ",";
  300. });
  301. ids = ids.substring(0, ids.length - 1);
  302. var index = layer.confirm('确定要关闭吗?', function (index) {
  303. $.ajax({
  304. type: "POST",
  305. url: "/Admin/SysAdmin/Close?r=" + Math.random(1),
  306. data: "Id=" + ids,
  307. dataType: "text",
  308. success: function (data) {
  309. layer.close(index);
  310. if (data == "success") {
  311. table.reload('LAY-list-manage');
  312. } else {
  313. layer.msg(data);
  314. }
  315. }
  316. });
  317. });
  318. }
  319. }
  320. };
  321. $('.layui-btn').on('click', function () {
  322. var type = $(this).data('type');
  323. active[type] ? active[type].call(this) : '';
  324. });
  325. });