common_spread.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * 工料机SpreadJS类
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/18
  6. * @version
  7. */
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} header
  12. * @return {void}
  13. */
  14. function CommonSpreadJs (header) {
  15. this.header = header;
  16. this.spread = null;
  17. this.sheet = null;
  18. this.columnInfo = [];
  19. this.isChanging = false;
  20. }
  21. /**
  22. * 初始化
  23. *
  24. * @param {String} target
  25. * @return {Object}
  26. */
  27. CommonSpreadJs.prototype.init = function(target) {
  28. let setting = {
  29. header: [],
  30. view:{rowHeaderWidth:35}
  31. };
  32. this.columnInfo = [];
  33. for(let tmp of this.header) {
  34. let width = tmp.width==undefined ? 120 : tmp.width;
  35. setting.header.push({headerName: tmp.name, headerWidth: width});
  36. let options= {name: tmp.field, displayName: tmp.name, visible: tmp.visible, cellType: tmp.cellType, size: width};
  37. if(tmp.decimalField){
  38. let decimal = getDecimal(tmp.decimalField);
  39. options.formatter = getFormatter(decimal);
  40. }
  41. this.columnInfo.push(options);
  42. }
  43. this.spread = sheetCommonObj.buildSheet(document.getElementById(target), setting, 10);
  44. this.spread.options.scrollbarShowMax = true;
  45. this.spread.options.scrollbarMaxAlign = true;
  46. this.spread.options.showHorizontalScrollbar = true;
  47. this.spread.options.scrollIgnoreHidden = true;
  48. this.sheet = this.spread.getActiveSheet();
  49. this.sheet.autoGenerateColumns = false;
  50. // 设置表单不可编辑
  51. this.sheet.options.isProtected = true;
  52. this.sheet.options.protectionOptions = {
  53. allowSelectLockedCells: true,
  54. allowSelectUnlockedCells: true,
  55. allowResizeRows: true,
  56. allowResizeColumns: true
  57. };
  58. return this.spread;
  59. };
  60. /**
  61. * 获取对应字段的列号
  62. *
  63. * @param {String} field
  64. * @return {Number}
  65. */
  66. CommonSpreadJs.prototype.getFieldColumn = function(field) {
  67. let result = -1;
  68. if (this.header.length <= 0) {
  69. return result;
  70. }
  71. for (let tmp in this.header) {
  72. if (this.header[tmp].field === field) {
  73. result = tmp;
  74. break;
  75. }
  76. }
  77. result = parseInt(result);
  78. return result;
  79. };
  80. /**
  81. * 根据列号获取对应的字段
  82. *
  83. * @param {Number} column
  84. * @return {String}
  85. */
  86. CommonSpreadJs.prototype.getColumnField = function(column) {
  87. let field = this.header[column] !== undefined && this.header[column].field !== undefined ?
  88. this.header[column].field : '';
  89. return field;
  90. };
  91. /**
  92. * 设置指定列可编辑
  93. *
  94. * @param {Number} column
  95. * @return {void}
  96. */
  97. CommonSpreadJs.prototype.setColumnEditable = function(column) {
  98. this.sheet.getRange(-1, column, -1, 1).locked(false);
  99. };
  100. /**
  101. * 设置指定列可编辑
  102. *
  103. *
  104. * @param {Number} column
  105. * @return {void}
  106. */
  107. CommonSpreadJs.prototype.setColumnLock = function(row, column) {
  108. this.sheet.getRange(-1, column, -1, 1).locked(true);
  109. };
  110. /**
  111. * 设置样式
  112. *
  113. * @param {Number} row
  114. * @param {Number} column
  115. * @param {Object} styleSetting
  116. */
  117. CommonSpreadJs.prototype.setStyle = function(row, column, styleSetting) {
  118. let style = new GC.Spread.Sheets.Style();
  119. style.locked = styleSetting.readOnly === undefined ? true : styleSetting.readOnly;
  120. style.hAlign = styleSetting.hAlign === undefined ? GC.Spread.Sheets.HorizontalAlign.center : styleSetting.hAlign;
  121. this.sheet.setStyle(row, column, style, GC.Spread.Sheets.SheetArea.viewport);
  122. };
  123. /**
  124. * 获取sheet对象
  125. *
  126. * @return {Object}
  127. */
  128. CommonSpreadJs.prototype.getSheet = function() {
  129. return this.sheet;
  130. };
  131. /**
  132. * 设置数据
  133. *
  134. * @param {Object} data
  135. * @return {void}
  136. */
  137. CommonSpreadJs.prototype.setData = function(data) {
  138. this.sheet.setDataSource(data);
  139. this.sheet.bindColumns(this.columnInfo);
  140. this.spread.refresh();
  141. };
  142. /**
  143. * 绑定事件
  144. *
  145. * @param {Number} event
  146. * @param {function} callback
  147. * @return {void}
  148. */
  149. CommonSpreadJs.prototype.bind = function (event, callback) {
  150. this.sheet.bind(event, callback);
  151. };
  152. /**
  153. * 查找指定数据并返回行号
  154. *
  155. * @param {String} keyword
  156. * @param {Number} rowStart
  157. * @param {Number} columnStart
  158. * @return {Number}
  159. */
  160. CommonSpreadJs.prototype.searchKeyword = function(keyword, rowStart = 0, columnStart = 0) {
  161. let condition = new GC.Spread.Sheets.Search.SearchCondition();
  162. condition.searchString = keyword;
  163. condition.startSheetIndex = 0;
  164. condition.endSheetIndex = 0;
  165. condition.searchFlags = GC.Spread.Sheets.Search.SearchFlags.ignoreCase | GC.Spread.Sheets.Search.SearchFlags.blockRange;
  166. condition.searchOrder = GC.Spread.Sheets.Search.SearchOrder.nOrder;
  167. condition.searchTarget = GC.Spread.Sheets.Search.SearchFoundFlags.cellText;
  168. condition.sheetArea = GC.Spread.Sheets.SheetArea.viewport;
  169. condition.rowStart = rowStart;
  170. condition.columnStart = columnStart;
  171. let result = this.spread.search(condition);
  172. return result.foundRowIndex;
  173. };
  174. /**
  175. * 过滤数据
  176. *
  177. * @param {String} field
  178. * @param {Array} filterList
  179. * @return {void}
  180. */
  181. CommonSpreadJs.prototype.filterData = function(field, filterList) {
  182. let fieldColumn = this.getFieldColumn(field);
  183. fieldColumn = parseInt(fieldColumn);
  184. let filter = new GC.Spread.Sheets.Filter.HideRowFilter(new GC.Spread.Sheets.Range(-1, fieldColumn, -1, 1));
  185. this.sheet.rowFilter(filter);
  186. let rowFilter = this.sheet.rowFilter();
  187. rowFilter.filterButtonVisible(false);
  188. rowFilter.removeFilterItems(fieldColumn);
  189. for (let tmp of filterList) {
  190. let condition = new GC.Spread.Sheets.ConditionalFormatting.Condition(GC.Spread.Sheets.ConditionalFormatting.ConditionType.numberCondition, {
  191. compareType: GC.Spread.Sheets.ConditionalFormatting.GeneralComparisonOperators.equalsTo,
  192. expected: tmp
  193. });
  194. rowFilter.addFilterItem(fieldColumn, condition);
  195. }
  196. rowFilter.filter(fieldColumn);
  197. this.sheet.invalidateLayout();
  198. this.sheet.repaint();
  199. };
  200. /**
  201. * 选中指定行
  202. *
  203. * @param {Number} row
  204. * @return {void}
  205. */
  206. CommonSpreadJs.prototype.selectRow = function(row) {
  207. this.sheet.setSelection(row, 0, 1, 1);
  208. };
  209. /**
  210. * 验证数据
  211. *
  212. * @param {Number} column
  213. * @param {String} value
  214. * @return {boolean}
  215. */
  216. CommonSpreadJs.prototype.checkData = function(column, value) {
  217. let result = true;
  218. let validator = this.header[column].validator !== undefined ? this.header[column].validator : null;
  219. if (validator === null) {
  220. return result;
  221. }
  222. switch (validator) {
  223. case 'number':
  224. let regular = /^\d+(\.\d+)?$/;
  225. result = regular.test(value);
  226. break;
  227. case 'boolean':
  228. let booleanValue = [true, false];
  229. result = booleanValue.indexOf(value) >= 0;
  230. break;
  231. }
  232. return result;
  233. };
  234. /**
  235. * 设置对应字段数值
  236. *
  237. * @param {String} field
  238. * @param {Number} value
  239. * @param {boolean} appendMode
  240. * @return {void}
  241. */
  242. CommonSpreadJs.prototype.setCellByField = function(field, value, appendMode,row) {
  243. row = row?row:this.sheet.getActiveRowIndex();
  244. let columnIndex = this.getFieldColumn(field);
  245. let decimal = field=="quantity"?getDecimal("glj.quantity"):getDecimal("glj.unitPrice")
  246. if (appendMode) {
  247. let oldValue = scMathUtil.roundForObj(this.sheet.getValue(row, columnIndex),decimal);
  248. value = scMathUtil.roundForObj(value,decimal);
  249. value = (oldValue + value).toDecimal(2);
  250. }
  251. this.sheet.setValue(row, columnIndex, value);
  252. };
  253. /**
  254. * 获取当前选中行指定字段的值
  255. *
  256. * @param {String} field
  257. * @return {Mixed}
  258. */
  259. CommonSpreadJs.prototype.getActiveDataByField = function(field) {
  260. let result = null;
  261. let activeGLJRow = this.sheet.getActiveRowIndex();
  262. let column = this.getFieldColumn(field);
  263. if (column < 0) {
  264. return result;
  265. }
  266. result = this.sheet.getValue(activeGLJRow, column);
  267. return result;
  268. };
  269. CommonSpreadJs.prototype.setProjectGLJDiffPrice = function (data) {
  270. let projGLJ = projectObj.project.projectGLJ;
  271. data.unit_price.market_price = projGLJ.getMarketPrice(data);
  272. if(gljOprObj.calcPriceDiff(data)==true){//是否记算价差
  273. data.base_price = projGLJ.getBasePrice(data);
  274. data.adjust_price = projGLJ.getAdjustPrice(data);
  275. }else {
  276. data.base_price = data.unit_price.market_price;
  277. data.adjust_price = data.unit_price.market_price;
  278. }
  279. return data;
  280. };
  281. CommonSpreadJs.prototype.getUnitPrice = function (data) {
  282. let gljList=projectObj.project.projectGLJ.datas.gljList;
  283. var pg = _.find(gljList, {
  284. 'code': data.code,
  285. 'name': data.name,
  286. 'specs': data.specs,
  287. 'type': data.type,
  288. 'unit': data.unit
  289. })
  290. if(pg){
  291. return pg.unit_price;
  292. }
  293. return null;
  294. };