let equipmentPurchaseObj = { setting:{ header: [ {headerName: "编号", headerWidth: 160, dataCode: "code", dataType: "String",formatter: "@"}, {headerName: "名称", headerWidth: 200, dataCode: "name", dataType: "String"}, {headerName: "单位", headerWidth: 100, dataCode: "unit", dataType: "String"}, {headerName: "数量", headerWidth: 160, dataCode: "quantity", hAlign: "right", dataType: "Number",validator:'number'}, {headerName: "单价", headerWidth: 160, dataCode: "unitPrice", hAlign: "right", dataType: "Number",validator:'number'}, {headerName: "金额", headerWidth: 160, dataCode: "totalPrice", hAlign: "right", dataType: "Number"}, ], view: { lockColumns: ["totalPrice"], rowHeaderWidth:40, colHeaderHeight:35 } }, sheet:null, initSpread:function () { if(this.sheet == null){ this.spread = SheetDataHelper.createNewSpread($("#equipmentSpread")[0]); sheetCommonObj.spreadDefaultStyle(this.spread); this.sheet = this.spread.getSheet(0); sheetCommonObj.initSheet(this.sheet, this.setting, 0); this.sheet.bind(GC.Spread.Sheets.Events.ValueChanged,this.onValueChange); this.sheet.bind(GC.Spread.Sheets.Events.RangeChanged, this.onSheetRangeChange); if (projectReadOnly) { sheetCommonObj.disableSpread(this.spread); } else { this.initRightClick(); } } }, showData:function(){ let equipment_purchase = projectObj.project.equipment_purchase; this.data = equipment_purchase.datas.equipments; _.sortBy(this.data,['code']); sheetCommonObj.showData(this.sheet, this.setting,this.data); this.sheet.setRowCount(this.data.length); }, onValueChange:function (e,info) { let me = equipmentPurchaseObj,row = info.row, col = info.col; let dataCode = me.setting.header[col].dataCode; let value = info.newValue; let equipment = me.data[row]; if (value&&! sheetCommonObj.checkData(col,me.setting,value)) { alert('输入的数据类型不对,请重新输入!'); return me.showData(); } let data = {doc:{},ID:equipment.ID}; if(dataCode == 'quantity' || dataCode == 'unitPrice'){ me.calcTotalPrice(value,dataCode,data.doc,equipment); } if(equipment[dataCode] == value) return me.showData(); data.doc[dataCode] = value; me.updateEquipments([data]); }, calcTotalPrice:function(newValue,dataCode,doc,equipment){ let unitPrice = equipment.unitPrice?scMathUtil.roundForObj(equipment.unitPrice,getDecimal('glj.unitPrice')):0; let quantity = equipment.quantity?scMathUtil.roundForObj(equipment.quantity,getDecimal('glj.quantity')):0; if(newValue){ if(dataCode === 'quantity') { newValue = scMathUtil.roundForObj(newValue,getDecimal('glj.quantity')); quantity = newValue; if(gljUtil.isDef(doc.unitPrice)) unitPrice = doc.unitPrice; } if(dataCode === 'unitPrice') { newValue = scMathUtil.roundForObj(newValue,getDecimal('glj.unitPrice')); unitPrice = newValue; if(gljUtil.isDef(doc.quantity)) quantity = doc.quantity; } doc.totalPrice = scMathUtil.roundForObj(quantity * unitPrice,getDecimal('glj.unitPrice')); } }, onSheetRangeChange:function(e,args){ let updateMap = {}; let updateData = [] let me = equipmentPurchaseObj; for(let c of args.changedCells){ let dataCode = me.setting.header[c.col].dataCode; let value= args.sheet.getCell(c.row, c.col).text(); let equipment = me.data[c.row]; if (value&&!sheetCommonObj.checkData(c.col,me.setting,value)) { alert('输入的数据类型不对,请重新输入!'); me.showData(); return ; } let tem = updateMap[equipment.ID]?updateMap[equipment.ID]:{}; if(dataCode == 'quantity' || dataCode == 'unitPrice'){ me.calcTotalPrice(value,dataCode,tem,equipment); } tem[dataCode] = value; updateMap[equipment.ID] = tem; } for(let ID in updateMap){ let data = {doc:updateMap[ID],ID:ID}; updateData.push(data); } if(updateData.length > 0) me.updateEquipments(updateData); }, newEquipment:function(){ return {ID:uuid.v1()} }, updateEquipments:async function(updateData){ try { $.bootstrapLoading.start(); let projectID = projectObj.project.ID(); await ajaxPost('/equipmentPurchase/updateEquipments', { projectID, updateData }); for(let data of updateData){ let equipment = _.find(this.data,{ID:data.ID}); if(equipment){ Object.assign(equipment,data.doc); } } } catch (error) { alert('更新失败,请重试'); } this.showData(); $.bootstrapLoading.end(); }, insertEquipments:async function(equipments){ try { $.bootstrapLoading.start(); let projectID = projectObj.project.ID(); await ajaxPost('/equipmentPurchase/insertData', { projectID, equipments }); this.data.push(...equipments) this.showData(); } catch (error) { alert('插入失败,请重试'); } $.bootstrapLoading.end(); }, deleteEquipment:async function(ID){ try { let projectID = projectObj.project.ID(); await ajaxPost('/equipmentPurchase/deleteEquipment', { projectID, ID }); _.remove(this.data,{ID}); this.showData(); } catch (error) { alert('删除失败,请重试'); } }, registerInputContextMenuItem:function(){ const insertEquipmentHtml = `插入    行`; let me = this; return sheetCommonObj.registerInputContextMenuItem('insertEquipment', insertEquipmentHtml, 'fa-sign-in', async function () { const number = +$('#insert-equipment-number').val(); if (!number) { return; } const newData = []; for (let i = 0; i < number; i++) { newData.push(me.newEquipment()); } me.insertEquipments(newData) }); }, initRightClick: function () { let me = this; $.contextMenu({ selector: '#equipmentSpread', build: function ($trigger, e) { me.rightClickTarget = SheetDataHelper.safeRightClickSelection($trigger, e, me.spread); return me.rightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.viewport || me.rightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader; }, items: { "insert": { type: me.registerInputContextMenuItem(), disabled: function () { return false; }, /* callback: function (key, opt) { me.insertEquipments([me.newEquipment()]) } */ }, "delete": { name: "删除", icon: 'fa-times', disabled: function () { return me.rightClickTarget.row === undefined; }, callback: function (key, opt) { let row = me.rightClickTarget.row; me.deleteEquipment(me.data[row].ID); //me.preApplyInfoPrice(row); } } } }); }, } $(function () { $('#tab_equipment_purchase').on('shown.bs.tab', function (e) { sessionStorage.setItem('mainTab', '#tab_equipment_purchase'); $(e.relatedTarget.hash).removeClass('active'); equipmentPurchaseObj.initSpread(); equipmentPurchaseObj.showData(); }) })