123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- /**
- * 工料机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: []
- };
- this.columnInfo = [];
- for(let tmp of this.header) {
- let width = tmp.field === 'name' ? 200 : 120;
- setting.header.push({headerName: tmp.name, headerWidth: width});
- this.columnInfo.push({name: tmp.field, displayName: tmp.name, visible: tmp.visible, cellType: tmp.cellType, size: width});
- }
- 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, 5, -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) {
- let row = this.sheet.getActiveRowIndex();
- let columnIndex = this.getFieldColumn(field);
- if (appendMode) {
- let oldValue = this.sheet.getValue(row, columnIndex);
- 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;
- };
|