priceArea.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 curArea = { ID: null, name: '' };
  68. // 焦点变更处理
  69. const debounceSelectionChanged = _.debounce(function (e, info) {
  70. const row = info.newSelections && info.newSelections[0] ? info.newSelections[0].row : 0;
  71. handleSelectionChanged(row);
  72. }, DEBOUNCE_TIME, { leading: true }); // leading = true : 先触发再延迟
  73. function handleSelectionChanged(row) {
  74. const areaItem = cache[row];
  75. curArea.ID = areaItem && areaItem.ID || null;
  76. curArea.name = areaItem && areaItem.name || '';
  77. CLASS_BOOK.initData(libID, curArea.ID);
  78. }
  79. sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, debounceSelectionChanged);
  80. // 新增
  81. async function insert() {
  82. const data = {
  83. compilationID,
  84. ID: uuid.v1(),
  85. name: '',
  86. };
  87. try {
  88. $.bootstrapLoading.start();
  89. await ajaxPost('/priceInfo/insertArea', { insertData: [data] });
  90. // 新增的数据总是添加在最后
  91. sheet.addRows(cache.length, 1);
  92. cache.push(data);
  93. const lastRow = cache.length - 1;
  94. sheet.setSelection(lastRow, 0, 1, 1);
  95. sheet.showRow(lastRow, GC.Spread.Sheets.VerticalPosition.top);
  96. handleSelectionChanged(lastRow);
  97. } catch (err) {
  98. alert(err);
  99. } finally {
  100. $.bootstrapLoading.end();
  101. }
  102. }
  103. // 删除
  104. async function del() {
  105. try {
  106. $.bootstrapLoading.start();
  107. await ajaxPost('/priceInfo/deleteArea', { deleteData: [curArea.ID] });
  108. const index = cache.findIndex(item => item.ID === curArea.ID);
  109. sheet.deleteRows(index, 1);
  110. cache.splice(index, 1);
  111. const row = sheet.getActiveRowIndex();
  112. handleSelectionChanged(row);
  113. } catch (err) {
  114. alert(err);
  115. } finally {
  116. $.bootstrapLoading.end();
  117. }
  118. }
  119. // 右键功能
  120. function buildContextMenu() {
  121. $.contextMenu({
  122. selector: '#area-spread',
  123. build: function ($triggerElement, e) {
  124. // 控制允许右键菜单在哪个位置出现
  125. const offset = $('#area-spread').offset();
  126. const x = e.pageX - offset.left;
  127. const y = e.pageY - offset.top;
  128. const target = sheet.hitTest(x, y);
  129. if (target.hitTestType === 3) { // 在表格内
  130. const sel = sheet.getSelections()[0];
  131. if (sel && sel.rowCount === 1 && typeof target.row !== 'undefined') {
  132. const orgRow = sheet.getActiveRowIndex();
  133. if (orgRow !== target.row) {
  134. sheet.setActiveCell(target.row, target.col);
  135. handleSelectionChanged(target.row);
  136. }
  137. }
  138. return {
  139. items: {
  140. lock: {
  141. name: areaLocked ? '解锁' : '锁定',
  142. icon: areaLocked ? "fa-unlock" : 'fa-lock',
  143. disabled: function () {
  144. return locked;
  145. },
  146. callback: function (key, opt) {
  147. areaLocked = !areaLocked;
  148. if (locked || areaLocked) {
  149. lockUtil.lockSpreads([workBook], locked || areaLocked);
  150. } else {
  151. lockUtil.unLockSpreads([workBook]);
  152. bindEdit();
  153. }
  154. }
  155. },
  156. insert: {
  157. name: '新增',
  158. icon: "fa-arrow-left",
  159. disabled: function () {
  160. return locked || areaLocked;
  161. },
  162. callback: function (key, opt) {
  163. insert();
  164. }
  165. },
  166. del: {
  167. name: '删除',
  168. icon: "fa-arrow-left",
  169. disabled: function () {
  170. return locked || areaLocked || !cache[target.row];
  171. },
  172. callback: function (key, opt) {
  173. del();
  174. }
  175. },
  176. }
  177. };
  178. }
  179. else {
  180. return false;
  181. }
  182. }
  183. });
  184. }
  185. buildContextMenu();
  186. return {
  187. handleSelectionChanged,
  188. curArea,
  189. cache,
  190. }
  191. })();