ration_coe.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 = curCache.concat(newAddArr);
  126. curCache.sort(function(a, b) {
  127. var rst = 0;
  128. if (a.ID > b.ID) rst = 1
  129. else if (a.ID < b.ID) rst = -1;
  130. return rst;
  131. });
  132. me.cache["_Coe_" + me.curRation.ID] = curCache;
  133. if (newAddArr.length > 0) {
  134. me.updateCurRation();
  135. };
  136. }else{
  137. rstArr.sort(function(a, b) {
  138. var rst = 0;
  139. if (a.ID > b.ID) rst = 1
  140. else if (a.ID < b.ID) rst = -1;
  141. return rst;
  142. });
  143. me.cache["_Coe_" + me.curRation.ID] = rstArr;
  144. me.updateCurRation();
  145. };
  146. me.showCoeItems(me.curRation.ID);
  147. };
  148. sheetCommonObj.lockCells(me.sheet, me.setting);
  149. },
  150. error:function(err){
  151. alert(err);
  152. }
  153. })
  154. },
  155. updateCurRation: function() {
  156. var me = this, updateArr = [];
  157. if (me.curRation) {
  158. var rst = [];
  159. var curCache = me.cache["_Coe_" + me.curRation.ID];
  160. if (curCache) {
  161. for (let obj of curCache) {
  162. rst.push(obj.ID);
  163. };
  164. me.curRation.rationCoeList = rst;
  165. updateArr.push(me.curRation);
  166. rationOprObj.mixUpdateRequest(updateArr, [], []);
  167. };
  168. };
  169. },
  170. getCoeItems: function(ration) {
  171. var me = this;
  172. me.curRation = ration;
  173. if (ration == undefined || ration.rationCoeList == undefined ||
  174. ration.rationCoeList.length == 0){return;};
  175. var coeList = ration.rationCoeList;
  176. var curCache = me.cache["_Coe_" + ration.ID];
  177. //alert('0001 缓存:' + JSON.stringify(curCache));
  178. if (curCache && curCache.length > 0) {
  179. me.showCoeItems(ration.ID);
  180. sheetCommonObj.lockCells(me.sheet, me.setting);
  181. } else {
  182. var data = {"libID": me.libID, "coeIDs": coeList};
  183. //alert('0002 进入后台:' + JSON.stringify(data));
  184. $.ajax({
  185. type:"POST",
  186. url:"api/getCoeItemsByIDs",
  187. data: {"data": JSON.stringify(data)},
  188. dataType:"json",
  189. cache:false,
  190. timeout:5000,
  191. success:function(result){
  192. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  193. //alert('0003 后台结果:' + JSON.stringify(result.data));
  194. if (result.data) {
  195. var tempResult = [];
  196. for (let obj of result.data) {
  197. tempResult.push(obj);
  198. };
  199. me.cache["_Coe_" + ration.ID] = tempResult;
  200. me.showCoeItems(ration.ID);
  201. }
  202. sheetCommonObj.lockCells(me.sheet, me.setting);
  203. },
  204. error:function(err){
  205. alert(err);
  206. }
  207. });
  208. };
  209. },
  210. showCoeItems: function(rationID) {
  211. var me = this;
  212. var curCache = me.cache["_Coe_" + rationID];
  213. if (curCache) {
  214. sheetCommonObj.showData(me.sheet, me.setting, curCache);
  215. }
  216. rationOprObj.workBook.focus(true);
  217. }
  218. }