ration_coe.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * Created by CSL on 2017-06-08.
  3. */
  4. var rationCoeOprObj = {
  5. sheet: null,
  6. libID: null,
  7. curRation: null,
  8. cache: {},
  9. setting: {
  10. header:[
  11. {headerName:"编码",headerWidth:120,dataCode:"ID", dataType: "Number"},
  12. {headerName:"名称",headerWidth:400,dataCode:"name", dataType: "String"},
  13. {headerName:"内容",headerWidth:300,dataCode:"content", dataType: "String"}
  14. ],
  15. view:{
  16. comboBox:[],
  17. lockColumns:[1,2]
  18. }
  19. },
  20. buildSheet: function(sheet) {
  21. var me = this;
  22. me.sheet = sheet;
  23. me.libID = storageUtil.getSessionCache("RationGrp","repositoryID"); // 不可靠,有时取不到
  24. if (me.libID == undefined){me.libID = getQueryString('repository')};
  25. //alert('0000 初始化libID: ' + JSON.stringify(me.libID));
  26. sheetCommonObj.initSheet(me.sheet, me.setting, 30);
  27. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  28. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  29. me.sheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
  30. me.sheet.bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);
  31. },
  32. onRangeChanged: function(sender, args) {
  33. if (args.action == GC.Spread.Sheets.RangeChangedAction.clear) {
  34. var me = rationCoeOprObj, updateArr = [], removeArr = [];
  35. if (args.col == 0) {
  36. var curCache = me.cache["_Coe_" + me.curRation.ID];
  37. if (curCache) {
  38. for (var i = args.rowCount - 1; i >= 0; i--) {
  39. if (args.row + i < curCache.length) {
  40. curCache.splice(args.row + i, 1);
  41. };
  42. };
  43. me.updateCurRation();
  44. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  45. me.showCoeItems(me.curRation.ID);
  46. };
  47. };
  48. };
  49. },
  50. onClipboardPasting: function(sender, args) {
  51. var me = rationCoeOprObj;
  52. if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.curRation)) {
  53. args.cancel = true;
  54. }
  55. },
  56. onClipboardPasted: function(e, info) {
  57. var me = rationCoeOprObj;
  58. if (me.libID) {
  59. // 修改第一列(编号)
  60. if (info.cellRange.col == 0) {
  61. var coeIDs = [];
  62. var temp = sheetCommonObj.analyzePasteData({header:[{dataCode: "ID"}] }, info);
  63. for (let obj of temp) {
  64. coeIDs.push(obj.ID);
  65. };
  66. //alert('0009 ' + JSON.stringify(IDs));
  67. me.addCoeItems(coeIDs);
  68. } else {
  69. //修改用量
  70. }
  71. }
  72. },
  73. onCellEditEnd: function(sender, args){
  74. var me = rationCoeOprObj;
  75. if (args.col == 0) { // 编号列
  76. //delete
  77. if (args.editingText == null || args.editingText.trim() == "") {
  78. var curCache = me.cache["_Coe_" + me.curRation.ID];
  79. if (curCache) {
  80. if (args.row < curCache.length) {
  81. curCache.splice(args.row, 1);
  82. me.updateCurRation();
  83. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  84. me.showCoeItems(me.curRation.ID);
  85. }
  86. }
  87. } else {
  88. if (me.libID) {
  89. var ID = args.editingText.trim();
  90. if (!isNaN(ID)) {
  91. me.addCoeItems([ID]);
  92. };
  93. };
  94. };
  95. };
  96. },
  97. addCoeItems: function(coeIDs) {
  98. var me = this;
  99. $.ajax({
  100. type:"POST",
  101. url:"api/getCoeItemsByIDs",
  102. data: {"data": JSON.stringify({"libID": me.libID, "coeIDs": coeIDs})},
  103. dataType:"json",
  104. cache:false,
  105. timeout:5000,
  106. success:function(result){
  107. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  108. if (result) {
  109. var rstArr = [], newAddArr = [];
  110. for (let obj of result.data){rstArr.push(obj)};
  111. var curCache = me.cache["_Coe_" + me.curRation.ID];
  112. if (curCache) {
  113. for (var i = 0; i < rstArr.length; i++) {
  114. var hasDup = false;
  115. for (let obj of curCache) {
  116. if (obj.ID == rstArr[i].ID) {
  117. hasDup = true;
  118. break;
  119. }
  120. };
  121. if (!hasDup) {
  122. newAddArr.push(rstArr[i]);
  123. };
  124. };
  125. curCache.sort(function(a, b) {
  126. var rst = 0;
  127. if (a > b) rst = 1
  128. else if (a < b) rst = -1;
  129. return rst;
  130. });
  131. me.cache["_Coe_" + me.curRation.ID] = curCache.concat(newAddArr);
  132. if (newAddArr.length > 0) {
  133. me.updateCurRation();
  134. };
  135. }else{
  136. me.cache["_Coe_" + me.curRation.ID] = rstArr;
  137. me.updateCurRation();
  138. };
  139. me.showCoeItems(me.curRation.ID);
  140. };
  141. sheetCommonObj.lockCells(me.sheet, me.setting);
  142. },
  143. error:function(err){
  144. alert(err);
  145. }
  146. })
  147. },
  148. updateCurRation: function() {
  149. var me = this, updateArr = [];
  150. if (me.curRation) {
  151. var rst = [];
  152. var curCache = me.cache["_Coe_" + me.curRation.ID];
  153. if (curCache) {
  154. for (let obj of curCache) {
  155. rst.push(obj.ID);
  156. };
  157. me.curRation.rationCoeList = rst;
  158. updateArr.push(me.curRation);
  159. rationOprObj.mixUpdateRequest(updateArr, [], []);
  160. };
  161. };
  162. },
  163. getCoeItems: function(ration) {
  164. var me = this;
  165. me.curRation = ration;
  166. if (ration == undefined || ration.rationCoeList == undefined ||
  167. ration.rationCoeList.length == 0){return;};
  168. var coeList = ration.rationCoeList;
  169. var curCache = me.cache["_Coe_" + ration.ID];
  170. //alert('0001 缓存:' + JSON.stringify(curCache));
  171. if (curCache && curCache.length > 0) {
  172. me.showCoeItems(ration.ID);
  173. sheetCommonObj.lockCells(me.sheet, me.setting);
  174. } else {
  175. var data = {"libID": me.libID, "coeIDs": coeList};
  176. //alert('0002 进入后台:' + JSON.stringify(data));
  177. $.ajax({
  178. type:"POST",
  179. url:"api/getCoeItemsByIDs",
  180. data: {"data": JSON.stringify(data)},
  181. dataType:"json",
  182. cache:false,
  183. timeout:5000,
  184. success:function(result){
  185. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  186. //alert('0003 后台结果:' + JSON.stringify(result.data));
  187. if (result.data) {
  188. var tempResult = [];
  189. for (let obj of result.data) {
  190. tempResult.push(obj);
  191. };
  192. me.cache["_Coe_" + ration.ID] = tempResult;
  193. me.showCoeItems(ration.ID);
  194. }
  195. sheetCommonObj.lockCells(me.sheet, me.setting);
  196. },
  197. error:function(err){
  198. alert(err);
  199. }
  200. });
  201. };
  202. },
  203. showCoeItems: function(rationID) {
  204. var me = this;
  205. var curCache = me.cache["_Coe_" + rationID];
  206. if (curCache) {
  207. sheetCommonObj.showData(me.sheet, me.setting, curCache);
  208. }
  209. rationOprObj.workBook.focus(true);
  210. }
  211. }