ReportTable.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Created by Jacky.Gao on 2017-01-26.
  3. */
  4. import * as utils from '../Utils.js';
  5. import {
  6. afterRenderer
  7. } from './CellRenderer.js';
  8. import buildMenuConfigure from './ContextMenu.js';
  9. import Handsontable from 'handsontable';
  10. export default class ReportTable {
  11. constructor(container, callback) {
  12. this.container = container;
  13. this.hot = new Handsontable(container, {
  14. startCols: 1,
  15. startRows: 1,
  16. fillHandle: {
  17. autoInsertRow: false
  18. },
  19. colHeaders: true,
  20. rowHeaders: true,
  21. autoColumnSize: false,
  22. autoRowSize: false,
  23. manualColumnResize: true,
  24. manualRowResize: true,
  25. maxColsNumber: 700,
  26. outsideClickDeselects: false
  27. });
  28. this.buildMenu();
  29. this.hot.addHook("afterRenderer", afterRenderer);
  30. // 强制更新tableview
  31. let _render = this.hot.render.bind(this.hot);
  32. this.hot.render = () => {
  33. _render();
  34. // 强制表格重新绘制
  35. this.hot.view.render();
  36. }
  37. // let file = utils.getParameter("_u");
  38. // if (!file || file === null || file === '') {
  39. // file = 'classpath:template/template.ureport.xml';
  40. // } else {
  41. // window._reportFile = file;
  42. // }
  43. this.cellsMap = new Map();
  44. this.loadFile(callback);
  45. this.hot.addHook('afterRowResize', function (currentRow, newSize) {
  46. let rowHeights = this.getSettings().rowHeights;
  47. let oldRowHeights = rowHeights.concat([]);
  48. let newRowHeights = rowHeights.concat([]);
  49. newRowHeights.splice(currentRow, 1, newSize);
  50. this.updateSettings({
  51. rowHeights: newRowHeights,
  52. manualRowResize: newRowHeights
  53. });
  54. const _this = this;
  55. utils.undoManager.add({
  56. redo: function () {
  57. rowHeights = _this.getSettings().rowHeights;
  58. oldRowHeights = rowHeights.concat([]);
  59. newRowHeights.splice(currentRow, 1, newSize);
  60. _this.updateSettings({
  61. rowHeights: newRowHeights,
  62. manualRowResize: newRowHeights
  63. });
  64. utils.setDirty();
  65. },
  66. undo: function () {
  67. _this.updateSettings({
  68. rowHeights: oldRowHeights,
  69. manualRowResize: oldRowHeights
  70. });
  71. utils.setDirty();
  72. }
  73. });
  74. utils.setDirty();
  75. });
  76. this.hot.addHook('afterColumnResize', function (currentColumn, newSize) {
  77. let colWidths = this.getSettings().colWidths;
  78. let newColWidths = colWidths.concat([]);
  79. let oldColWidths = colWidths.concat([]);
  80. newColWidths.splice(currentColumn, 1, newSize);
  81. this.updateSettings({
  82. colWidths: newColWidths,
  83. manualColumnResize: newColWidths
  84. });
  85. const _this = this;
  86. utils.undoManager.add({
  87. redo: function () {
  88. colWidths = _this.getSettings().colWidths;
  89. newColWidths = colWidths.concat([]);
  90. oldColWidths = colWidths.concat([]);
  91. newColWidths.splice(currentColumn, 1, newSize);
  92. _this.updateSettings({
  93. colWidths: newColWidths,
  94. manualColumnResize: newColWidths
  95. });
  96. utils.setDirty();
  97. },
  98. undo: function () {
  99. _this.updateSettings({
  100. colWidths: oldColWidths,
  101. manualColumnResize: oldColWidths
  102. });
  103. utils.setDirty();
  104. }
  105. });
  106. utils.setDirty();
  107. });
  108. window.onresize = () => {
  109. this.hot.render();
  110. }
  111. }
  112. // 页面初始化`
  113. loadFile(callback) {
  114. utils.showLoading()
  115. const fileId = utils.getUrlParam('id')
  116. const token = utils.getUrlParam('token')
  117. const appCode = utils.getUrlParam('appCode')
  118. let requestUrl = fileId ? `${window._server}/Data/${fileId}` : `${window._server}/Data/init`;
  119. const _this = this;
  120. $.ajax({
  121. url: requestUrl,
  122. type: 'GET',
  123. headers: {
  124. 'Authorization': token,
  125. 'app-code': appCode
  126. },
  127. success: (res) => {
  128. const reportDef = res.data
  129. utils.initTable(reportDef, {}, !!fileId)
  130. // 编辑状态下额外处理
  131. if (fileId) {
  132. _this.baseInfo = res.baseInfo
  133. }
  134. _this.reportDef = reportDef
  135. _this._buildReportData(reportDef);
  136. if (callback) {
  137. callback.call(_this, reportDef);
  138. }
  139. _this.hot.render();
  140. if (reportDef.paper.bgImage) {
  141. $('.ht_master').css('background', `url(${reportDef.paper.bgImage}) 50px 26px no-repeat`);
  142. } else {
  143. $('.ht_master').css('background', 'transparent');
  144. }
  145. utils.hideLoading()
  146. },
  147. error: function (response) {
  148. if (response && response.responseText) {
  149. alert("服务端错误:" + response.responseText + "");
  150. } else {
  151. // alert(`${window.i18n.table.report.load}${file}${window.i18n.table.report.fail}`);
  152. }
  153. }
  154. });
  155. }
  156. _buildReportData(data) {
  157. this.cellsMap.clear();
  158. const rows = data.rows;
  159. const rowHeights = [];
  160. for (let row of rows) {
  161. const height = row.height;
  162. rowHeights.push(utils.pointToPixel(height));
  163. }
  164. const columns = data.columns;
  165. const colWidths = [];
  166. for (let col of columns) {
  167. const width = col.width;
  168. colWidths.push(utils.pointToPixel(width));
  169. }
  170. const cellsMap = data.cellsMap;
  171. const dataArray = [],
  172. mergeCells = [];
  173. for (let row of rows) {
  174. const rowData = [];
  175. for (let col of columns) {
  176. let key = row.rowNumber + "," + col.columnNumber;
  177. let cell = cellsMap[key];
  178. if (cell) {
  179. this.cellsMap.set(key, cell);
  180. rowData.push(cell.value.value || "");
  181. let rowspan = cell.rowSpan,
  182. colspan = cell.colSpan;
  183. if (rowspan > 0 || colspan > 0) {
  184. if (rowspan === 0) rowspan = 1;
  185. if (colspan === 0) colspan = 1;
  186. mergeCells.push({
  187. rowspan,
  188. colspan,
  189. row: row.rowNumber - 1,
  190. col: col.columnNumber - 1
  191. });
  192. }
  193. } else {
  194. rowData.push("");
  195. }
  196. }
  197. dataArray.push(rowData);
  198. }
  199. this.hot.loadData(dataArray);
  200. this.hot.updateSettings({
  201. colWidths,
  202. rowHeights,
  203. mergeCells,
  204. readOnly: true
  205. });
  206. }
  207. buildMenu() {
  208. this.hot.updateSettings({
  209. contextMenu: buildMenuConfigure()
  210. });
  211. }
  212. bindSelectionEvent(callback) {
  213. const _this = this;
  214. Handsontable.hooks.add("afterSelectionEnd", function (rowIndex, colIndex, row2Index, col2Index) {
  215. callback.call(_this, rowIndex, colIndex, row2Index, col2Index);
  216. }, this.hot);
  217. }
  218. };