priceArea.js 5.4 KB

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