priceArea.js 6.4 KB

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