sp_data.js 6.6 KB

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