priceArea.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // 地区表
  2. const AREA_BOOK = (() => {
  3. // 地区表格锁
  4. let areaLocked = true;
  5. const cache = areaList;
  6. const setting = {
  7. header: [
  8. { headerName: '序号', headerWidth: 60, dataCode: 'serialNo', dataType: 'Number', hAlign: 'center', vAlign: 'center' },
  9. { headerName: '地区', headerWidth: $('#area-spread').width() - 80, dataCode: 'name', dataType: 'String', hAlign: 'center', vAlign: 'center' },
  10. ]
  11. };
  12. // 初始化表格
  13. const workBook = initSheet($('#area-spread')[0], setting);
  14. lockUtil.lockSpreads([workBook], locked || areaLocked);
  15. workBook.options.allowExtendPasteRange = false;
  16. workBook.options.allowUserDragDrop = false;
  17. workBook.options.allowUserDragFill = false;
  18. const sheet = workBook.getSheet(0);
  19. // 排序显示
  20. cache.sort((a, b) => a.serialNo - b.serialNo);
  21. // 显示数据
  22. showData(sheet, cache, setting.header);
  23. // 编辑处理
  24. async function handleEdit(changedCells) {
  25. const updateData = [];
  26. let reSort = false;
  27. changedCells.forEach(({ row, col }) => {
  28. const field = setting.header[col].dataCode;
  29. let value = sheet.getValue(row, col);
  30. if (field === 'serialNo') {
  31. reSort = true;
  32. value = +value;
  33. }
  34. updateData.push({
  35. row,
  36. field,
  37. value,
  38. ID: cache[row].ID,
  39. });
  40. });
  41. try {
  42. await ajaxPost('/priceInfo/editArea', { updateData }, TIME_OUT);
  43. updateData.forEach(({ row, field, value }) => cache[row][field] = value);
  44. if (reSort) {
  45. cache.sort((a, b) => a.serialNo - b.serialNo);
  46. showData(sheet, cache, setting.header);
  47. }
  48. } catch (err) {
  49. // 恢复各单元格数据
  50. sheetCommonObj.renderSheetFunc(sheet, () => {
  51. changedCells.forEach(({ row, col, field }) => {
  52. sheet.setValue(row, col, cache[row][field]);
  53. });
  54. });
  55. }
  56. }
  57. const bindEdit = () => {
  58. sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function (e, info) {
  59. const changedCells = [{ row: info.row, col: info.col }];
  60. handleEdit(changedCells);
  61. });
  62. sheet.bind(GC.Spread.Sheets.Events.RangeChanged, function (e, info) {
  63. handleEdit(info.changedCells);
  64. });
  65. };
  66. bindEdit();
  67. const setCurAreaStorage = (curLibID, curAreaID) => {
  68. window.localStorage.setItem(`priceArea:${curLibID}`, curAreaID);
  69. };
  70. const geCurAreaStorage = (curLibID) => {
  71. const curAreaID = window.localStorage.getItem(`priceArea:${curLibID}`);
  72. return curAreaID;
  73. };
  74. const curArea = { ID: null, name: '' };
  75. // 焦点变更处理
  76. const debounceSelectionChanged = _.debounce(function (e, info) {
  77. const row = info.newSelections && info.newSelections[0] ? info.newSelections[0].row : 0;
  78. handleSelectionChanged(row);
  79. }, DEBOUNCE_TIME, { leading: true }); // leading = true : 先触发再延迟
  80. function handleSelectionChanged(row) {
  81. const areaItem = cache[row];
  82. curArea.ID = areaItem && areaItem.ID || null;
  83. curArea.name = areaItem && areaItem.name || '';
  84. setCurAreaStorage(libID, curArea.ID);
  85. CLASS_BOOK.initData(libID, curArea.ID);
  86. }
  87. sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, debounceSelectionChanged);
  88. // 第一次进来初始化显示
  89. const init = () => {
  90. const curAreaID = geCurAreaStorage(libID);
  91. if (!curAreaID) {
  92. handleSelectionChanged(0);
  93. return;
  94. }
  95. let row = cache.findIndex(item => item.ID === curAreaID);
  96. row = row >= 0 ? row : 0;
  97. handleSelectionChanged(row);
  98. sheet.setActiveCell(row, 1);
  99. sheet.showCell(row, 0, GC.Spread.Sheets.VerticalPosition.center);
  100. }
  101. // 新增
  102. async function insert() {
  103. const data = {
  104. compilationID,
  105. ID: uuid.v1(),
  106. name: '',
  107. };
  108. try {
  109. $.bootstrapLoading.start();
  110. await ajaxPost('/priceInfo/insertArea', { insertData: [data] });
  111. // 新增的数据总是添加在最后
  112. sheet.addRows(cache.length, 1);
  113. cache.push(data);
  114. const lastRow = cache.length - 1;
  115. sheet.setSelection(lastRow, 0, 1, 1);
  116. sheet.showRow(lastRow, GC.Spread.Sheets.VerticalPosition.top);
  117. handleSelectionChanged(lastRow);
  118. } catch (err) {
  119. alert(err);
  120. } finally {
  121. $.bootstrapLoading.end();
  122. }
  123. }
  124. // 删除
  125. async function del() {
  126. try {
  127. $.bootstrapLoading.start();
  128. await ajaxPost('/priceInfo/deleteArea', { deleteData: [curArea.ID] });
  129. const index = cache.findIndex(item => item.ID === curArea.ID);
  130. sheet.deleteRows(index, 1);
  131. cache.splice(index, 1);
  132. const row = sheet.getActiveRowIndex();
  133. handleSelectionChanged(row);
  134. } catch (err) {
  135. alert(err);
  136. } finally {
  137. $.bootstrapLoading.end();
  138. }
  139. }
  140. // 右键功能
  141. function buildContextMenu() {
  142. $.contextMenu({
  143. selector: '#area-spread',
  144. build: function ($triggerElement, e) {
  145. // 控制允许右键菜单在哪个位置出现
  146. const offset = $('#area-spread').offset();
  147. const x = e.pageX - offset.left;
  148. const y = e.pageY - offset.top;
  149. const target = sheet.hitTest(x, y);
  150. if (target.hitTestType === 3) { // 在表格内
  151. const sel = sheet.getSelections()[0];
  152. if (sel && sel.rowCount === 1 && typeof target.row !== 'undefined') {
  153. const orgRow = sheet.getActiveRowIndex();
  154. if (orgRow !== target.row) {
  155. sheet.setActiveCell(target.row, target.col);
  156. handleSelectionChanged(target.row);
  157. }
  158. }
  159. return {
  160. items: {
  161. lock: {
  162. name: areaLocked ? '解锁' : '锁定',
  163. icon: areaLocked ? "fa-unlock" : 'fa-lock',
  164. disabled: function () {
  165. return locked;
  166. },
  167. callback: function (key, opt) {
  168. areaLocked = !areaLocked;
  169. if (locked || areaLocked) {
  170. lockUtil.lockSpreads([workBook], locked || areaLocked);
  171. } else {
  172. lockUtil.unLockSpreads([workBook]);
  173. bindEdit();
  174. }
  175. }
  176. },
  177. insert: {
  178. name: '新增',
  179. icon: "fa-arrow-left",
  180. disabled: function () {
  181. return locked || areaLocked;
  182. },
  183. callback: function (key, opt) {
  184. insert();
  185. }
  186. },
  187. del: {
  188. name: '删除',
  189. icon: "fa-arrow-left",
  190. disabled: function () {
  191. return locked || areaLocked || !cache[target.row];
  192. },
  193. callback: function (key, opt) {
  194. del();
  195. }
  196. },
  197. }
  198. };
  199. }
  200. else {
  201. return false;
  202. }
  203. }
  204. });
  205. }
  206. buildContextMenu();
  207. return {
  208. init,
  209. handleSelectionChanged,
  210. curArea,
  211. cache,
  212. setCurAreaStorage,
  213. geCurAreaStorage
  214. }
  215. })();