material_replace.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. //检查库是否已经存在,存在则返回false
  66. async function validateLib(billLibID,compilationID){
  67. try {
  68. let lib = await ajaxPost("/materialReplace/findLib",{billLibID:billLibID,compilationID:compilationID});
  69. return lib?false:true;
  70. }catch (err){
  71. console.log(err);
  72. return false
  73. }
  74. }
  75. //取所有的定额并生成下拉框
  76. async function getCompilationOptions() {
  77. try {
  78. let compilations = await ajaxPost("/stdBillsEditor/getCompilationList");
  79. for(let com of compilations){
  80. let $option = $("<option >"+ com.name +"</option>");
  81. $option.val( com._id);
  82. $('#compilationSels').append($option);
  83. }
  84. }catch (err){
  85. console.log(err)
  86. }
  87. }
  88. //取所有的清单规则库并生成下拉框
  89. async function getBillsLibOptions(){
  90. try {
  91. let libs = await ajaxPost("/stdBillsEditor/getStdBillsLib");
  92. for(let b of libs){
  93. let $option = $("<option >"+ b.billsLibName +"</option>");
  94. $option.val( b.billsLibId);
  95. $('#billLibs').append($option);
  96. }
  97. }catch (err){
  98. console.log(err)
  99. }
  100. }
  101. async function getMaterialLib (ID) {
  102. try {
  103. let lib = await ajaxPost("/materialReplace/findLib",{ID:ID});
  104. if(lib){
  105. $("#renameText").val(lib.name);
  106. $("#libID").val(ID);
  107. $("#edit").modal({show:true});
  108. }else {
  109. alert("没有找到材料库");
  110. }
  111. }catch (err){
  112. console.log(err);
  113. }
  114. }
  115. function showDeleteModal(ID){
  116. $("#libID_del").val(ID);
  117. $("#delCount").val(0);
  118. $("#del").modal({show:true});
  119. }