ration_template.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/11/28
  7. * @version
  8. */
  9. /*
  10. * 定额下模板关联
  11. * */
  12. const RationTemplate = (function () {
  13. let curRation = null;
  14. function _isDef(v) {
  15. return typeof v !== 'undefined' && v !== null;
  16. }
  17. //当前定额下的定额模板关联数据
  18. let templateData = [];
  19. let templateSheet = null;
  20. //重新设置templateData
  21. function setTemplateData(newData) {
  22. templateData = _isDef(newData) && Array.isArray(newData) ? newData : [];
  23. }
  24. let setting = {
  25. header:[
  26. {headerName:"关联类别",headerWidth:110,dataCode:"type", dataType: "String", hAlign: "left", formatter: "@"},
  27. {headerName:"编码",headerWidth:90,dataCode:"code", dataType: "String", hAlign: "left", formatter: "@"},
  28. {headerName:"名称",headerWidth:240,dataCode:"name", dataType: "String", hAlign: "left", formatter: "@"},
  29. {headerName:"具体位置",headerWidth:90,dataCode:"billsLocation", dataType: "String", hAlign: "left", formatter: "@"}
  30. ],
  31. view:{},
  32. };
  33. //渲染时方法,停止渲染
  34. //@param {Object}sheet {Function}func @return {void}
  35. function renderSheetFunc(sheet, func){
  36. sheet.suspendEvent();
  37. sheet.suspendPaint();
  38. if(func){
  39. func();
  40. }
  41. sheet.resumeEvent();
  42. sheet.resumePaint();
  43. }
  44. const items = ['模板关联', '超高关联'];
  45. //设置下拉单元格
  46. //@return {void}
  47. function setComboCells() {
  48. if (!templateSheet) {
  49. return;
  50. }
  51. let combo = sheetCommonObj.getDynamicCombo();
  52. combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  53. templateSheet.getRange(-1, 0, -1, 1).cellType(combo);
  54. }
  55. //有效数据
  56. //@param {String}dataCode {String}text @return {String}
  57. function validateData(dataCode, text) {
  58. //类别只能为规定的两种关联,否则将类别自动恢复为''
  59. if (dataCode === 'type') {
  60. if (text !== '' && !items.includes(text)) {
  61. text = '';
  62. }
  63. }
  64. return text;
  65. }
  66. //从当前表格获取数据
  67. //@return {Array}
  68. function getDataFromSheet() {
  69. let rst = [];
  70. if (!templateSheet) {
  71. return rst;
  72. }
  73. let rowCount = templateSheet.getRowCount(),
  74. colCount = templateSheet.getColumnCount();
  75. for (let row = 0; row < rowCount; row++) {
  76. let rowData = {type: '', code: '', name: '', billsLocation: ''},
  77. rowExistData = false;
  78. for (let col = 0; col < colCount; col++) {
  79. let text = templateSheet.getValue(row, col),
  80. dataCode = setting.header[col]['dataCode'];
  81. text = _isDef(text) ? text : '';
  82. text = validateData(dataCode, text);
  83. if (text !== '') {
  84. rowExistData = true;
  85. rowData[dataCode] = text;
  86. } else {
  87. if (dataCode === 'code') {
  88. templateSheet.setValue(row, col + 1, '');
  89. }
  90. }
  91. }
  92. //不为空行
  93. if (rowExistData) {
  94. rst.push(rowData);
  95. }
  96. }
  97. return rst;
  98. }
  99. //编辑,定额编号自动匹配定额名称,匹配过程在后端进行,前端进行名称的更新输出
  100. //@return {void}
  101. function edit() {
  102. if (!curRation) {
  103. return;
  104. }
  105. let sheetData = getDataFromSheet();
  106. CommonAjax.post('/rationRepository/api/updateRationTemplate',
  107. {rationRepId: getQueryString('repository'), rationID: curRation.ID, templateData: sheetData}, function (rstData) {
  108. templateData = rstData;
  109. curRation.rationTemplateList = templateData;
  110. renderSheetFunc(templateSheet, function () {
  111. sheetCommonObj.showData(templateSheet, setting, templateData);
  112. });
  113. }, function () {
  114. renderSheetFunc(templateSheet, function () {
  115. sheetCommonObj.showData(templateSheet, setting, templateData);
  116. });
  117. });
  118. }
  119. let events = {
  120. onEnterCell: function (sender, args) {
  121. args.sheet.repaint();
  122. },
  123. onEditEnded: function (sender, args) {
  124. edit();
  125. },
  126. onRangeChanged: function (sender, args) {
  127. edit();
  128. }
  129. };
  130. //重新绑定del建
  131. //@return {void}
  132. function bindRationTempDel() {
  133. if (!templateSheet) {
  134. return;
  135. }
  136. let workBook = templateSheet.getParent();
  137. workBook.commandManager().register('rationTempDel', function () {
  138. renderSheetFunc(templateSheet, function () {
  139. let sels = templateSheet.getSelections();
  140. for (let sel of sels) {
  141. sel.row = sel.row === -1 ? 0 : sel.row;
  142. sel.col = sel.col === -1 ? 0 : sel.col;
  143. for (let row = sel.row; row < sel.rowCount + sel.row; row++) {
  144. for (let col = sel.col; col < sel.colCount + sel.col; col++) {
  145. templateSheet.setValue(row, col, '');
  146. }
  147. }
  148. }
  149. });
  150. edit();
  151. });
  152. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  153. workBook.commandManager().setShortcutKey('rationTempDel', GC.Spread.Commands.Key.del, false, false, false, false);
  154. }
  155. //建表
  156. //@param {Object}sheet @return {void}
  157. function buildSheet(sheet) {
  158. templateSheet = sheet;
  159. sheetCommonObj.initSheet(templateSheet, setting, 30);
  160. templateSheet.options.isProtected = true;
  161. setComboCells();
  162. const Events = GC.Spread.Sheets.Events;
  163. sheet.bind(Events.EditEnded, events.onEditEnded);
  164. sheet.bind(Events.RangeChanged, events.onRangeChanged);
  165. sheet.bind(Events.EnterCell, events.onEnterCell);
  166. }
  167. //更改模板关联表锁定状态
  168. //@param {Boolean}locked @return {void}
  169. function changeLockMode(locked) {
  170. if (templateSheet) {
  171. if (!locked) {
  172. templateSheet.getRange(-1, 0, -1, 2).locked(false);
  173. templateSheet.getRange(-1, 3, -1, 1).locked(false);
  174. } else {
  175. templateSheet.getRange(-1, 0, -1, 2).locked(true);
  176. templateSheet.getRange(-1, 3, -1, 1).locked(true);
  177. }
  178. }
  179. }
  180. //变更定额
  181. function rationInitSel(ration) {
  182. curRation = ration ? ration : null;
  183. changeLockMode(!ration);
  184. sheetCommonObj.cleanData(templateSheet, setting, -1);
  185. if (ration) {
  186. setTemplateData(ration.rationTemplateList);
  187. sheetCommonObj.showData(templateSheet, setting, templateData);
  188. } else {
  189. setTemplateData([]);
  190. }
  191. }
  192. return {buildSheet, rationInitSel, bindRationTempDel, events}
  193. })();