ration.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. $("#gongliao").click(function(){
  5. $(this).attr('href', "/rationRepository/lmm" + "?repository=" + getQueryString("repository"))
  6. });
  7. var rationOprObj = {
  8. workBook: null,
  9. currentRations: {},
  10. currentSectionId: -1,
  11. setting: {
  12. header:[
  13. {headerName:"编码",headerWidth:120,dataCode:"code"},
  14. {headerName:"名称",headerWidth:300,dataCode:"name"},
  15. {headerName:"单位",headerWidth:120,dataCode:"unit"},
  16. {headerName:"基价",headerWidth:120,dataCode:"basePrice"},
  17. {headerName:"显示名称(以%s表示参数)",headerWidth:350,dataCode:"caption"},
  18. {headerName:"取费专业",headerWidth:120,dataCode:"feeType"}
  19. ],
  20. view:{
  21. comboBox:[
  22. {row:-1,col:2,rowCount:-1,colCount:1}
  23. ],
  24. lockedCells:[
  25. {row:-1,col:3,rowCount:-1, colCount:1}
  26. ]
  27. }
  28. },
  29. buildSheet: function(container) {
  30. var me = this;
  31. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30);
  32. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  33. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  34. },
  35. onClipboardPasting: function(sender, args) {
  36. var me = rationOprObj;
  37. if (args.cellRange.colCount != me.setting.header.length) {
  38. args.cancel = true;
  39. }
  40. },
  41. onClipboardPasted: function(e, info) {
  42. var me = rationOprObj;
  43. var cacheSection = me.currentRations["_SEC_ID_" + me.currentSectionId];
  44. var updateArr = [], addArr = [];
  45. var private_createRationItems = function() {
  46. var rst = [], propId = 0, preStrIdx = 0, itemObj = {};
  47. for (var i = 0; i < info.pasteData.text.length; i++) {
  48. if (info.pasteData.text[i] === "\n") {
  49. propId = 0;
  50. preStrIdx = i + 1;
  51. rst.push(itemObj);
  52. if (i < info.pasteData.text.length - 1) {
  53. itemObj = {};
  54. }
  55. } else if (info.pasteData.text[i] === "\t" || info.pasteData.text[i] === "\r") {
  56. itemObj[me.setting.header[propId].dataCode] = info.pasteData.text.slice(preStrIdx, i);
  57. propId++;
  58. preStrIdx = i + 1;
  59. //if the last copied-cell were empty, should check whether the end of text
  60. if (i == info.pasteData.text.length - 1) {
  61. itemObj[me.setting.header[propId].dataCode] = info.pasteData.text.slice(preStrIdx);
  62. rst.push(itemObj);
  63. }
  64. } else if (i == info.pasteData.text.length - 1) {
  65. itemObj[me.setting.header[propId].dataCode] = info.pasteData.text.slice(preStrIdx);
  66. rst.push(itemObj);
  67. }
  68. }
  69. return rst;
  70. };
  71. var items = private_createRationItems();
  72. for (var i = 0; i < items.length; i++) {
  73. if (cacheSection) {
  74. var hasCacheItem = false;
  75. for (var j = 0; j < cacheSection.length; j++) {
  76. if (cacheSection[j][me.setting.header[0].dataCode] == items[i][me.setting.header[0].dataCode]) {
  77. hasCacheItem = true;
  78. items[i]["ID"] = cacheSection[j]["ID"];
  79. break;
  80. }
  81. }
  82. if (!hasCacheItem) {
  83. addArr.push(items[i]);
  84. } else {
  85. updateArr.push(items[i]);
  86. }
  87. } else {
  88. addArr.push(items[i])
  89. }
  90. };
  91. if (updateArr.length > 0 || addArr.length > 0) {
  92. $.ajax({
  93. type:"POST",
  94. url:"api/mixUpdateRationItems",
  95. data:{"sectionID": me.currentSectionId, "updateItems": JSON.stringify(updateArr), "addItems": JSON.stringify(addArr)},
  96. dataType:"json",
  97. cache:false,
  98. timeout:5000,
  99. success:function(result){
  100. if (result.err) {
  101. alert(err);
  102. me.getRationItems(me.currentSectionId);
  103. } else {
  104. if (!(me.currentRations["_SEC_ID_" + me.currentSectionId])) {
  105. me.currentRations["_SEC_ID_" + me.currentSectionId] = [];
  106. cacheSection = me.currentRations["_SEC_ID_" + me.currentSectionId];
  107. }
  108. if (addArr.length > 0) {
  109. me.currentRations["_SEC_ID_" + me.currentSectionId] = cacheSection.concat(addArr);
  110. cacheSection = me.currentRations["_SEC_ID_" + me.currentSectionId];
  111. }
  112. for (var i = 0; i < updateArr.length; i++) {
  113. for (var j = 0; j < cacheSection.length; j++) {
  114. if (cacheSection[j][me.setting.header[0].dataCode] == updateArr[i][me.setting.header[0].dataCode]) {
  115. cacheSection[j] = updateArr[i];
  116. }
  117. }
  118. }
  119. me.showRationItems(me.currentSectionId);
  120. }
  121. },
  122. error:function(){
  123. }
  124. })
  125. }
  126. },
  127. getRationItems: function(sectionID){
  128. if (sectionID != -1) {
  129. var me = rationOprObj;
  130. me.currentSectionId = sectionID;
  131. if (me.currentRations["_SEC_ID_" + sectionID]) {
  132. me.showRationItems(sectionID);
  133. } else {
  134. $.ajax({
  135. type:"POST",
  136. url:"api/getRationItems",
  137. data:{"sectionID": sectionID},
  138. dataType:"json",
  139. cache:false,
  140. timeout:1000,
  141. success:function(result){
  142. if (result) {
  143. me.currentRations["_SEC_ID_" + sectionID] = result.data;
  144. me.showRationItems(sectionID);
  145. }
  146. },
  147. error:function(err){
  148. alert(err);
  149. }
  150. })
  151. }
  152. }
  153. },
  154. showRationItems: function(sectionID){
  155. var me = rationOprObj;
  156. if (me.workBook) {
  157. if (me.currentRations && me.currentRations["_SEC_ID_" + sectionID] && me.currentRations["_SEC_ID_" + sectionID].length > 0) {
  158. var cacheSection = me.currentRations["_SEC_ID_" + sectionID];
  159. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  160. sheetCommonObj.showData(me.workBook.getSheet(0), me.setting, cacheSection);
  161. } else {
  162. //清除ration数据及工料机数据
  163. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  164. }
  165. }
  166. }
  167. }