ration_coe.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.onEditEnded);
  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. onEditEnded: 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. if (!confirm(`确定要删除选中的 ${args.rowCount} 条附注条件吗?`)){return; }
  80. var me = rationCoeOprObj;
  81. if (args.col == 0) {
  82. var curCache = me.cache["_Coe_" + me.curRation.ID];
  83. if (curCache) {
  84. for (var i = args.rowCount - 1; i >= 0; i--) {
  85. if (args.row + i < curCache.length) {
  86. curCache.splice(args.row + i, 1);
  87. };
  88. };
  89. me.updateCurRation();
  90. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  91. me.showCoeItems(me.curRation.ID);
  92. };
  93. };
  94. };
  95. },
  96. addCoeItems: function(coeIDs) {
  97. var me = this;
  98. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  99. var curCache = me.cache["_Coe_" + me.curRation.ID];
  100. var temp = [];
  101. if (curCache) {
  102. for (var i = 0; i < coeIDs.length; i++) {
  103. var isExist = false;
  104. for (let obj of curCache) {
  105. if (obj.ID == coeIDs[i]) {
  106. isExist = true;
  107. break;
  108. };
  109. };
  110. if (!isExist) {
  111. temp.push(coeIDs[i]);
  112. };
  113. };
  114. }else{
  115. for (let obj of coeIDs){temp.push(obj)};
  116. };
  117. if(temp.length == 0){
  118. me.showCoeItems(me.curRation.ID);
  119. sheetCommonObj.lockCells(me.sheet, me.setting);
  120. }else{
  121. $.ajax({
  122. type:"POST",
  123. url:"api/getCoeItemsByIDs",
  124. data: {"data": JSON.stringify({"libID": me.libID, "coeIDs": temp})},
  125. dataType:"json",
  126. cache:false,
  127. timeout:5000,
  128. success:function(result){
  129. if (result) {
  130. var rstArr = [];
  131. for (let obj of result.data){rstArr.push(obj)};
  132. if (curCache) {
  133. curCache = curCache.concat(rstArr);
  134. }else{
  135. curCache = rstArr;
  136. }
  137. curCache.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] = curCache;
  144. me.updateCurRation();
  145. me.showCoeItems(me.curRation.ID);
  146. };
  147. sheetCommonObj.lockCells(me.sheet, me.setting);
  148. },
  149. error:function(err){
  150. alert(err);
  151. }
  152. });
  153. };
  154. },
  155. getCoeItems: function(ration) {
  156. var me = this;
  157. me.curRation = ration;
  158. if (ration == undefined || ration.rationCoeList == undefined ||
  159. ration.rationCoeList.length == 0){return;};
  160. var coeList = ration.rationCoeList;
  161. var curCache = me.cache["_Coe_" + ration.ID];
  162. if (curCache && curCache.length > 0) {
  163. me.showCoeItems(ration.ID);
  164. sheetCommonObj.lockCells(me.sheet, me.setting);
  165. } else {
  166. var data = {"libID": me.libID, "coeIDs": coeList};
  167. $.ajax({
  168. type:"POST",
  169. url:"api/getCoeItemsByIDs",
  170. data: {"data": JSON.stringify(data)},
  171. dataType:"json",
  172. cache:false,
  173. timeout:5000,
  174. success:function(result){
  175. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  176. if (result.data) {
  177. var tempResult = [];
  178. for (let obj of result.data) {
  179. tempResult.push(obj);
  180. };
  181. me.cache["_Coe_" + ration.ID] = tempResult;
  182. me.showCoeItems(ration.ID);
  183. }
  184. sheetCommonObj.lockCells(me.sheet, me.setting);
  185. },
  186. error:function(err){
  187. alert(err);
  188. }
  189. });
  190. };
  191. },
  192. showCoeItems: function(rationID) {
  193. var me = this;
  194. var curCache = me.cache["_Coe_" + rationID];
  195. if (curCache) {
  196. sheetCommonObj.showData(me.sheet, me.setting, curCache);
  197. }
  198. },
  199. updateCurRation: function() {
  200. var me = this, updateArr = [];
  201. if (me.curRation) {
  202. var rst = [];
  203. var curCache = me.cache["_Coe_" + me.curRation.ID];
  204. if (curCache) {
  205. for (let obj of curCache) {
  206. rst.push(obj.ID);
  207. };
  208. me.curRation.rationCoeList = rst;
  209. updateArr.push(me.curRation);
  210. rationOprObj.mixUpdateRequest(updateArr, [], []);
  211. };
  212. };
  213. }
  214. }