main.js 9.7 KB

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