summarySheet.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. function setAlign(sheet, headers) {
  2. const fuc = () => {
  3. headers.forEach(({ hAlign, vAlign }, index) => {
  4. sheetCommonObj.setAreaAlign(sheet.getRange(-1, index, -1, 1), hAlign, vAlign)
  5. });
  6. };
  7. sheetCommonObj.renderSheetFunc(sheet, fuc);
  8. }
  9. function setFormatter(sheet, headers) {
  10. const fuc = () => {
  11. headers.forEach(({ formatter }, index) => {
  12. if (formatter) {
  13. sheet.setFormatter(-1, index, formatter);
  14. }
  15. });
  16. };
  17. sheetCommonObj.renderSheetFunc(sheet, fuc);
  18. }
  19. function initSheet(dom, setting) {
  20. const workBook = sheetCommonObj.buildSheet(dom, setting);
  21. const sheet = workBook.getSheet(0);
  22. setAlign(sheet, setting.header);
  23. setFormatter(sheet, setting.header);
  24. return workBook;
  25. }
  26. function showData(sheet, data, headers, emptyRows) {
  27. const fuc = () => {
  28. sheet.setRowCount(data.length);
  29. data.forEach((item, row) => {
  30. headers.forEach(({ dataCode }, col) => {
  31. sheet.setValue(row, col, item[dataCode] || '');
  32. });
  33. });
  34. if (emptyRows) {
  35. sheet.addRows(data.length, emptyRows);
  36. }
  37. };
  38. sheetCommonObj.renderSheetFunc(sheet, fuc);
  39. }
  40. // 获取当前表中行数据
  41. function getRowData(sheet, row, headers) {
  42. const item = {};
  43. headers.forEach(({ dataCode }, index) => {
  44. const value = sheet.getValue(row, index) || '';
  45. if (value) {
  46. item[dataCode] = value;
  47. }
  48. });
  49. return item;
  50. }
  51. // 获取表数据和缓存数据的不同数据
  52. function getRowDiffData(curRowData, cacheRowData, headers) {
  53. let item = null;
  54. headers.forEach(({ dataCode }) => {
  55. const curValue = curRowData[dataCode];
  56. const cacheValue = cacheRowData[dataCode];
  57. if (!cacheValue && !curValue) {
  58. return;
  59. }
  60. if (cacheValue !== curValue) {
  61. if (!item) {
  62. item = {};
  63. }
  64. item[dataCode] = curValue || '';
  65. }
  66. });
  67. return item;
  68. }
  69. const UpdateType = {
  70. UPDATE: 'update',
  71. DELETE: 'delete',
  72. CREATE: 'create',
  73. };
  74. const TIME_OUT = 10000;
  75. const SUMMARY_BOOK = (() => {
  76. const locked = lockUtil.getLocked();
  77. const setting = {
  78. header: [
  79. { headerName: '主从对应码', headerWidth: 200, dataCode: 'masterSubCode', dataType: 'String', hAlign: 'left', vAlign: 'center', formatter: "@" },
  80. { headerName: '别名编码', headerWidth: 100, dataCode: 'classCode', dataType: 'String', hAlign: 'left', vAlign: 'center', formatter: "@" },
  81. { headerName: '计算式', headerWidth: 100, dataCode: 'expString', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  82. { headerName: '材料名称', headerWidth: 350, dataCode: 'name', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  83. { headerName: '规格型号', headerWidth: 200, dataCode: 'specs', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  84. { headerName: '单位', headerWidth: 80, dataCode: 'unit', dataType: 'String', hAlign: 'center', vAlign: 'center' },
  85. ],
  86. };
  87. // 初始化表格
  88. const workBook = initSheet($('#summary-spread')[0], setting);
  89. workBook.options.allowUserDragDrop = true;
  90. workBook.options.allowUserDragFill = true;
  91. lockUtil.lockSpreads([workBook], locked);
  92. const sheet = workBook.getSheet(0);
  93. // 当前数据缓存
  94. const cache = [];
  95. // 清空
  96. function clear() {
  97. cache.length = 0;
  98. sheet.setRowCount(0);
  99. }
  100. let loading = false;
  101. // 当前页面数据总量
  102. let totalCount = 0;
  103. // 当前页数
  104. let curPage = 0;
  105. // 搜索内容
  106. let searchStr = '';
  107. // 加载分页数据
  108. const loadPageData = async (page) => {
  109. curPage = page;
  110. loading = true;
  111. const data = await ajaxPost('/priceInfoSummary/getPagingData', { page, searchStr, pageSize: 100 }, TIME_OUT);
  112. totalCount = data.totalCount;
  113. cache.push(...data.items);
  114. showData(sheet, cache, setting.header, 5);
  115. loading = false;
  116. }
  117. // 搜索
  118. const handleSearch = (val) => {
  119. searchStr = val;
  120. clear();
  121. loadPageData(0);
  122. }
  123. // 无限滚动加载
  124. const onTopRowChanged = (sender, args) => {
  125. const bottomRow = args.sheet.getViewportBottomRow(1);
  126. console.log(cache.length, totalCount, loading, cache.length - 1, bottomRow)
  127. if (cache.length >= totalCount || loading) {
  128. return;
  129. }
  130. if (cache.length - 1 <= bottomRow) {
  131. loadPageData(curPage + 1, searchStr);
  132. }
  133. }
  134. sheet.bind(GC.Spread.Sheets.Events.TopRowChanged, _.debounce(onTopRowChanged, 100));
  135. // 编辑处理
  136. async function handleEdit(changedCells) {
  137. $.bootstrapLoading.start();
  138. const postData = []; // 请求用
  139. // 更新缓存用
  140. const updateData = [];
  141. const deleteData = [];
  142. const insertData = [];
  143. try {
  144. changedCells.forEach(({ row }) => {
  145. if (cache[row]) {
  146. const rowData = getRowData(sheet, row, setting.header);
  147. if (Object.keys(rowData).length) { // 还有数据,更新
  148. const diffData = getRowDiffData(rowData, cache[row], setting.header);
  149. if (diffData) {
  150. postData.push({ type: UpdateType.UPDATE, ID: cache[row].ID, data: diffData });
  151. updateData.push({ row, data: diffData });
  152. }
  153. } else { // 该行无数据了,删除
  154. postData.push({ type: UpdateType.DELETE, ID: cache[row].ID });
  155. deleteData.push(cache[row]);
  156. }
  157. } else { // 新增
  158. const rowData = getRowData(sheet, row, setting.header);
  159. if (Object.keys(rowData).length) {
  160. rowData.ID = uuid.v1();
  161. postData.push({ type: UpdateType.CREATE, data: rowData });
  162. insertData.push(rowData);
  163. }
  164. }
  165. });
  166. if (postData.length) {
  167. await ajaxPost('/priceInfoSummary/editSummaryData', { postData }, TIME_OUT);
  168. // 更新缓存,先更新然后删除,最后再新增,防止先新增后缓存数据的下标与更新、删除数据的下标对应不上
  169. updateData.forEach(item => {
  170. Object.assign(cache[item.row], item.data);
  171. });
  172. deleteData.forEach(item => {
  173. const index = cache.indexOf(item);
  174. if (index >= 0) {
  175. cache.splice(index, 1);
  176. }
  177. });
  178. insertData.forEach(item => cache.push(item));
  179. if (deleteData.length || insertData.length) {
  180. showData(sheet, cache, setting.header, 5);
  181. }
  182. }
  183. } catch (err) {
  184. // 恢复各单元格数据
  185. showData(sheet, cache, setting.header, 5);
  186. }
  187. $.bootstrapLoading.end();
  188. }
  189. sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function (e, info) {
  190. const changedCells = [{ row: info.row }];
  191. handleEdit(changedCells);
  192. });
  193. sheet.bind(GC.Spread.Sheets.Events.RangeChanged, function (e, info) {
  194. const changedRows = [];
  195. let preRow;
  196. info.changedCells.forEach(({ row }) => {
  197. if (row !== preRow) {
  198. changedRows.push({ row });
  199. }
  200. preRow = row;
  201. });
  202. handleEdit(changedRows);
  203. });
  204. const initData = async () => {
  205. try {
  206. $.bootstrapLoading.start();
  207. await loadPageData(0);
  208. } catch (error) {
  209. console.log(error);
  210. alert(error.message);
  211. }
  212. $.bootstrapLoading.end();
  213. }
  214. initData();
  215. return {
  216. sheet,
  217. handleSearch,
  218. }
  219. })()