priceArea.js 7.2 KB

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