/** * 工料机SpreadJS类 * * @author CaiAoLin * @date 2017/7/18 * @version */ /** * 构造函数 * * @param {Object} header * @return {void} */ function CommonSpreadJs (header) { this.header = header; this.spread = null; this.sheet = null; this.columnInfo = []; this.isChanging = false; } /** * 初始化 * * @param {String} target * @return {Object} */ CommonSpreadJs.prototype.init = function(target) { let setting = { header: [], view:{rowHeaderWidth:35} }; this.columnInfo = []; for(let tmp of this.header) { let width = tmp.width==undefined ? 120 : tmp.width; setting.header.push({headerName: tmp.name, headerWidth: width}); let options= {name: tmp.field, displayName: tmp.name, visible: tmp.visible, cellType: tmp.cellType, size: width}; if(tmp.decimalField){ let decimal = getDecimal(tmp.decimalField); options.formatter = getFormatter(decimal); } this.columnInfo.push(options); } this.spread = sheetCommonObj.buildSheet(document.getElementById(target), setting, 10); this.spread.options.scrollbarShowMax = true; this.spread.options.scrollbarMaxAlign = true; this.spread.options.showHorizontalScrollbar = true; this.spread.options.scrollIgnoreHidden = true; this.sheet = this.spread.getActiveSheet(); this.sheet.autoGenerateColumns = false; // 设置表单不可编辑 this.sheet.options.isProtected = true; this.sheet.options.protectionOptions = { allowSelectLockedCells: true, allowSelectUnlockedCells: true, allowResizeRows: true, allowResizeColumns: true }; return this.spread; }; /** * 获取对应字段的列号 * * @param {String} field * @return {Number} */ CommonSpreadJs.prototype.getFieldColumn = function(field) { let result = -1; if (this.header.length <= 0) { return result; } for (let tmp in this.header) { if (this.header[tmp].field === field) { result = tmp; break; } } result = parseInt(result); return result; }; /** * 根据列号获取对应的字段 * * @param {Number} column * @return {String} */ CommonSpreadJs.prototype.getColumnField = function(column) { let field = this.header[column] !== undefined && this.header[column].field !== undefined ? this.header[column].field : ''; return field; }; /** * 设置指定列可编辑 * * @param {Number} column * @return {void} */ CommonSpreadJs.prototype.setColumnEditable = function(column) { this.sheet.getRange(-1, column, -1, 1).locked(false); }; /** * 设置指定列可编辑 * * * @param {Number} column * @return {void} */ CommonSpreadJs.prototype.setColumnLock = function(row, column) { this.sheet.getRange(-1, column, -1, 1).locked(true); }; /** * 设置样式 * * @param {Number} row * @param {Number} column * @param {Object} styleSetting */ CommonSpreadJs.prototype.setStyle = function(row, column, styleSetting) { let style = new GC.Spread.Sheets.Style(); style.locked = styleSetting.readOnly === undefined ? true : styleSetting.readOnly; style.hAlign = styleSetting.hAlign === undefined ? GC.Spread.Sheets.HorizontalAlign.center : styleSetting.hAlign; this.sheet.setStyle(row, column, style, GC.Spread.Sheets.SheetArea.viewport); }; /** * 获取sheet对象 * * @return {Object} */ CommonSpreadJs.prototype.getSheet = function() { return this.sheet; }; /** * 设置数据 * * @param {Object} data * @return {void} */ CommonSpreadJs.prototype.setData = function(data) { this.sheet.setDataSource(data); this.sheet.bindColumns(this.columnInfo); this.spread.refresh(); }; /** * 绑定事件 * * @param {Number} event * @param {function} callback * @return {void} */ CommonSpreadJs.prototype.bind = function (event, callback) { this.sheet.bind(event, callback); }; /** * 查找指定数据并返回行号 * * @param {String} keyword * @param {Number} rowStart * @param {Number} columnStart * @return {Number} */ CommonSpreadJs.prototype.searchKeyword = function(keyword, rowStart = 0, columnStart = 0) { let condition = new GC.Spread.Sheets.Search.SearchCondition(); condition.searchString = keyword; condition.startSheetIndex = 0; condition.endSheetIndex = 0; condition.searchFlags = GC.Spread.Sheets.Search.SearchFlags.ignoreCase | GC.Spread.Sheets.Search.SearchFlags.blockRange; condition.searchOrder = GC.Spread.Sheets.Search.SearchOrder.nOrder; condition.searchTarget = GC.Spread.Sheets.Search.SearchFoundFlags.cellText; condition.sheetArea = GC.Spread.Sheets.SheetArea.viewport; condition.rowStart = rowStart; condition.columnStart = columnStart; let result = this.spread.search(condition); return result.foundRowIndex; }; /** * 过滤数据 * * @param {String} field * @param {Array} filterList * @return {void} */ CommonSpreadJs.prototype.filterData = function(field, filterList) { let fieldColumn = this.getFieldColumn(field); fieldColumn = parseInt(fieldColumn); let filter = new GC.Spread.Sheets.Filter.HideRowFilter(new GC.Spread.Sheets.Range(-1, fieldColumn, -1, 1)); this.sheet.rowFilter(filter); let rowFilter = this.sheet.rowFilter(); rowFilter.filterButtonVisible(false); rowFilter.removeFilterItems(fieldColumn); for (let tmp of filterList) { let condition = new GC.Spread.Sheets.ConditionalFormatting.Condition(GC.Spread.Sheets.ConditionalFormatting.ConditionType.numberCondition, { compareType: GC.Spread.Sheets.ConditionalFormatting.GeneralComparisonOperators.equalsTo, expected: tmp }); rowFilter.addFilterItem(fieldColumn, condition); } rowFilter.filter(fieldColumn); this.sheet.invalidateLayout(); this.sheet.repaint(); }; /** * 选中指定行 * * @param {Number} row * @return {void} */ CommonSpreadJs.prototype.selectRow = function(row) { this.sheet.setSelection(row, 0, 1, 1); }; /** * 验证数据 * * @param {Number} column * @param {String} value * @return {boolean} */ CommonSpreadJs.prototype.checkData = function(column, value) { let result = true; let validator = this.header[column].validator !== undefined ? this.header[column].validator : null; if (validator === null) { return result; } switch (validator) { case 'number': let regular = /^\d+(\.\d+)?$/; result = regular.test(value); break; case 'boolean': let booleanValue = [true, false]; result = booleanValue.indexOf(value) >= 0; break; } return result; }; /** * 设置对应字段数值 * * @param {String} field * @param {Number} value * @param {boolean} appendMode * @return {void} */ CommonSpreadJs.prototype.setCellByField = function(field, value, appendMode,row) { row = row?row:this.sheet.getActiveRowIndex(); let columnIndex = this.getFieldColumn(field); let decimal = field=="quantity"?getDecimal("glj.quantity"):getDecimal("glj.unitPrice") if (appendMode) { let oldValue = scMathUtil.roundForObj(this.sheet.getValue(row, columnIndex),decimal); value = scMathUtil.roundForObj(value,decimal); value = (oldValue + value).toDecimal(2); } this.sheet.setValue(row, columnIndex, value); }; /** * 获取当前选中行指定字段的值 * * @param {String} field * @return {Mixed} */ CommonSpreadJs.prototype.getActiveDataByField = function(field) { let result = null; let activeGLJRow = this.sheet.getActiveRowIndex(); let column = this.getFieldColumn(field); if (column < 0) { return result; } result = this.sheet.getValue(activeGLJRow, column); return result; }; CommonSpreadJs.prototype.setProjectGLJDiffPrice = function (data) { let projGLJ = projectObj.project.projectGLJ; data.unit_price.market_price = projGLJ.getMarketPrice(data); if(gljOprObj.calcPriceDiff(data)==true){//是否记算价差 data.base_price = projGLJ.getBasePrice(data); data.adjust_price = projGLJ.getAdjustPrice(data); }else { data.base_price = data.unit_price.market_price; data.adjust_price = data.unit_price.market_price; } return data; }; CommonSpreadJs.prototype.getUnitPrice = function (data) { let gljList=projectObj.project.projectGLJ.datas.gljList; var pg = _.find(gljList, { 'code': data.code, 'name': data.name, 'specs': data.specs, 'type': data.type, 'unit': data.unit }) if(pg){ return pg.unit_price; } return null; };