ration_coe.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. sheetCommonObj.initSheet(me.sheet, me.setting, 30);
  26. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  27. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  28. me.sheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
  29. me.sheet.bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);
  30. },
  31. onClipboardPasting: function(sender, args) {
  32. var me = rationCoeOprObj;
  33. if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.curRation)) {
  34. args.cancel = true;
  35. }
  36. },
  37. onClipboardPasted: function(e, info) {
  38. var me = rationCoeOprObj;
  39. if (me.libID) {
  40. // 修改第一列(编号)
  41. if (info.cellRange.col == 0) {
  42. var coeIDs = [];
  43. var temp = sheetCommonObj.analyzePasteData({header:[{dataCode: "ID"}] }, info);
  44. for (let obj of temp) {
  45. coeIDs.push(obj.ID);
  46. };
  47. me.addCoeItems(coeIDs);
  48. } else {
  49. //修改其它列。
  50. }
  51. }
  52. },
  53. onCellEditEnd: function(sender, args){
  54. var me = rationCoeOprObj;
  55. if (args.col == 0) { // 编号列
  56. //delete
  57. if (args.editingText == null || args.editingText.trim() == "") {
  58. var curCache = me.cache["_Coe_" + me.curRation.ID];
  59. if (curCache) {
  60. if (args.row < curCache.length) {
  61. curCache.splice(args.row, 1);
  62. me.updateCurRation();
  63. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  64. me.showCoeItems(me.curRation.ID);
  65. }
  66. }
  67. } else {
  68. if (me.libID) {
  69. var ID = args.editingText.trim();
  70. if (!isNaN(ID)) {
  71. me.addCoeItems([ID]);
  72. };
  73. };
  74. };
  75. };
  76. },
  77. onRangeChanged: function(sender, args) {
  78. if (args.action == GC.Spread.Sheets.RangeChangedAction.clear) {
  79. var me = rationCoeOprObj, updateArr = [], removeArr = [];
  80. if (args.col == 0) {
  81. var curCache = me.cache["_Coe_" + me.curRation.ID];
  82. if (curCache) {
  83. for (var i = args.rowCount - 1; i >= 0; i--) {
  84. if (args.row + i < curCache.length) {
  85. curCache.splice(args.row + i, 1);
  86. };
  87. };
  88. me.updateCurRation();
  89. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  90. me.showCoeItems(me.curRation.ID);
  91. };
  92. };
  93. };
  94. },
  95. addCoeItems: function(coeIDs) {
  96. var me = this;
  97. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  98. var curCache = me.cache["_Coe_" + me.curRation.ID];
  99. var temp = [];
  100. if (curCache) {
  101. for (var i = 0; i < coeIDs.length; i++) {
  102. var isExist = false;
  103. for (let obj of curCache) {
  104. if (obj.ID == coeIDs[i]) {
  105. isExist = true;
  106. break;
  107. };
  108. };
  109. if (!isExist) {
  110. temp.push(coeIDs[i]);
  111. };
  112. };
  113. }else{
  114. for (let obj of coeIDs){temp.push(obj)};
  115. };
  116. if(temp.length == 0){
  117. me.showCoeItems(me.curRation.ID);
  118. sheetCommonObj.lockCells(me.sheet, me.setting);
  119. }else{
  120. $.ajax({
  121. type:"POST",
  122. url:"api/getCoeItemsByIDs",
  123. data: {"data": JSON.stringify({"libID": me.libID, "coeIDs": temp})},
  124. dataType:"json",
  125. cache:false,
  126. timeout:5000,
  127. success:function(result){
  128. if (result) {
  129. var rstArr = [];
  130. for (let obj of result.data){rstArr.push(obj)};
  131. if (curCache) {
  132. curCache = curCache.concat(rstArr);
  133. }else{
  134. curCache = rstArr;
  135. }
  136. curCache.sort(function(a, b) {
  137. var rst = 0;
  138. if (a.ID > b.ID) rst = 1
  139. else if (a.ID < b.ID) rst = -1;
  140. return rst;
  141. });
  142. me.cache["_Coe_" + me.curRation.ID] = curCache;
  143. me.updateCurRation();
  144. me.showCoeItems(me.curRation.ID);
  145. };
  146. sheetCommonObj.lockCells(me.sheet, me.setting);
  147. },
  148. error:function(err){
  149. alert(err);
  150. }
  151. });
  152. };
  153. },
  154. getCoeItems: function(ration) {
  155. var me = this;
  156. me.curRation = ration;
  157. if (ration == undefined || ration.rationCoeList == undefined ||
  158. ration.rationCoeList.length == 0){return;};
  159. var coeList = ration.rationCoeList;
  160. var curCache = me.cache["_Coe_" + ration.ID];
  161. if (curCache && curCache.length > 0) {
  162. me.showCoeItems(ration.ID);
  163. sheetCommonObj.lockCells(me.sheet, me.setting);
  164. } else {
  165. var data = {"libID": me.libID, "coeIDs": coeList};
  166. $.ajax({
  167. type:"POST",
  168. url:"api/getCoeItemsByIDs",
  169. data: {"data": JSON.stringify(data)},
  170. dataType:"json",
  171. cache:false,
  172. timeout:5000,
  173. success:function(result){
  174. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  175. if (result.data) {
  176. var tempResult = [];
  177. for (let obj of result.data) {
  178. tempResult.push(obj);
  179. };
  180. me.cache["_Coe_" + ration.ID] = tempResult;
  181. me.showCoeItems(ration.ID);
  182. }
  183. sheetCommonObj.lockCells(me.sheet, me.setting);
  184. },
  185. error:function(err){
  186. alert(err);
  187. }
  188. });
  189. };
  190. },
  191. showCoeItems: function(rationID) {
  192. var me = this;
  193. var curCache = me.cache["_Coe_" + rationID];
  194. if (curCache) {
  195. sheetCommonObj.showData(me.sheet, me.setting, curCache);
  196. }
  197. },
  198. updateCurRation: function() {
  199. var me = this, updateArr = [];
  200. if (me.curRation) {
  201. var rst = [];
  202. var curCache = me.cache["_Coe_" + me.curRation.ID];
  203. if (curCache) {
  204. for (let obj of curCache) {
  205. rst.push(obj.ID);
  206. };
  207. me.curRation.rationCoeList = rst;
  208. updateArr.push(me.curRation);
  209. rationOprObj.mixUpdateRequest(updateArr, [], []);
  210. };
  211. };
  212. }
  213. }