device.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. $(function() {
  2. var defaultPage = 0;
  3. var defaultSize = 10;
  4. var currentPage = defaultPage;
  5. var currentSize = defaultSize;
  6. var tableFirstLoad = true;
  7. layui.use(["form", "table", "laypage", "layer"], function() {
  8. var table = layui.table;
  9. var laypage = layui.laypage;
  10. var layer = layui.layer;
  11. var title = $("#realName");
  12. var searchTitleKey = "";
  13. function loadDeviceList(page, size, searchTitle) {
  14. if (page === null || page === undefined) page = defaultPage;
  15. if (size === null || size === undefined) size = defaultSize;
  16. var tableOption = {
  17. elem: "#deviceList",
  18. height: "full-81",
  19. limit: size,
  20. cols: [
  21. [{
  22. type: "checkbox"
  23. },
  24. {
  25. field: "aircraft_id",
  26. title: "机型编号",
  27. align: "center"
  28. },
  29. {
  30. field: "aircraft_type",
  31. title: "机型名称",
  32. align: "center"
  33. },
  34. {
  35. field: "device_name",
  36. title: "部件型号",
  37. align: "center"
  38. },
  39. {
  40. field: "device_type",
  41. title: "部件名称",
  42. align: "center"
  43. },
  44. {
  45. field: "parent_id",
  46. title: "所属单位",
  47. align: "center"
  48. },
  49. {
  50. field: "parent",
  51. title: "起源",
  52. align: "center"
  53. },
  54. {
  55. field: "mark",
  56. title: "备注",
  57. align: "center"
  58. },
  59. {
  60. fixed: "right",
  61. title: "操作",
  62. width: 180,
  63. toolbar: "#operationToolbar",
  64. align: "center"
  65. }
  66. ]
  67. ]
  68. };
  69. if (searchTitle === null || searchTitle === undefined || searchTitle === "") {
  70. var url = "api/device/listpage?page=" + (page + 1) + "&size=" + size;
  71. fetchJSON(url, {}, "get", function(data) {
  72. if (data.status === 0) {
  73. if (tableFirstLoad) {
  74. tableFirstLoad = false;
  75. laypage.render({
  76. curr: page + 1,
  77. elem: "pagination",
  78. count: data.data.total,
  79. layout: ["count", "prev", "page", "next", "limit", "refresh", "skip"],
  80. jump: function(obj, first) {
  81. if (first) return;
  82. currentPage = obj.curr - 1;
  83. currentSize = obj.limit;
  84. loadDeviceList(currentPage, currentSize, searchTitleKey);
  85. }
  86. });
  87. }
  88. tableOption.data = data.data.list;
  89. table.render(tableOption);
  90. } else {
  91. showMsg(data.msg, 2, 2000);
  92. }
  93. });
  94. } else {
  95. var url = "api/device/getByName?name=" + searchTitle;
  96. fetchJSON(url, {}, "get", function(data) {
  97. if (data.status === 0) {
  98. if (tableFirstLoad) {
  99. tableFirstLoad = false;
  100. laypage.render({
  101. curr: page + 1,
  102. elem: "pagination",
  103. count: data.data.total,
  104. layout: ["count", "prev", "page", "next", "limit", "refresh", "skip"],
  105. jump: function(obj, first) {
  106. if (first) return;
  107. currentPage = obj.curr - 1;
  108. currentSize = obj.limit;
  109. loadDeviceList(currentPage, currentSize, searchTitleKey);
  110. }
  111. });
  112. }
  113. tableOption.data = data.data;
  114. table.render(tableOption);
  115. } else {
  116. showMsg(data.msg, 2, 2000);
  117. }
  118. });
  119. }
  120. }
  121. loadDeviceList();
  122. $("#searchArticle").on("click", function(e) {
  123. e.preventDefault();
  124. tableFirstLoad = true;
  125. searchTitleKey = title.val().trim();
  126. if (searchTitleKey === "")
  127. loadDeviceList(0, currentSize);
  128. else
  129. loadDeviceList(0, currentSize, searchTitleKey);
  130. });
  131. $("#keyword").on("keyup", function(e) {
  132. e.preventDefault();
  133. if (e.keyCode === 13) {
  134. $("#searchArticle").click();
  135. }
  136. });
  137. // 监听表内工具条
  138. table.on("tool(deviceList)", function(obj) {
  139. var data = obj.data;
  140. if (obj.event === "edit") {
  141. // 打开编辑框 进行详细编辑
  142. x_admin_show(
  143. "编辑",
  144. encodeURI(
  145. "../admin/device-edit.html?id=" + data.id + "&aircraft_id=" + data.aircraft_id + "&aircraft_type=" + data.aircraft_type +
  146. "&device_name=" + data.device_name + "&device_type=" + data.device_type + "&parent_id=" + data.parent_id +
  147. "&parent=" + data.parent + "&mark=" + data.mark
  148. ),
  149. 800,
  150. 400,
  151. function() {
  152. loadDeviceList(currentPage, currentSize);
  153. }
  154. );
  155. } else if (obj.event === "delete") {
  156. // 删除
  157. var ids = [];
  158. ids.push(data.id);
  159. layer.confirm("确认要删除吗?", function() {
  160. fetchJSON("api/device/delete", {
  161. ids: ids
  162. }, "post", function(data) {
  163. if (data.status === 0) {
  164. showMsg("已删除!", 1, 2000);
  165. tableFirstLoad = true;
  166. loadDeviceList(currentPage, currentSize);
  167. } else {
  168. showMsg(data.msg, 2, 2000);
  169. }
  170. });
  171. });
  172. }
  173. });
  174. // 批量删除
  175. $("#batchDelete").on("click", function(e) {
  176. e.preventDefault();
  177. var checkedData = table.checkStatus("deviceList").data;
  178. if (checkedData.length === 0) {
  179. showMsg("未选择需要删除的日志", 2, 2000);
  180. return;
  181. }
  182. layer.confirm("确认要删除吗?", function() {
  183. var ids = [];
  184. for (var i = 0; i < checkedData.length; i++) {
  185. ids.push(checkedData[i].id);
  186. }
  187. fetchJSON("api/device/delete", {
  188. ids: ids
  189. }, "post", function(data) {
  190. if (data.status === 0) {
  191. showMsg("已删除!");
  192. tableFirstLoad = true;
  193. loadDeviceList(currentPage, currentSize);
  194. } else {
  195. showMsg(data.msg, 2, 2000);
  196. }
  197. });
  198. });
  199. });
  200. // 新建用户
  201. $("#add").on("click", function(e) {
  202. e.preventDefault();
  203. tableFirstLoad = true;
  204. x_admin_show("添加",
  205. "../admin/device-edit.html?id=0&aircraft_id=&aircraft_type=&device_name=&device_type=&mark=&parent&parent_id=",
  206. 800, 400,
  207. function() {
  208. loadDeviceList(currentPage, currentSize);
  209. });
  210. });
  211. });
  212. });