ration_template.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. if (dataCode === 'code') {//编码没有小写,自动转化为大写
  86. text = text.toString().toUpperCase();
  87. }
  88. rowData[dataCode] = text;
  89. } else {
  90. if (dataCode === 'code') {
  91. templateSheet.setValue(row, col + 1, '');
  92. }
  93. }
  94. }
  95. //不为空行
  96. if (rowExistData) {
  97. rst.push(rowData);
  98. }
  99. }
  100. return rst;
  101. }
  102. //编辑,定额编号自动匹配定额名称,匹配过程在后端进行,前端进行名称的更新输出
  103. //@return {void}
  104. function edit() {
  105. if (!curRation) {
  106. return;
  107. }
  108. let sheetData = getDataFromSheet();
  109. CommonAjax.post('/rationRepository/api/updateRationTemplate',
  110. {rationRepId: getQueryString('repository'), rationID: curRation.ID, templateData: sheetData}, function (rstData) {
  111. templateData = rstData;
  112. curRation.rationTemplateList = templateData;
  113. renderSheetFunc(templateSheet, function () {
  114. sheetCommonObj.showData(templateSheet, setting, templateData);
  115. });
  116. }, function () {
  117. renderSheetFunc(templateSheet, function () {
  118. sheetCommonObj.showData(templateSheet, setting, templateData);
  119. });
  120. });
  121. }
  122. let events = {
  123. onEnterCell: function (sender, args) {
  124. args.sheet.repaint();
  125. },
  126. onEditStarting: function (sender, args) {
  127. let dataCode = setting.header[args.col]['dataCode'];
  128. if (dataCode === 'name') {
  129. args.cancel = true;
  130. }
  131. },
  132. onEditEnded: function (sender, args) {
  133. edit();
  134. },
  135. onRangeChanged: function (sender, args) {
  136. edit();
  137. }
  138. };
  139. //重新绑定del建
  140. //@return {void}
  141. function bindRationTempDel() {
  142. if (locked || !templateSheet) {
  143. return;
  144. }
  145. let workBook = templateSheet.getParent();
  146. workBook.commandManager().register('rationTempDel', function () {
  147. renderSheetFunc(templateSheet, function () {
  148. let sels = templateSheet.getSelections();
  149. for (let sel of sels) {
  150. sel.row = sel.row === -1 ? 0 : sel.row;
  151. sel.col = sel.col === -1 ? 0 : sel.col;
  152. for (let row = sel.row; row < sel.rowCount + sel.row; row++) {
  153. for (let col = sel.col; col < sel.colCount + sel.col; col++) {
  154. templateSheet.setValue(row, col, '');
  155. }
  156. }
  157. }
  158. });
  159. edit();
  160. });
  161. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  162. workBook.commandManager().setShortcutKey('rationTempDel', GC.Spread.Commands.Key.del, false, false, false, false);
  163. }
  164. //建表
  165. //@param {Object}sheet @return {void}
  166. function buildSheet(sheet) {
  167. templateSheet = sheet;
  168. sheetCommonObj.initSheet(templateSheet, setting, 30);
  169. templateSheet.options.isProtected = true;
  170. setComboCells();
  171. const Events = GC.Spread.Sheets.Events;
  172. sheet.bind(Events.EditStarting, events.onEditStarting);
  173. sheet.bind(Events.EditEnded, events.onEditEnded);
  174. sheet.bind(Events.RangeChanged, events.onRangeChanged);
  175. sheet.bind(Events.EnterCell, events.onEnterCell);
  176. }
  177. //更改模板关联表锁定状态
  178. //@param {Boolean}locked @return {void}
  179. function changeLockMode(locked) {
  180. if (templateSheet) {
  181. if (!locked) {
  182. templateSheet.getRange(-1, 0, -1, 4).locked(false);
  183. //templateSheet.getRange(-1, 3, -1, 1).locked(false);
  184. } else {
  185. templateSheet.getRange(-1, 0, -1, 4).locked(true);
  186. //templateSheet.getRange(-1, 3, -1, 1).locked(true);
  187. }
  188. }
  189. }
  190. //变更定额
  191. function rationInitSel(ration) {
  192. curRation = ration ? ration : null;
  193. changeLockMode(!ration);
  194. sheetCommonObj.cleanData(templateSheet, setting, -1);
  195. if (ration) {
  196. setTemplateData(ration.rationTemplateList);
  197. sheetCommonObj.showData(templateSheet, setting, templateData);
  198. } else {
  199. setTemplateData([]);
  200. }
  201. }
  202. return {buildSheet, rationInitSel, bindRationTempDel, events}
  203. })();