summarySheet.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 handleTrim = async () => {
  145. try {
  146. $.bootstrapLoading.progressStart('清除前后空格', true);
  147. $("#progress_modal_body").text('正在清除前后空格,请稍后...');
  148. await ajaxPost('/priceInfoSummary/trim', {}, 1000 * 60 * 5);
  149. setTimeout(() => {
  150. $.bootstrapLoading.progressEnd();
  151. window.location.reload()
  152. }, 1000)
  153. } catch (error) {
  154. console.log(error);
  155. $.bootstrapLoading.progressEnd();
  156. }
  157. }
  158. // 无限滚动加载
  159. const onTopRowChanged = (sender, args) => {
  160. const bottomRow = args.sheet.getViewportBottomRow(1);
  161. console.log(cache.length, totalCount, loading, cache.length - 1, bottomRow)
  162. if (cache.length >= totalCount || loading) {
  163. return;
  164. }
  165. if (cache.length - 1 <= bottomRow) {
  166. loadPageData(curPage + 1, searchStr);
  167. }
  168. }
  169. sheet.bind(GC.Spread.Sheets.Events.TopRowChanged, _.debounce(onTopRowChanged, 100));
  170. // 编辑处理
  171. async function handleEdit(changedCells) {
  172. $.bootstrapLoading.start();
  173. const postData = []; // 请求用
  174. // 更新缓存用
  175. const updateData = [];
  176. const deleteData = [];
  177. const insertData = [];
  178. try {
  179. changedCells.forEach(({ row }) => {
  180. if (cache[row]) {
  181. const rowData = getRowData(sheet, row, setting.header);
  182. if (Object.keys(rowData).length) { // 还有数据,更新
  183. const diffData = getRowDiffData(rowData, cache[row], setting.header);
  184. if (diffData) {
  185. postData.push({ type: UpdateType.UPDATE, ID: cache[row].ID, data: diffData });
  186. updateData.push({ row, data: diffData });
  187. }
  188. } else { // 该行无数据了,删除
  189. postData.push({ type: UpdateType.DELETE, ID: cache[row].ID });
  190. deleteData.push(cache[row]);
  191. }
  192. } else { // 新增
  193. const rowData = getRowData(sheet, row, setting.header);
  194. if (Object.keys(rowData).length) {
  195. rowData.ID = uuid.v1();
  196. postData.push({ type: UpdateType.CREATE, data: rowData });
  197. insertData.push(rowData);
  198. }
  199. }
  200. });
  201. if (postData.length) {
  202. await ajaxPost('/priceInfoSummary/editSummaryData', { postData }, 1000 * 60 * 2);
  203. // 更新缓存,先更新然后删除,最后再新增,防止先新增后缓存数据的下标与更新、删除数据的下标对应不上
  204. updateData.forEach(item => {
  205. Object.assign(cache[item.row], item.data);
  206. });
  207. deleteData.forEach(item => {
  208. const index = cache.indexOf(item);
  209. if (index >= 0) {
  210. cache.splice(index, 1);
  211. }
  212. });
  213. insertData.forEach(item => cache.push(item));
  214. if (deleteData.length || insertData.length) {
  215. showData(sheet, cache, setting.header, 5);
  216. }
  217. }
  218. } catch (err) {
  219. // 恢复各单元格数据
  220. showData(sheet, cache, setting.header, 5);
  221. }
  222. $.bootstrapLoading.end();
  223. }
  224. sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function (e, info) {
  225. const changedCells = [{ row: info.row }];
  226. handleEdit(changedCells);
  227. });
  228. sheet.bind(GC.Spread.Sheets.Events.RangeChanged, function (e, info) {
  229. const changedRows = [];
  230. let preRow;
  231. info.changedCells.forEach(({ row }) => {
  232. if (row !== preRow) {
  233. changedRows.push({ row });
  234. }
  235. preRow = row;
  236. });
  237. handleEdit(changedRows);
  238. });
  239. const initData = async () => {
  240. try {
  241. $.bootstrapLoading.start();
  242. await loadPageData(0);
  243. } catch (error) {
  244. console.log(error);
  245. alert(error.message);
  246. }
  247. $.bootstrapLoading.end();
  248. }
  249. initData();
  250. return {
  251. sheet,
  252. handleSearch,
  253. handleTrim,
  254. }
  255. })()