main.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * Created by Zhong on 2017/8/14.
  3. */
  4. $(function () {
  5. let dispNameArr;
  6. let compilationsArr;
  7. let usedCom;
  8. getCompilationList(function (datas) {
  9. compilationsArr = datas;
  10. getAllGljLib(function (dispNames, compilationsUsedArr) {
  11. usedCom = compilationsUsedArr;
  12. dispNameArr = dispNames;
  13. //添加
  14. $('#addBtn').click(function () {
  15. let compilationName = $('#compilationSels option:selected').text();
  16. let compilationId = $('#compilationSels option:selected').val();
  17. let libName = $('#libNameTxt').val();
  18. if(libName.trim().length === 0){
  19. alert('名称不可为空!');
  20. $('#libNameTxt').val('')
  21. }
  22. else if(compilationsUsedArr.indexOf(compilationId) !== -1){// compilationsUsedArr;//在该编办下已建库,限制一个编办只能有一个库
  23. alert('该编办已被绑定!');
  24. $('#libNameTxt').val('')
  25. }
  26. else if(dispNames.indexOf(libName) !== -1){
  27. alert('此工料机库已存在!');
  28. $('#libNameTxt').val('')
  29. }
  30. else if(compilationName.trim().length === 0){
  31. alert('编办不可为空!');
  32. }
  33. else{
  34. let newGljLib = {};
  35. newGljLib.dispName = libName;
  36. newGljLib.compilationId = compilationId;
  37. newGljLib.compilationName = compilationName;
  38. newGljLib.creator = oprtor;
  39. newGljLib.appType = "建筑";
  40. $('#libNameTxt').val('');
  41. createGljLib(newGljLib, dispNameArr, usedCom);
  42. }
  43. });
  44. //重命名
  45. $("#showArea").on("click", "[data-target = '#edit']", function(){
  46. let renameId = $(this).parent().parent().attr("id");
  47. let compilationName = $(this).parent().parent().children()[1].textContent;
  48. $('#compilationEdit option').text(compilationName);
  49. $("#renameA").attr("renameId", renameId);
  50. });
  51. $("#renameA").click(function(){
  52. let newName = $("#renameText").val();
  53. let libId = $(this).attr("renameId");
  54. let jqSel = "#" + libId + " td:first" + " a";
  55. let orgName = $(jqSel).text();
  56. if(newName.trim().length === 0){
  57. alert("名称不可为空!");
  58. $("#renameText").val('');
  59. }
  60. else if(dispNameArr.indexOf(newName) !== -1){
  61. alert("该工料机库已存在!");
  62. $("#renameText").val('');
  63. }
  64. else{
  65. renameGljLib({ID: libId, newName: newName, orgName: orgName}, dispNameArr);
  66. }
  67. });
  68. //删除
  69. $("#showArea").on("click", "[data-target = '#del']", function(){
  70. let deleteId = $(this).parent().parent().attr("id");
  71. $("#deleteA").attr("deleteId", deleteId);
  72. });
  73. $("#deleteA").click(function(){
  74. let deleteId = $(this).attr("deleteId");
  75. let jqSel = "#" + deleteId + " td:first" + " a";
  76. let libName = $(jqSel).text();
  77. let compilationName = $("#" + deleteId + " td:eq(1)").text();
  78. for(let i = 0, len = compilationsArr.length; i < len; i++){
  79. if(compilationsArr[i].name === compilationName.trim()){
  80. usedCom.splice(usedCom.indexOf(compilationsArr[i]._id), 1);
  81. break;
  82. }
  83. }
  84. removeGljLib({libId: deleteId, libName: libName}, dispNameArr);
  85. });
  86. });
  87. });
  88. });
  89. function getAllGljLib(callback){
  90. $.ajax({
  91. type: 'post',
  92. url: 'api/getAllGljLib',
  93. dataType: 'json',
  94. success: function (result) {
  95. let dispNames = [];
  96. let compilationsUsedArr = [];
  97. if(result.data.length > 0){
  98. for(let i = 0; i < result.data.length; i++){
  99. compilationsUsedArr.push(result.data[i].compilationId);//已建库的编办
  100. let id = result.data[i].ID;
  101. let libName = result.data[i].dispName;
  102. let createDate = result.data[i].createDate.split(' ')[0];
  103. let compilationName = result.data[i].compilationName;
  104. let rationLibs = result.data[i].rationLibs;
  105. let rationLibsName = '';
  106. for(let j = 0; j < rationLibs.length; j++){
  107. rationLibsName += rationLibs[j].dispName + "</br>";
  108. }
  109. dispNames.push(result.data[i].dispName);
  110. $("#showArea").append(
  111. "<tr id='tempId'>" +
  112. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  113. "<td>"+compilationName+" </td>" +
  114. "<td>"+rationLibsName+" </td>" +
  115. "<td>"+createDate+" </td>" +
  116. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  117. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  118. "<i class='fa fa-remove'></i></a></td></tr>");
  119. var newHref = "/stdGljRepository/glj?gljLibId="+id;
  120. $("#tempId td:first a").attr("href", newHref);
  121. $("#tempId").attr("id", id);
  122. }
  123. }
  124. callback(dispNames, compilationsUsedArr);
  125. }
  126. });
  127. }
  128. function getCompilationList(callback){
  129. $.ajax({
  130. type: 'post',
  131. url: 'api/getCompilationList',
  132. dataType: 'json',
  133. success: function (result) {
  134. //addoptions
  135. for(let i = 0; i < result.data.length; i++){
  136. let $option = $("<option >"+ result.data[i].name +"</option>");
  137. $option.val( result.data[i]._id);
  138. $('#compilationSels').append($option);
  139. }
  140. $('#compilationSels').on("change", function () {
  141. });
  142. callback(result.data);
  143. }
  144. });
  145. }
  146. function createGljLib(gljLibObj, dispNamesArr, usedCom){
  147. $.ajax({
  148. type: 'post',
  149. url: 'api/createGljLib',
  150. data: {gljLibObj: JSON.stringify(gljLibObj)},
  151. dataType: 'json',
  152. success: function (result) {
  153. if(result.data){
  154. let id = result.data.ID;
  155. let libName = result.data.dispName;
  156. let createDate = result.data.createDate.split(' ')[0];
  157. let compilationName = result.data.compilationName;
  158. dispNamesArr.push(libName);
  159. usedCom.push(gljLibObj.compilationId);
  160. $("#showArea").append(
  161. "<tr id='tempId'>" +
  162. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  163. "<td>"+compilationName+" </td>" +
  164. "<td>"+''+" </td>" +
  165. "<td>"+createDate+" </td>" +
  166. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  167. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  168. "<i class='fa fa-remove'></i></a></td></tr>");
  169. var newHref = "/stdGljRepository/glj?gljLibId="+id;
  170. $("#tempId td:first a").attr("href", newHref);
  171. $("#tempId").attr("id", id);
  172. }
  173. $('#cancelBtn').click();
  174. }
  175. })
  176. }
  177. function renameGljLib(renameObj, dispNames){
  178. $.ajax({
  179. type: 'post',
  180. url: 'api/renameGljLib',
  181. data: {oprtor: oprtor, renameObj: JSON.stringify(renameObj)},
  182. dataType: 'json',
  183. success: function (result) {
  184. if(!result.error){
  185. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  186. $(jqSel).text(renameObj.newName);
  187. let index = dispNames.indexOf(renameObj.orgName);
  188. dispNames.splice(index, 1);
  189. dispNames.splice(index, 0, renameObj.newName);
  190. }
  191. $('#editCancelBtn').click();
  192. $('#renameText').val('');
  193. }
  194. })
  195. }
  196. function removeGljLib(delObj, dispNames){
  197. $.ajax({
  198. type: 'post',
  199. url: 'api/removeGljLib',
  200. data: {oprtor: oprtor, libId: delObj.libId},
  201. dataType: 'json',
  202. success: function (result) {
  203. if(!result.error){
  204. if(result.message === '此工料机库已被引用!'){
  205. $('#delCancelBtn').click();
  206. alert("此工料机库已被引用,不可删除!");
  207. }
  208. else if(result.message === '删除成功'){
  209. var jqSel = "#"+ delObj.libId;
  210. $(jqSel).remove();
  211. let index = dispNames.indexOf(delObj.libName);
  212. dispNames.splice(index, 1);
  213. $('#delCancelBtn').click();
  214. }
  215. }
  216. }
  217. })
  218. }