material_replace.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Created by zhang on 2018/8/22.
  3. */
  4. $(document).ready(function() {
  5. $('#add').on('show.bs.modal', function () {
  6. $('#compilationSels').empty();
  7. $('#billLibs').empty();
  8. getCompilationOptions();
  9. getBillsLibOptions();
  10. });
  11. // 保存按钮
  12. $("#addLibs").click(async function() {
  13. let name = $('#name').val();
  14. if(name==''){
  15. $("#nameError").show();
  16. return;
  17. }else {
  18. let result = await validateLib($('#billLibs').val(),$('#compilationSels').val());
  19. if(result == true){//不存在则验证通过
  20. $("#addLibs").attr("disabled",true);//防止重复提交
  21. $("#addLibForm").submit();
  22. }else {
  23. alert('清单规则和定额库对应的材料库已存在,请重新选择');
  24. }
  25. }
  26. });
  27. $("#rename").click(async function() {
  28. let libID = $("#libID").val();
  29. let name = $('#renameText').val();
  30. if(libID!=''){
  31. if(name ==''){
  32. $("#renameError").show();
  33. return;
  34. }else {
  35. try {
  36. let newMaterial = await ajaxPost("/materialReplace/saveLib",{query:{ID:libID},data:{name:name}});
  37. $("#"+libID).children("a").text(newMaterial.name);
  38. $("#edit").modal('hide');
  39. }catch(err) {
  40. console.log(err);
  41. }
  42. }
  43. }
  44. });
  45. $("#delete").click(async function() {
  46. let libID = $("#libID_del").val();
  47. let delCount = parseInt($("#delCount").val());
  48. delCount = delCount+1;
  49. $("#delCount").val(delCount);
  50. if(delCount == 3){
  51. if(libID!=""){
  52. try {
  53. let result = await ajaxPost("/materialReplace/deleteLibByID",{ID:libID});
  54. if(result.ok){
  55. $("#"+libID).parent(".libTr").remove();
  56. }
  57. $("#del").modal('hide');
  58. }catch (err){
  59. console.log(err);
  60. }
  61. }
  62. }
  63. });
  64. // 锁定、解锁
  65. $('.lock').click(function () {
  66. lockUtil.handleLockClick($(this));
  67. });
  68. })
  69. //检查库是否已经存在,存在则返回false
  70. async function validateLib(billLibID,compilationID){
  71. try {
  72. let lib = await ajaxPost("/materialReplace/findLib",{billLibID:billLibID,compilationID:compilationID});
  73. return lib?false:true;
  74. }catch (err){
  75. console.log(err);
  76. return false
  77. }
  78. }
  79. //取所有的定额并生成下拉框
  80. async function getCompilationOptions() {
  81. try {
  82. let compilations = await ajaxPost("/stdBillsEditor/getCompilationList");
  83. for(let com of compilations){
  84. let $option = $("<option >"+ com.name +"</option>");
  85. $option.val( com._id);
  86. $('#compilationSels').append($option);
  87. }
  88. }catch (err){
  89. console.log(err)
  90. }
  91. }
  92. //取所有的清单规则库并生成下拉框
  93. async function getBillsLibOptions(){
  94. try {
  95. let libs = await ajaxPost("/stdBillsEditor/getStdBillsLib");
  96. for(let b of libs){
  97. let $option = $("<option >"+ b.billsLibName +"</option>");
  98. $option.val( b.billsLibId);
  99. $('#billLibs').append($option);
  100. }
  101. }catch (err){
  102. console.log(err)
  103. }
  104. }
  105. async function getMaterialLib (ID) {
  106. try {
  107. let lib = await ajaxPost("/materialReplace/findLib",{ID:ID});
  108. if(lib){
  109. $("#renameText").val(lib.name);
  110. $("#libID").val(ID);
  111. $("#edit").modal({show:true});
  112. }else {
  113. alert("没有找到材料库");
  114. }
  115. }catch (err){
  116. console.log(err);
  117. }
  118. }
  119. function showDeleteModal(ID){
  120. $("#libID_del").val(ID);
  121. $("#delCount").val(0);
  122. $("#del").modal({show:true});
  123. }