project_spread.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. $(document).ready(() => {
  2. autoFlashHeight();
  3. const colSpread = SpreadJsObj.createNewSpread($('#spread-col')[0]);
  4. const colSheet = colSpread.getActiveSheet();
  5. const canCheck = function(data, col) {
  6. return data && data[col.relaField];
  7. };
  8. const colSpreadSetting = {
  9. cols: [
  10. { title: 'key', colSpan: '1', rowSpan: '2', field: 'key', hAlign: 0, width: 0, visible: false, formatter: '@', readOnly: true, },
  11. { title: '可显示列', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 200, formatter: '@', readOnly: true, },
  12. { title: '项目节\n清单', colSpan: '1', rowSpan: '2', field: 'bills_valid', hAlign: 1, width: 80, readOnly: true, cellType: 'signalCheckbox', show: canCheck, relaField: 'bills', },
  13. { title: '计量单元', colSpan: '1', rowSpan: '2', field: 'pos_valid', hAlign: 1, width: 80, readOnly: true, cellType: 'signalCheckbox', show: canCheck, relaField: 'pos', },
  14. { title: '配置项|是否显示', colSpan: '2|1', rowSpan: '1|1', field: 'valid', hAlign: 1, width: 60, cellType: 'checkbox', readOnly: true },
  15. { title: '|别名', colSpan: '|1', rowSpan: '1|1', field: 'alias', hAlign: 0, width: 120, formatter: '@', },
  16. ],
  17. headRows: 2,
  18. headRowHeight: [25, 25],
  19. defaultRowHeight: 21,
  20. headerFont: '12px 微软雅黑',
  21. font: '12px 微软雅黑',
  22. emptyRows: 0,
  23. getColor: function (sheet, data, row, col, defaultColor) {
  24. if (!data) return defaultColor;
  25. switch (col.field) {
  26. case 'bills_valid':
  27. case 'pos_valid':
  28. case 'valid':
  29. return data.fixed.indexOf('valid') >= 0 ? '#eeeeee' : defaultColor;
  30. case 'alias':
  31. return data.fixed.indexOf(col.field) >= 0 ? '#eeeeee' : defaultColor;
  32. default:
  33. return defaultColor;
  34. }
  35. },
  36. };
  37. SpreadJsObj.initSheet(colSheet, colSpreadSetting);
  38. const reloadData = function () {
  39. const data = [];
  40. colSet.forEach(x => {
  41. const baseCol = BaseSetCol.find(b => { return x.key === b.key });
  42. if (!baseCol) return;
  43. const colData = { ...x, ...baseCol };
  44. if (colData.bills && colData.bills_valid === undefined) colData.bills_valid = colData.valid;
  45. if (colData.pos && colData.pos_valid === undefined) colData.pos_valid = colData.valid;
  46. data.push(colData);
  47. });
  48. BaseSetCol.forEach(x => {
  49. const col = colSet.find(c => { return c.key === x.key });
  50. if (!col) data.push({ ...x, valid: 0});
  51. });
  52. SpreadJsObj.loadSheetData(colSheet, SpreadJsObj.DataType.Data, data);
  53. };
  54. const colSpreadObj = {
  55. refreshOperationValid: function (sheet, selection) {
  56. const setObjEnable = function (obj, enable) {
  57. if (enable) {
  58. obj.removeClass('disabled');
  59. } else {
  60. obj.addClass('disabled');
  61. }
  62. };
  63. const invalidAll = function () {
  64. setObjEnable($('a[name=base-opr][type=up-move]'), false);
  65. setObjEnable($('a[name=base-opr][type=down-move]'), false);
  66. };
  67. const sel = selection ? selection[0] : sheet.getSelections()[0];
  68. const row = sel ? sel.row : -1;
  69. const data = sheet.zh_data;
  70. const select = sheet.zh_data[row];
  71. if (!select) {
  72. invalidAll();
  73. return;
  74. }
  75. const pre = data[row-1];
  76. const next = data[row + 1];
  77. setObjEnable($('a[name=base-opr][type=up-move]'), !!pre && pre.fixed.indexOf('move') < 0 && select.fixed.indexOf('move') < 0);
  78. setObjEnable($('a[name=base-opr][type=down-move]'), !!next && next.fixed.indexOf('move') < 0 && select.fixed.indexOf('move') < 0);
  79. },
  80. selectionChanged: function (e, info) {
  81. if (info.newSelections) {
  82. if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row) {
  83. colSpreadObj.refreshOperationValid(info.sheet, info.newSelections);
  84. }
  85. }
  86. },
  87. baseOpr: function (sheet, type) {
  88. const sel = sheet.getSelections()[0];
  89. const data = sheet.zh_data;
  90. if (type === 'up-move') {
  91. [data[sel.row - 1], data[sel.row]] = [data[sel.row], data[sel.row - 1]];
  92. SpreadJsObj.reLoadRowsData(sheet, [sel.row - 1, sel.row]);
  93. sheet.setSelection(sel.row - 1, sel.col, sel.rowCount, sel.colCount);
  94. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, sel.row - 1]);
  95. } else if (type === 'down-move') {
  96. [data[sel.row + 1], data[sel.row]] = [data[sel.row], data[sel.row + 1]];
  97. SpreadJsObj.reLoadRowsData(sheet, [sel.row, sel.row + 1]);
  98. sheet.setSelection(sel.row + 1, sel.col, sel.rowCount, sel.colCount);
  99. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, sel.row + 1]);
  100. }
  101. colSpreadObj.refreshOperationValid(sheet);
  102. },
  103. editStarting(e, info) {
  104. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  105. const col = info.sheet.zh_setting.cols[info.col];
  106. const node = info.sheet.zh_data[info.row];
  107. if (!node) {
  108. info.cancel = true;
  109. return;
  110. }
  111. switch (col.field) {
  112. case 'alias':
  113. info.cancel = node.fixed.indexOf('alias') >= 0;
  114. break;
  115. }
  116. },
  117. editEnded: function (e, info) {
  118. if (!info.sheet.zh_setting) return;
  119. const col = info.sheet.zh_setting.cols[info.col];
  120. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  121. const node = sortData[info.row];
  122. // 未改变值则不提交
  123. const orgValue = node[col.field];
  124. const newValue = trimInvalidChar(info.editingText);
  125. if (orgValue == info.editingText || ((!orgValue || orgValue === '') && (newValue === ''))) {
  126. return;
  127. }
  128. node[col.field] = newValue;
  129. },
  130. deletePress: function (sheet) {
  131. if (!sheet.zh_setting) return;
  132. const sel = sheet.getSelections()[0], datas = [];
  133. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  134. let bDel = false;
  135. const node = sheet.zh_tree.nodes[iRow];
  136. const data = sheet.zh_tree.getNodeKeyData(node);
  137. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  138. const col = sheet.zh_setting.cols[iCol];
  139. const style = sheet.getStyle(iRow, iCol);
  140. if (style.locked || (['dgn_qty1', 'dgn_qty2'].indexOf(col.field) >= 0 && node.b_code)) continue;
  141. if (col.type === 'Number' && node.settle_status === settleStatus.finish) continue;
  142. if (['dgn_qty1', 'dgn_qty2'].indexOf(col.field) < 0 && sheet.zh_tree.checkNodeUsed(node, pos)) {
  143. toastr.warning('"' + (node.code || '') + (node.b_code || '') + ' ' + node.name +'"已计量,请勿修改');
  144. return;
  145. }
  146. data[col.field] = null;
  147. const exprInfo = getExprInfo(col.field);
  148. if (exprInfo) {
  149. data[exprInfo.expr] = '';
  150. }
  151. bDel = true;
  152. }
  153. if (bDel) datas.push(data);
  154. }
  155. if (datas.length > 0) {
  156. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  157. const refreshNode = sheet.zh_tree.loadPostData(result);
  158. colSpreadObj.refreshTree(sheet, refreshNode);
  159. colSpreadObj.loadExprToInput(sheet);
  160. });
  161. }
  162. },
  163. buttonClicked: function(e, info) {
  164. if (!info.sheet.zh_setting) return;
  165. const col = info.sheet.zh_setting.cols[info.col];
  166. if (col.field.indexOf('valid') < 0) return;
  167. if (col.field === 'pos_valid' && !col.pos) return;
  168. if (col.field === 'bills_valid' && !col.bills) return;
  169. const node = SpreadJsObj.getSelectObject(info.sheet);
  170. if (node.fixed.indexOf('valid') >= 0) return;
  171. if (col.field === 'valid') {
  172. node.valid = !node.valid;
  173. node.bills_valid = node.valid && node.bills;
  174. node.pos_valid = node.valid && node.pos;
  175. } else {
  176. node[col.field] = !node[col.field];
  177. if (node.bills_valid || node.pos_valid) node.valid = 1;
  178. if (!node.bills_valid && !node.pos_valid) node.valid = 0;
  179. }
  180. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  181. },
  182. };
  183. reloadData();
  184. colSpreadObj.refreshOperationValid(colSheet);
  185. colSpread.bind(spreadNS.Events.SelectionChanged, colSpreadObj.selectionChanged);
  186. colSpread.bind(spreadNS.Events.EditStarting, colSpreadObj.editStarting);
  187. colSpread.bind(spreadNS.Events.EditEnded, colSpreadObj.editEnded);
  188. colSpread.bind(spreadNS.Events.ButtonClicked, colSpreadObj.buttonClicked);
  189. $('a[name="base-opr"]').click(function () {
  190. colSpreadObj.baseOpr(colSheet, this.getAttribute('type'));
  191. });
  192. const getCurrentColSet = function() {
  193. const cols = { key: 0, name: 1, bills_valid: 2, pos_valid: 3, valid: 4, alias: 5};
  194. const rowCount = colSheet.getRowCount();
  195. const result = [];
  196. for (let iRow = 0; iRow < rowCount; iRow++) {
  197. const keyName = colSheet.getText(iRow, cols.key);
  198. const baseCol = BaseSetCol.find(x => { return x.key === keyName; });
  199. if (!baseCol) continue;
  200. const valid = colSheet.getText(iRow, cols.valid);
  201. const colSet = { key: keyName, valid: valid === '1' || valid === 'TRUE' ? 1 : 0 };
  202. if (baseCol.bills) {
  203. const bills_valid = colSheet.getText(iRow, cols.bills_valid);
  204. colSet.bills_valid = bills_valid === '1' || bills_valid === 'TRUE' ? 1 : 0;
  205. }
  206. if (baseCol.pos) {
  207. const pos_valid = colSheet.getText(iRow, cols.pos_valid);
  208. colSet.pos_valid = pos_valid === '1' || pos_valid === 'TRUE' ? 1 : 0;
  209. }
  210. if (baseCol.fixed.indexOf('alias') < 0) {
  211. const alias = colSheet.getText(iRow, cols.alias);
  212. if (alias) colSet.alias = alias;
  213. }
  214. result.push(colSet);
  215. }
  216. console.log(result);
  217. return result;
  218. };
  219. let previewSpreadSetting, previewBillsSpread, previewPosSpread;
  220. $('#preview-spread').on('shown.bs.modal', function () {
  221. if (!previewBillsSpread) {
  222. previewBillsSpread = SpreadJsObj.createNewSpread($('#preview-bills-spread')[0]);
  223. }
  224. const previewBillsSheet = previewBillsSpread.getActiveSheet();
  225. SpreadJsObj.initSheet(previewBillsSheet, previewSpreadSetting.billsSpread);
  226. if (!previewPosSpread) {
  227. previewPosSpread = SpreadJsObj.createNewSpread($('#preview-pos-spread')[0]);
  228. }
  229. const previewPosSheet = previewPosSpread.getActiveSheet();
  230. SpreadJsObj.initSheet(previewPosSheet, previewSpreadSetting.posSpread);
  231. });
  232. $('#spread-save').click(() => {
  233. const data = { sType, code: sCode, colSet: getCurrentColSet() };
  234. postData('/setting/spread/save', data, function(result) {
  235. colSet.length = 0;
  236. colSet.push(...result.colSet);
  237. reloadData();
  238. });
  239. });
  240. $('#spread-preview').click(() => {
  241. const data = { sType, colSet: getCurrentColSet() };
  242. postData('/setting/spread/preview', data, function(result) {
  243. previewSpreadSetting = result;
  244. $('#preview-spread').modal('show');
  245. })
  246. });
  247. $('#spread-reset').click(() => {
  248. const data = { sType, code: sCode };
  249. postData('/setting/spread/reset', data, function(result) {
  250. colSet.length = 0;
  251. colSet.push(...result.colSet);
  252. reloadData();
  253. });
  254. });
  255. });