project_spread.js 12 KB

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