material_replace_edit.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Created by zhang on 2018/8/23.
  3. */
  4. let materialOjb = {
  5. billsSpread:null,
  6. materialSpread:null,
  7. billsList:JSON.parse(billsList),
  8. billsSetting:{
  9. header: [
  10. {headerName: "清单编号", headerWidth: 180, dataCode: "code", dataType: "String",formatter: "@"},
  11. {headerName: "清单名称", headerWidth: 240, dataCode: "name", dataType: "String"},
  12. {headerName: "规则", headerWidth: 150, dataCode: "rule", hAlign: "left", dataType: "String",cellType:'comboBox',editorValueType:true,options:[{text:"规则1",value:1},{text:"规则2",value:2}]}
  13. ],
  14. view: {
  15. lockColumns: [1]
  16. },
  17. headerHeight:45
  18. },
  19. materialSetting:{
  20. header: [
  21. {headerName: "材料编号", headerWidth: 180, dataCode: "code", dataType: "String"},
  22. {headerName: "材料名称", headerWidth: 240, dataCode: "name", dataType: "String",cellType:'tipsCell'},
  23. {headerName: "规格", headerWidth: 150, dataCode: "specs", hAlign: "left", dataType: "String",cellType:'tipsCell'}
  24. ],
  25. view: {
  26. lockColumns: [1,2]
  27. },
  28. headerHeight:45
  29. },
  30. initSpread:function () {
  31. if(!this.billsSpread){
  32. this.billsSpread = SheetDataHelper.createNewSpread($("#billsSpread")[0]);
  33. }
  34. if(!this.materialSpread){
  35. this.materialSpread = SheetDataHelper.createNewSpread($("#materialSpread")[0]);
  36. }
  37. this.billsSheet = this.billsSpread .getSheet(0);
  38. sheetCommonObj.initSheet(this.billsSheet,this.billsSetting, 30);
  39. this.billsSheet.name('billsSheet');
  40. this.billsSheet.bind(GC.Spread.Sheets.Events.ValueChanged, this.onBillsValueChange);
  41. this.billsSheet.bind(GC.Spread.Sheets.Events.SelectionChanged,this.onBillsSelectionChange);
  42. this.initRightClick("billsSpread",this.billsSpread);
  43. this.materialSheet = this.materialSpread .getSheet(0);
  44. sheetCommonObj.initSheet(this.materialSheet,this.materialSetting, 30);
  45. this.materialSheet.name('materialSheet');
  46. this.refreshSheet();
  47. },
  48. initRightClick : function(id,spread) {
  49. let me = this;
  50. $.contextMenu({
  51. selector: '#'+id,
  52. build: function ($trigger, e) {
  53. me.rightClickTarget = SheetDataHelper.safeRightClickSelection($trigger, e, spread);
  54. return me.rightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.viewport ||
  55. me.rightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  56. },
  57. items: {
  58. "delete": {
  59. name: "删除",
  60. icon: 'fa-trash-o',
  61. disabled: function () {
  62. return false;
  63. },
  64. callback: function (key, opt) {
  65. console.log();
  66. }
  67. }
  68. }
  69. });
  70. },
  71. refreshSheet:function(){
  72. sheetCommonObj.showData(this.billsSheet,this.billsSetting,this.billsList);
  73. this.billsSheet.setRowCount(this.billsList.length + 1);
  74. },
  75. onBillsSelectionChange:function (sander,args) {
  76. args.sheet.repaint();
  77. },
  78. onBillsValueChange: function(sander,args){
  79. let me = materialOjb;
  80. let field = me.billsSetting.header[args.col].dataCode;
  81. let code = null;
  82. if(args.row < me.billsList.length){
  83. code = me.billsList[args.row].code;
  84. }
  85. if(me.validateBills(field,args.newValue)){
  86. let data = me.getUpdateData(field,args.newValue,code);
  87. if (data){
  88. me.saveBills([data]);
  89. return;
  90. }
  91. }
  92. me.refreshSheet();
  93. },
  94. validateBills:function (field,value) {
  95. if(field == 'code'){
  96. if(value.length !== 9){
  97. alert("清单长度不正确");
  98. return false;
  99. }
  100. if(_.find(this.billsList,{'code':value})) {
  101. alert("清单已存在");
  102. return false;
  103. }
  104. }
  105. return true;
  106. },
  107. getUpdateData:function (field,newValue,code) {
  108. if(field == 'code'){
  109. if(!isDef(code) || code ==''&&newValue!=null){//说明是新增
  110. return {
  111. type:'add',
  112. code:newValue.toString(),
  113. libID:$('#libID').val(),
  114. billsLibId:parseInt($('#billsLibId').val())
  115. }
  116. }else {//说明是替换
  117. return {
  118. type:'update',
  119. oldCode:code.toString(),
  120. newCode:newValue.toString(),
  121. libID:$('#libID').val(),
  122. billsLibId:parseInt($('#billsLibId').val())
  123. }
  124. }
  125. }else if(isDef(code)){
  126. let updateData = {};
  127. updateData[field] = newValue;
  128. return {
  129. type:'update',
  130. oldCode:code.toString(),
  131. libID:$('#libID').val(),
  132. updateData:updateData
  133. }
  134. }
  135. },
  136. saveBills:async function (datas) {
  137. try {
  138. let result = await ajaxPost("/materialReplace/saveBills",datas);
  139. let missCodes = [];
  140. for(let r of result){
  141. if(r.missCodes && r.missCodes.length >0) missCodes =missCodes.concat(r.missCodes);
  142. if(r.type == 'add'){
  143. this.billsList = this.billsList.concat(r.list);
  144. }if(r.type == 'update'){
  145. for(let l of r.list){
  146. this.updateCache(l.code,l.updateData)
  147. }
  148. }if(r.type == 'delete'){
  149. }
  150. }
  151. if(missCodes.length > 0) alert(`没有找到清单:${missCodes.join("、")}`);
  152. this.refreshSheet();
  153. }catch (err){
  154. console.log(err);
  155. }
  156. },
  157. updateCache:function (code,updateData) {
  158. let bill = _.find(this.billsList,{'code':code});
  159. for(let key of updateData){
  160. bill[key] = updateData[key]
  161. }
  162. }
  163. };
  164. function isDef(obj) {
  165. return obj!==undefined && obj!==null;
  166. }
  167. materialOjb.initSpread();