/** * Created by Zhong on 2017/8/31. * 特征及内容 */ //todo:清单库相关联的操作 let contentOprObj = { workBook: null, currentCache: [],//按照serialNo排序 setting: { header: [ {headerName:"工作内容",headerWidth:160,dataCode:"content", dataType: "String", hAlign: "center", vAlign: "center"}, {headerName:"输出",headerWidth:80,dataCode:"isChecked", cellType:"checkBox", hAlign: "center", vAlign: "center"} ] }, buildSheet: function(container) { let me = contentOprObj; me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30); me.workBook.getSheet(0).setColumnWidth(0, 20, GC.Spread.Sheets.SheetArea.rowHeader); me.onContextmenuOpr();//右键菜单 }, //显示在jobSpread的数据 showContentData: function (sheet, setting, datas) { sheetCommonObj.showData(sheet, setting, datas); sheet.suspendPaint(); sheet.suspendEvent(); for(let i = 0, len = datas.length; i < len; i++){ sheet.getCell(i, 0).locked(true); } sheet.resumePaint(); sheet.suspendEvent(); }, //显示到清单工作内容列的数据 getColData: function (jobsArr) { let me = contentOprObj; let rstStr = "", count = 0; for(let i = 0, len = jobsArr.length; i < len; i++){ if(jobsArr[i].isChecked === true){ count ++; if(count === 1){ rstStr += "“"; } else{ rstStr += " "; } rstStr += count + " " + jobsArr[i].content + "\n"; } } if(rstStr.trim().length > 0){ let reg = /\n+$/g; let newStr = rstStr.replace(reg, "”"); return newStr; } return null; }, upMove: function (rowIdx) { let me = contentOprObj; let thisObj = me.currentCache[rowIdx], preObj = me.currentCache[rowIdx - 1], temp, contentTxt; temp = thisObj.serialNo; thisObj.serialNo = preObj.serialNo; preObj.serialNo = temp; me.sortCache(me.currentCache); if(thisObj.isChecked === true && preObj.isChecked === true){ me.refreshColData = true; contentTxt = me.getColData(me.currentCache); } let txtObj = contentTxt ? {field: 'jobContentText', text: contentTxt} : null; pageCCOprObj.updateCharacterContent(billId, {field: 'jobContent', updateArr: me.currentCache}, txtObj, contentOprObj); }, downMove: function (rowIdx) { let me = contentOprObj; let thisObj = me.currentCache[rowIdx], nextObj = me.currentCache[rowIdx + 1], temp, contentTxt; temp = thisObj.serialNo; thisObj.serialNo = nextObj.serialNo; nextObj.serialNo = temp; me.sortCache(me.currentCache); if(thisObj.isChecked === true && nextObj.isChecked === true){ me.refreshColData = true; contentTxt = me.getColData(me.currentCache); } let txtObj = contentTxt ? {field: 'jobContentText', text: contentTxt} : null; pageCCOprObj.updateCharacterContent(billId, {field: 'jobContent', updateArr: me.currentCache}, txtObj, contentOprObj); }, deleteContent: function (rowIdx) { let me = contentOprObj; let deleteObj = me.currentCache[rowIdx]; let contentTxt; me.currentCache.splice(rowIdx, 1); if(deleteObj.isChecked === true){//需要刷新显示 me.refreshColData = true; contentTxt = me.getColData(me.currentCache); } let txtObj = contentTxt ? {field: 'jobContentText', text: contentTxt} : null; pageCCOprObj.updateCharacterContent(billId, {field: 'jobContent', updateArr: me.currentCache}, txtObj, contentOprObj); }, //只有空行时才可以输入,即是新增时用,新增的空行只有输入了值,才保存 onEditEnded: function (sender, args) { let me = contentOprObj; let preObj = me.currentCache.length > 0 ? me.currentCache[me.currentCache.length - 1] : null; let contentTxt; if(args.editingText.toString().trim().length > 0){ let newObj = {content: args.editingText, isChecked: false}; newObj.serialNo = preObj ? preObj.serialNo + 1 : 1;//得到新增对象的排序号 me.currentCache.push(newObj); /*if(newObj.isChecked === true){//可能编辑内容前就打勾 me.refreshColData = true; contentTxt = me.getColData(me.currentCache); }*/ pageCCOprObj.updateCharacterContent(billId, {field: 'jobContent', updateArr: me.currentCache}, null, contentOprObj); } }, //复选框控制输出 onButtonClicked: function (sender, args) { let me = contentOprObj, contentTxt; if(args.sheet.isEditing()){ args.sheet.endEdit(true); } let isChecked = args.sheet.getValue(args.row, args.col); if(me.currentCache.length > args.row){ me.currentCache[args.row].isChecked = isChecked; me.refreshColData = true; contentTxt = me.getColData(me.currentCache); let txtObj = contentTxt ? {field: 'jobContentText', text: contentTxt} : null; pageCCOprObj.updateCharacterContent(billId, {field: 'jobContent', updateArr: me.currentCache}, txtObj, contentOprObj); } //else的情况就是新增 else{ //还没数据清空复选框 args.sheet.setValue(args.row, args.col, 0); } }, sortCache: function (cacheArr) { cacheArr.sort(function (a, b) { let rst = 0; if(a.serialNo > b.serialNo){ rst = 1; } else if(a.serialNo < b.serialNo){ rst = -1; } return rst; }); }, onContextmenuOpr: function () {//右键菜单 let me = contentOprObj; $.contextMenu({ selector: '#jobSpread', build: function($triggerElement, e){ //控制允许右键菜单在哪个位置出现 let clientX = e.originalEvent.clientX, clientY = e.originalEvent.clientY; let sheet = me.workBook.getSheet(0); let offset = $("#jobSpread").offset(), x = clientX - offset.left, y = clientY - offset.top; let target = sheet.hitTest(x, y); if(target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内 sheet.setActiveCell(target.row, target.col); //控制按钮是否可用 let insertDis = false, delDis = false, upDis = false, downDis = false; if(!me.currentCache ||target.row >= me.currentCache.length){//右键定位在有数据的行,删除键才显示可用 delDis = true; downDis = true; upDis = true; } else{//有数据 if(target.row === me.currentCache.length -1){//定位在最后一行,不可下移 downDis = true; } if(target.row === 0){//定位在第一行,不可上移 upDis = true; } } return { callback: function(){}, items: { "insert": {name: "插入", disabled: insertDis, icon: "fa-sign-in", callback: function (key, opt) { //插入空行 me.workBook.getSheet(0).addRows(me.workBook.getSheet(0).getRowCount(), 1); }}, "delete": {name: "删除", disabled: delDis, icon: "fa-remove", callback: function (key, opt) { me.deleteContent(target.row); }}, "upMove": {name: "上移", disabled: upDis, icon: "fa-arrow-up", callback: function (key, opt) { me.upMove(target.row); }}, "downMove": {name: "下移", disabled: downDis, icon: "fa-arrow-down", callback: function (key, opt) { me.downMove(target.row); }} } }; } else{ return false; } } }); } }; let characterOprObj = { workBook: null, currentCache: [], setting: { header: [ {headerName:"项目特征",headerWidth:160,dataCode:"character", dataType: "String", hAlign: "center", vAlign: "center"}, {headerName:"特征值",headerWidth:160,dataCode:"eigenvalue", dataType: "String", cellType: "comboBox", hAlign: "center", vAlign: "center"}, {headerName:"输出",headerWidth:80,dataCode:"isChecked", cellType:"checkBox", hAlign: "center", vAlign: "center"} ] }, buildSheet: function(container) { let me = characterOprObj; me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30); me.workBook.getSheet(0).setColumnWidth(0, 20, GC.Spread.Sheets.SheetArea.rowHeader); me.onContextmenuOpr(); }, //显示在itemSpread的数据 showCharacterData: function (sheet, setting, datas) { let me = characterOprObj; sheetCommonObj.showData(sheet, setting, datas); sheet.suspendPaint(); sheet.suspendEvent(); for(let i = 0, len = datas.length; i < len; i++){ let comboObj = me.getComboBox(datas[i]); comboObj.combo.editable(false);//不可编辑 sheet.getCell(i, 1).cellType(comboObj.combo).value(typeof comboObj.selectedValue !== 'undefined' ? comboObj.selectedValue : ''); sheet.getCell(i, 0).locked(true); } sheet.resumePaint(); sheet.suspendEvent(); }, //获得项目特征特征值comboBox getComboBox: function (characterItem) { let rst; let combo = new GC.Spread.Sheets.CellTypes.ComboBox(); let comboItems = [], eigenvalues = characterItem.eigenvalue; for(let i = 0, len = eigenvalues.length; i < len; i++){ comboItems.push(eigenvalues[i].value); if(eigenvalues[i].isSelected){ rst.selectedValue = eigenvalues[i].value; } } combo.items(comboItems); rst.combo = combo; return rst; }, //显示到清单项目特征列的数据 getColData: function (itemsArr) { let me = characterOprObj; let rstStr = "", count = 0; for(let i = 0, len = itemsArr.length; i < len; i++){ if(itemsArr[i].isChecked === true){ //获取选中的特征值 let eigenvalueStr = ""; let eigenvalue = itemsArr[i].eigenvalue; for(let j = 0, vLen = eigenvalue.length; j < vLen; j++){ if(eigenvalue[j].isSelected){ eigenvalueStr += eigenvalue[j].value; break; } } count ++; if(count === 1){ rstStr += "“"; } else{ rstStr += " "; } rstStr += count + " " + itemsArr[i].content + ": " + eigenvalueStr + "\n"; } } if(rstStr.trim().length > 0){ let reg = /\n+$/g; let newStr = rstStr.replace(reg, "”"); return newStr; } return null; }, upMove: function (rowIdx) { let me = characterOprObj; let thisObj = me.currentCache[rowIdx], preObj = me.currentCache[rowIdx - 1], temp, characterTxt; temp = thisObj.serialNo; thisObj.serialNo = preObj.serialNo; preObj.serialNo = temp; contentOprObj.sortCache(me.currentCache); if(thisObj.isChecked === true && preObj.isChecked === true){ me.refreshColData = true; characterTxt = me.getColData(me.currentCache); } let txtObj = characterTxt ? {field: 'itemCharacter', text: characterTxt} : null; pageCCOprObj.updateCharacterContent(billId, {field: 'itemCharacter', updateArr: me.currentCache}, txtObj, characterOprObj); }, downMove: function (rowIdx) { let me = characterOprObj; let thisObj = me.currentCache[rowIdx], nextObj = me.currentCache[rowIdx + 1], temp, characterTxt; temp = thisObj.serialNo; thisObj.serialNo = nextObj.serialNo; nextObj.serialNo = temp; contentOprObj.sortCache(me.currentCache); if(thisObj.isChecked === true && nextObj.isChecked === true){ me.refreshColData = true; characterTxt = me.getColData(me.currentCache); } let txtObj = characterTxt ? {field: 'itemCharacter', text: characterTxt} : null; pageCCOprObj.updateCharacterContent(billId, {field: 'itemCharacter', updateArr: me.currentCache}, txtObj, characterOprObj); }, deleteCharacter: function (rowIdx) { let me = characterOprObj, characterTxt; let deleteObj = me.currentCache[rowIdx]; me.currentCache.splice(rowIdx, 1); if(deleteObj.isChecked === true){//需要刷新显示 me.refreshColData = true; characterTxt = me.getColData(me.currentCache); } let txtObj = characterTxt ? {field: 'itemCharacter', text: characterTxt} : null; pageCCOprObj.updateCharacterContent(billId, {field: 'itemCharacter', updateArr: me.currentCache}, txtObj, characterOprObj); }, onEditStart: function (sender, args) { let me = characterOprObj; if(args.col === 1){//改变选择的特征值 let selectedValue = args.sheet.getValue(args.row, 1); if(selectedValue.toString().trim().length > 0){//当前有选择 me.currentSelectedValue = selectedValue; } else{ me.currentSelectedValue = null; } } }, onEnterCell: function (sender, args) { let me = characterOprObj; if(me.addRow && me.addRow !== args.row && typeof me.currentCache[me.addRow] !== 'undefined'){//新增空行后,当编辑完空行后保存 pageCCOprObj.updateCharacterContent(billId, {field: 'itemCharacter', updateArr: me.currentCache}, null, characterOprObj); } }, onEditEnded: function (sender, args) {//新增保存逻辑:勾选了复选框或者离开当前编辑行 let me = characterOprObj, characterTxt; //只有空行时才可以输入,即是新增时用,新增的空行只有输入了值,才保存 if(args.col === 0){ let preObj = me.currentCache.length > 0 ? me.currentCache[me.currentCache.length - 1] : null; if(args.editingText.toString().trim().length > 0){ let newObj = {character: args.editingText, isChecked: false}; newObj.serialNo = preObj ? preObj.serialNo + 1 : 1;//得到新增对象的排序号 newObj.eigenvalue = []; me.currentCache.push(newObj); } } else if(args.col === 1){//改变特征值 if(me.addRow && me.addRow === args.row){//新增时用户向空特征值输入数据 if(args.editingText.toString().trim().length > 0){ //只有项目特征列有数据,特征值才保存 if(typeof me.currentCache[args.row] !== 'undefined'){ let newValue = {value: args.editingText, isSelected: true}; me.currentCache[args.row].eigenvalue.push(newValue); } } } else{//平时选择 let eigenvalues = me.currentCache[args.row].eigenvalue; let isValid = false; //更新Selected if(me.currentSelectedValue && me.currentSelectedValue !== args.editingText){ for(let i = 0, len = eigenvalues.length; i < len; i++){ if(eigenvalues[i].value === me.currentSelectedValue){ eigenvalues[i].isSelected = false; } if(eigenvalues[i].value === args.editingText){ eigenvalues[i].isSelected = true; } } isValid = true; } else if(!me.currentSelectedValue){ for(let i = 0, len = eigenvalues.length; i < len; i++){ if(eigenvalues[i].value === args.editingText){ eigenvalues[i].isSelected = true; break; } } isValid = true; } if(isValid){ me.refreshColData = true; characterTxt = me.getColData(me.currentCache); let txtObj = characterTxt ? {field: 'itemCharacter', text: characterTxt} : null; pageCCOprObj.updateCharacterContent(billId, {field: 'itemCharacter', updateArr: me.currentCache}, txtObj, characterOprObj); } } } }, //复选框控制输出 onButtonClicked: function (sender, args) { let me = characterOprObj, characterTxt; if(args.sheet.isEditing()){ args.sheet.endEdit(true); } let isChecked = args.sheet.getValue(args.row, args.col); if(me.currentCache.length > args.row){ me.currentCache[args.row].isChecked = isChecked; me.refreshColData = true; characterTxt = me.getColData(me.currentCache); let txtObj = characterTxt ? {field: 'itemCharacter', text: characterTxt} : null; pageCCOprObj.updateCharacterContent(billId, {field: 'itemCharacter', updateArr: me.currentCache}, txtObj, characterOprObj); } //else的情况就是新增 else{ args.sheet.setValue(args.row, args.col, 0); } }, onContextmenuOpr: function () {//右键菜单 let me = characterOprObj; $.contextMenu({ selector: '#itemSpread', build: function($triggerElement, e){ //控制允许右键菜单在哪个位置出现 let clientX = e.originalEvent.clientX, clientY = e.originalEvent.clientY; let sheet = me.workBook.getSheet(0); let offset = $("#itemSpread").offset(), x = clientX - offset.left, y = clientY - offset.top; let target = sheet.hitTest(x, y); if(target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内 sheet.setActiveCell(target.row, target.col); //控制按钮是否可用 let insertDis = false, delDis = false, upDis = false, downDis = false; if(!me.currentCache ||target.row >= me.currentCache.length){//右键定位在有数据的行,删除键才显示可用 delDis = true; downDis = true; upDis = true; } else{//有数据 if(target.row === me.currentCache.length -1){//定位在最后一行,不可下移 downDis = true; } if(target.row === 0){//定位在第一行,不可上移 upDis = true; } } return { callback: function(){}, items: { "insert": {name: "插入", disabled: insertDis, icon: "fa-sign-in", callback: function (key, opt) { me.workBook.getSheet(0).addRows(me.workBook.getSheet(0).getRowCount(), 1); me.addRow = me.workBook.getSheet(0).getRowCount() - 1; }}, "delete": {name: "删除", disabled: delDis, icon: "fa-remove", callback: function (key, opt) { me.deleteCharacter(target.row); }}, "upMove": {name: "上移", disabled: upDis, icon: "fa-arrow-up", callback: function (key, opt) { me.upMove(target.row); }}, "downMove": {name: "下移", disabled: downDis, icon: "fa-arrow-down", callback: function (key, opt) { me.downMove(target.row); }} } }; } else{ return false; } } }); } }; let pageCCOprObj = { mainActiveCell: null,//mainSpread焦点单元格 //设置特征及内容currentCache setCacheAndShow: function (node) { let theCont = contentOprObj, theCha = characterOprObj; theCont.currentCache = typeof node.data.jobContent !== 'undefined' ? node.data.jobContent : []; theCha.currentCache = typeof node.data.itemCharacter !== 'undefined' ? node.data.itemCharacter : []; this.showData(theCont.workBook.getSheet(0), theCont.setting, theCont.currentCache); this.showData(theCha.workBook.getSheet(0), theCha.setting, theCha.currentCache); }, //contentSpread、itemSpread展示数据用 showData: function(sheet, setting, data) { let me = this, ch = GC.Spread.Sheets.SheetArea.viewport; sheet.suspendPaint(); sheet.suspendEvent(); sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data); for (let col = 0; col < setting.header.length; col++) { var hAlign = "left", vAlign = "center"; if (setting.header[col].hAlign) { hAlign = setting.header[col].hAlign; } else if (setting.header[col].dataType !== "String"){ hAlign = "right"; } vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign; sheetCommonObj.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign); if (setting.header[col].formatter) { sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport); } for (let row = 0; row < data.length; row++) { sheet.getCell(row, 0).locked(true);//locked let val = data[row][setting.header[col].dataCode]; if(setting.header[col].cellType === "checkBox"){ let checkBox = new GC.Spread.Sheets.CellTypes.CheckBox(); checkBox.isThreeState(false); sheet.getCell(row, col).cellType(checkBox).value(val); } else if(setting.header[col].cellType === 'comboBox'){ let comboObj = characterOprObj.getComboBox(data[row]); comboObj.combo.editable(false);//不可编辑 sheet.getCell(row, 1).cellType(comboObj.combo).value(typeof comboObj.selectedValue !== 'undefined' ? comboObj.selectedValue : ''); } else{ sheet.setValue(row, col, val, ch); } } } sheet.resumeEvent(); sheet.resumePaint(); }, clearData: function () { let theCon = contentOprObj, theCha = characterOprObj; sheetCommonObj.cleanSheet(theCon.workBook.getSheet(0), theCon.setting, -1); sheetCommonObj.cleanSheet(theCha.workBook.getSheet(0), theCha.setting, -1); }, //更新特征及内容数据 updateCharacterContent: function (billId, updateObj, txtObj, oprObj) { let me = pageCCOprObj, updateCol; if(txtObj){ updateCol = txtObj.field === 'itemCharacter' ? 4 : 5;//更新清单行特征列或内容列 } else{ updateCol = null; } $.ajax({ type: 'post', url: '/bills/updateCharacterContent', dataType: 'json', data: {billId: billId, updateObj: JSON.stringify(updateObj), txtObj: JSON.stringify(txtObj)}, success: function (result) { if(!result.error){ oprObj.addRow = null; me.showData(oprObj.workBook.getSheet(0), oprObj.setting, oprObj.currentCache);//刷新特征及内容Spread if(oprObj.refreshColData){ if(updateCol){ projectObj.mainSpread.getActiveSheet().setValue(me.mainActiveCell.row, updateCol, txtObj.text + ''); //刷新输出显示 } oprObj.refreshColData = false; } } } }); } }