sp_data.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. $(document).ready(() => {
  2. autoFlashHeight();
  3. class QtyObj {
  4. constructor(setting) {
  5. this.spread = SpreadJsObj.createNewSpread(setting.obj);
  6. this.sheet = this.spread.getActiveSheet();
  7. SpreadJsObj.initSheet(this.sheet, setting.spreadSetting);
  8. this.data = setting.data;
  9. this.type = setting.type;
  10. SpreadJsObj.loadSheetData(this.sheet, SpreadJsObj.DataType.Data, this.data);
  11. const self = this;
  12. this.sheet.bind(spreadNS.Events.EditEnded, function(e, info) {
  13. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  14. const node = info.sheet.zh_data[info.row];
  15. if (!node) return;
  16. const col = info.sheet.zh_setting.cols[info.col];
  17. const data = {};
  18. data.id = node.id;
  19. const oldValue = node ? node[col.field] : null;
  20. const newValue = trimInvalidChar(info.editingText);
  21. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  22. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  23. return;
  24. }
  25. if (col.type === 'Number') {
  26. const num = _.toNumber(newValue);
  27. if (num) {
  28. data[col.field] = num;
  29. }
  30. } else {
  31. data[col.field] = newValue;
  32. }
  33. postData('info/save', { type: self.type, updateData: [data]}, function (result) {
  34. self.loadUpdateData(result);
  35. SpreadJsObj.reLoadSheetData(info.sheet);
  36. }, function () {
  37. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  38. });
  39. });
  40. this.sheet.bind(spreadNS.Events.ClipboardPasting, function(e, info) {
  41. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  42. info.cancel = true;
  43. if (!setting || !sortData) return;
  44. const pasteData = info.pasteData.html
  45. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  46. : (info.pasteData.text === ''
  47. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  48. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  49. const uDatas = [];
  50. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  51. const curRow = info.cellRange.row + iRow;
  52. const node = sortData[curRow];
  53. let bPaste = false;
  54. const data = {};
  55. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  56. const curCol = info.cellRange.col + iCol;
  57. const colSetting = setting.cols[curCol];
  58. const value = trimInvalidChar(pasteData[iRow][iCol]);
  59. if (colSetting.type === 'Number') {
  60. const num = _.toNumber(value);
  61. if (num) {
  62. data[colSetting.field] = num;
  63. bPaste = true;
  64. }
  65. } else {
  66. data[colSetting.field] = value;
  67. bPaste = true;
  68. }
  69. }
  70. if (bPaste) {
  71. if (node) {
  72. data.id = node.id;
  73. uDatas.push(data);
  74. }
  75. }
  76. }
  77. if (uDatas.length > 0) {
  78. postData('info/save', { type: self.type, updateData: uDatas}, function (result) {
  79. self.loadUpdateData(result);
  80. SpreadJsObj.reLoadSheetData(info.sheet);
  81. });
  82. } else {
  83. SpreadJsObj.reLoadSheetData(info.sheet);
  84. }
  85. });
  86. SpreadJsObj.addDeleteBind(this.spread, function(sheet) {
  87. if (!sheet.zh_setting || !sheet.zh_data) return;
  88. const sortData = sheet.zh_data;
  89. const datas = [];
  90. const sels = sheet.getSelections();
  91. if (!sels || !sels[0]) return;
  92. for (let iRow = sels[0].row; iRow < sels[0].row + sels[0].rowCount; iRow++) {
  93. let bDel = false;
  94. const node = sortData[iRow];
  95. if (node) {
  96. const data = { id: node.id };
  97. for (let iCol = sels[0].col; iCol < sels[0].col + sels[0].colCount; iCol++) {
  98. const style = sheet.getStyle(iRow, iCol);
  99. if (!style.locked) {
  100. const colSetting = sheet.zh_setting.cols[iCol];
  101. data[colSetting.field] = colSetting.type === 'Number' ? 0 : '';
  102. bDel = true;
  103. }
  104. }
  105. if (bDel) {
  106. datas.push(data);
  107. }
  108. }
  109. }
  110. if (datas.length === 0) return;
  111. postData('info/save', { type: self.type, updateData: datas}, function (result) {
  112. self.loadUpdateData(result);
  113. SpreadJsObj.reLoadSheetData(sheet);
  114. }, function () {
  115. SpreadJsObj.reLoadSheetData(sheet);
  116. });
  117. });
  118. }
  119. loadUpdateData(data) {
  120. for (const d of data) {
  121. const source = this.data.find(x => { return x.id === d.id });
  122. if (!source) continue;
  123. source.dgn_qty = d.dgn_qty;
  124. source.final_qty = d.final_qty;
  125. }
  126. }
  127. }
  128. const spreadSetting = {
  129. cols: [
  130. {title: '工程名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 300, formatter: '@', readOnly: true},
  131. {title: '单位', colSpan: '1', rowSpan: '1', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  132. {title: '设计', colSpan: '1', rowSpan: '1', field: 'dgn_qty', hAlign: 2, width: 100, type: 'Number'},
  133. {title: '竣工', colSpan: '1', rowSpan: '1', field: 'final_qty', hAlign: 2, width: 100, type: 'Number'},
  134. ],
  135. emptyRows: 0,
  136. headRows: 1,
  137. headRowHeight: [32],
  138. defaultRowHeight: 21,
  139. headerFont: '12px 微软雅黑',
  140. font: '12px 微软雅黑',
  141. };
  142. const mainQtyObj = new QtyObj({
  143. obj: $('#main_qty_spread')[0],
  144. spreadSetting,
  145. data: mainQty,
  146. type: 'main_quantity',
  147. });
  148. const gclQtyObj = new QtyObj({
  149. obj: $('#gcl_qty_spread')[0],
  150. spreadSetting,
  151. data: gclQty,
  152. type: 'gcl_quantity',
  153. })
  154. });