summarySheet.js 7.6 KB

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