user.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. $(document).ready(function () {
  2. initRoleSelect();
  3. initUserTable();
  4. });
  5. function initRoleSelect() {
  6. $.ajax({
  7. url: "/Admin/GetAllRoles",
  8. method: "GET",
  9. headers: { "Content-Type": "application/json" },
  10. async: true,
  11. success: function (result) {
  12. if (result.code === 0) {
  13. for (var i = 0; i < result.data.length; i++) {
  14. var id = result.data[i].id;
  15. var value = result.data[i].name;
  16. $("#Role").append("<option value=" + id + ">" + value + "</option>");
  17. }
  18. } else if (result.code === 1) {
  19. alert(result.message);
  20. }
  21. }
  22. });
  23. }
  24. var userTable;
  25. var size;
  26. var number;
  27. function initUserTable() {
  28. var queryUrl = '/Admin/GetUsers';
  29. userTable = $("#user_table").bootstrapTable({
  30. url: queryUrl,
  31. columns: [
  32. { field: "id", title: "ID", visible: false },
  33. { field: "jobNo", title: "工号"},
  34. { field: "name", title: "名称" },
  35. { field: "roleId", title: "角色Id", visible: false },
  36. { field: "roleName", title: "角色名称" },
  37. { field: "createTime", title: "创建时间" },
  38. {
  39. title: '操作',
  40. field: "id",
  41. formatter: operation,
  42. width: 200
  43. }
  44. ],
  45. pagination: true,
  46. pageNumber: 1,
  47. pageSize: size,
  48. pageList: [10, 25, 50, 100],
  49. smartDisplay: false,
  50. sidePagination: "server",
  51. queryParams: function (params) {
  52. var temp = {
  53. pageNumber: (params.offset / params.limit) + 1,
  54. pageSize: params.limit,
  55. keyword: $("#keyword").val()
  56. };
  57. number = temp.pageNumber;
  58. return temp;
  59. }
  60. });
  61. }
  62. function operation(value, row, index) {
  63. var htm =
  64. "<a class='label' onclick='modelUpdateUser(\"" + value + "\")'> <i class='fa fa-edit'> 编辑</i></a >" +
  65. "<a class='label' onclick='deleteUser(\"" + value + "\")'> <i class='fa fa-trash-o'> 删除</i></a >";
  66. return htm;
  67. }
  68. function modelCreateUser() {
  69. $("#Id").val("");
  70. $("#FormJobNo")[0].disabled = false;
  71. $("#modal_user_add").modal("show");
  72. }
  73. function refreshUserTable() {
  74. userTable.bootstrapTable("refreshOptions", { pageNubmer: 1 });
  75. }
  76. function selectPageUserTable() {
  77. userTable.bootstrapTable("selectPage", number);
  78. }
  79. function saveUser() {
  80. var id = $("#Id").val();
  81. var url;
  82. if (id === null || id === "") {
  83. url = "/Admin/CreateUser";
  84. } else {
  85. url = "/Admin/UpdateUser/" + id;
  86. }
  87. $.ajax({
  88. url: url,
  89. method: "POST",
  90. headers: { "Content-Type": "application/json" },
  91. async: true,
  92. data: JSON.stringify({
  93. "jobNo": $("#FormJobNo").val(),
  94. "name": $("#Name").val(),
  95. "roleId": $("#Role").val(),
  96. }),
  97. success: function (result) {
  98. if (result.code === 0) {
  99. $("#modal_user_add").modal("hide");
  100. if (id === null || id === "") {
  101. refreshUserTable();
  102. } else {
  103. selectPageUserTable();
  104. }
  105. } else if (result.code === 1) {
  106. alert(result.message);
  107. }
  108. }
  109. });
  110. }
  111. function modelUpdateUser(id) {
  112. $("#Id").val(id);
  113. $("#FormJobNo")[0].disabled = true;
  114. $.ajax({
  115. url: "/Admin/User/" + id,
  116. method: "GET",
  117. headers: { "Content-Type": "application/json" },
  118. async: true,
  119. success: function (result) {
  120. if (result.code === 0) {
  121. $("#FormJobNo").val(result.data.jobNo);
  122. $("#Name").val(result.data.name);
  123. $("#Role").val(result.data.roleId);
  124. } else if (result.code === 1) {
  125. alert(result.message);
  126. }
  127. }
  128. });
  129. $("#modal_user_add").modal("show");
  130. }
  131. function deleteUser(id) {
  132. $.ajax({
  133. url: "/Admin/DeleteUser/" + id,
  134. method: "POST",
  135. headers: { "Content-Type": "application/json" },
  136. async: true,
  137. success: function (result) {
  138. if (result.code === 0) {
  139. if (id === null || id === "") {
  140. refreshUserTable();
  141. } else {
  142. selectPageUserTable();
  143. }
  144. } else if (result.code === 1) {
  145. alert(result.message);
  146. }
  147. }
  148. });
  149. }