使用vue写的新增页面,编辑页面等弹出页面,在页面中进行table数据的行编辑,利用JS中的Map做的实时响应的行编辑数据内容汇总计算、校验等。话不多说,上代码:
require(['vue', 'zview'],function(vue, zView) {
var store = require('store');
var testAdd = vue.extend({
data : function() {
return {
model:{
contInfoId:"",
contCode:"",
contName:"",
status:"1001",
custId:"",
custName:"",
useAreaName:"",
useAreaCode:"",
demandYear:"",
demandMonth:"",
startPlanTime:"",
endPlanTime:"",
},
loading : false,
lastsel:'',
totalPlanCountMap:{},
totalContInfoMap:{},
busiKey:['monDemPlanProdId'],
areaAndProdIDString:',',
workAreaSelectFlag:false,
dateStyle:"width:100%",
statusTypeList:JSON.parse(localStorage.getItem("main_param_t")).emphasisOrderStatusMap,
colModel:[
{label : '单号', name : 'monDemPlanNo', align: 'center',sortable:false,hidden:true},
{label : '工区(子客户)id', name : 'areaId',align: 'center',sortable:false, hidden:true},
{label : '计划数量', name : 'planCount', align: 'center',sortable:false,editable: true},
{label : '计划单价', name : 'planPrice', align: 'center',sortable:false, hidden:true},
{label : '计划销售额', name : 'planMonry', align: 'center',sortable:false, hidden:true},
{label : '已下单数量', name : 'orderTotalCount', align: 'center',sortable:false, hidden:true},
{label : '已发货数量', name : 'deliverTotalCount', align: 'center',sortable:false, hidden:true},
{label : '单据状态', name : 'status', align: 'center',sortable:false,hidden:true},
{label : '工作流程id', name : 'procInstId', align: 'center',sortable:false,hidden:true},
{label : '是否删除', name : 'deleteFlag', align: 'center',sortable:false, hidden:true},
{label : '创建人code', name : 'createUser', align: 'center',sortable:false, hidden:true},
{label : '创建人名称', name : 'createName', align: 'center',sortable:false, hidden:true},
{label : '创建时间', name : 'createTime', align: 'center',sortable:false, hidden:true},
{label : '更新人code', name : 'modifyUser', align: 'center',sortable:false, hidden:true},
{label : '更新人名称', name : 'modifyName', align: 'center',sortable:false, hidden:true},
{label : '更新时间', name : 'modifyTime', align: 'center',sortable:false, hidden:true},
{label : '所属组织id', name : 'orgId', align: 'center',sortable:false, hidden:true},
{label : '所属组织编码', name : 'orgCode', align: 'center',sortable:false, hidden:true},
{label : '所属组织名称', name : 'orgName', align: 'center',sortable:false, hidden:true},
{label : '时间戳', name : 'versionTime', align: 'center',sortable:false, hidden:true},
],
ruleValidate: {},
createMode:store.store.createMode,
imgName: '',
visible: false,
uploadListPic: [],
uploadListFile: [],
fileuploadOption:emphasisConstants.fileuploadOption,
imgSrc:"",
payWayName:"",
actionUrl:testAPI.upload,
}
},
mounted : function() {
var table = this.$refs['testAddTable'].ileadTable();
table.barButtonAddExt([
{
{
name:"新增",
class:"btn-primary",
clickFun:this.addRow
},
]);
this.init();
},
methods: {
init:function(){
var that=this;
var parentDate = ileadLayer.getParentDataById("testAddModal").rowData;
//初始化值
this.model.contInfoId=parentDate.contInfoId;
this.model.contCode=parentDate.contCode;
// 校验内容:计划数量需要校验已经审核的月度需求计划同一种产品的数量总和不能大于合同产品该产品数量总和
// 获取已经审核计划的所有产品数量数据
testAPI.queryLineDate({contInfoId:parentDate.contInfoId,orgId:that.$store.state.orgId}).then(function(response){
if(response.data.rows!=undefined){
rowsList=response.data.rows;
for(var i=0;i<rowsList.length;i++){
//key:产品Id-标段,value:已有的计划总量
if(that.totalPlanCountMap.hasOwnProperty(rowsList[i].productId+"-"+rowsList[i].bidSection)){
//存在的累加
that.totalPlanCountMap[rowsList[i].productId+"-"+rowsList[i].bidSection]+=rowsList[i].planCount;
}else{
//不存在的添加
that.totalPlanCountMap[rowsList[i].productId+"-"+rowsList[i].bidSection]=rowsList[i].planCount;
}
}
}else{
that.$Modal.warning({
title: '提醒',
content: "初始化异常"
});
}
});
//获取合同中所有标段、产品的数量数据
testAPI.prodQueryData({contInfoId:parentDate.contInfoId}).then(function(response){
if(response.data.rows!=undefined){
rowsList=response.data.rows;
for(var i=0;i<rowsList.length;i++){
//key:产品Id-标段,value:已有的计划总量
if(that.totalContInfoMap.hasOwnProperty(rowsList[i].productId+"-"+rowsList[i].bidSection)){
//存在的累加
that.totalContInfoMap[rowsList[i].productId+"-"+rowsList[i].bidSection]+=Number(rowsList[i].contSignCount);
}else{
//不存在的添加
that.totalContInfoMap[rowsList[i].productId+"-"+rowsList[i].bidSection]=Number(rowsList[i].contSignCount);
}
}
}else{
that.$Modal.warning({
title: '提醒',
content: "初始化异常"
});
}
});
},
//选择工区
doSelectWorkArea:function(rowId){
var that = this;
ileadLayer.openModal({
id:"selectWorkAreaModel",
type:2,
title:'选择工区',
skin: 'layui-layer-lan',
area: ['full', 'full'],
content: testAPI.selectWorkAreaURL,
icon: 2,
data: {rowId:rowId,contInfoId:that.model.contInfoId,status:'1002'}
},that);
},
//选择工区回填信息
setSelectWorkArea:function(selectChildData,parentData){
var that = this;
//新增行,非重复
var table = that.$refs["testAddTable"].ileadTable();
table.editRowAction(parentData.rowId);
table.setEditCell(parentData.rowId,"areaName",selectChildData.areaName);
table.setCell(parentData.rowId,"areaId",selectChildData.areaId);
table.setCell(parentData.rowId,"bidSection",selectChildData.bidSection);
that.workAreaSelectFlag=true;
//清空产品信息
table.setEditCell(parentData.rowId,"productName",null);
table.setCell(parentData.rowId,"productId",null);
},
//选择产品
doSelectProduct:function(rowId){
var that = this;
if(!that.workAreaSelectFlag){
that.$Message.info("先选工区");
return;
}
var rowData = that.$refs["testAddTable"].ileadTable().getRowData(rowId);
ileadLayer.openModal({
id:"selectProductModal",
type:2,
title:'选择产品',
skin: 'layui-layer-lan',
area: ['full', 'full'],
content: testAPI.selectProductURL,
icon: 2,
data: {rowId:rowId,contInfoId:that.model.contInfoId,bidSection:rowData.bidSection}
},this);
},
//选择产品回填信息
setSelectProduct:function(selectChildData,parentData){
var that = this;
var table = that.$refs["testAddTable"].ileadTable();
var rowData=table.getRowData(parentData.rowId);
//新增行,非重复
if( that.areaAndProdIDString.indexOf(rowData.areaId+"-"+selectChildData.productId+"-"+selectChildData.bidSection) != -1){
that.$Modal.warning({
title: '提醒',
content: "工区名称:"+rowData.areaName+",<br/>产品名称:"+selectChildData.productName
+",<br/>标段/包件:"+selectChildData.bidSection+",<br/>已存在",
});
}else{
table.editRowAction(parentData.rowId);
table.setEditCell(parentData.rowId,"productName",selectChildData.productName);
table.setCell(parentData.rowId,"prodType",selectChildData.prodType);//类型
table.setCell(parentData.rowId,"productId",selectChildData.productId);
that.areaAndProdIDString+=rowData.areaId+"-"+selectChildData.productId+"-"+selectChildData.bidSection+",";
}
},
inputQtyChange:function(){
var that=this;
var table = that.$refs['testAddTable'].ileadTable();
var rowId = that.lastsel;
var rowData = table.getRowData(that.lastsel);
if(!(/^[\+\-]?(([1-9]\d*)|\d)$/.test(rowData.planCount))){
that.$Message.info("输入数字");
table.setEditCell(that.lastsel,'planCount',null);
return;
}
},
doSubmit:function(){
that.$Modal.warning({
title: '提醒',
content: "请先保存",
});
},
doSave:function(){
var that = this;
var table = that.$refs['testAddTable'].ileadTable();
that.$refs["model"].validate(function(valid){
if(valid) {
var flg=that.beforeSave();
if(!flg){return;}
var parentMethod = ileadLayer.getParentById("testAddModal");
var parentDate = ileadLayer.getParentDataById("testAddModal");
testAPI.saveAPI(that.model).then(function (response) {
if(response.data.code=="01"){
that.$Message.success('保存成功');
parentDate.listParentMethod.doQuery();
ileadLayer.closeModalById("testAddModal");
ileadLayer.closeModalById("selectContractModel");
} else {
that.$Modal.warning({
title: '提醒',
content: response.data.message,
});
that.loading = false;
}
}).catch(function (error) {
console.log(error);
that.loading = false;
});
}
});
},
//保存前校验
beforeSave:function(){
var that=this;
var table = that.$refs['testAddTable'].ileadTable();
if(that.model.startPlanTime>that.model.endPlanTime){
that.$Modal.warning({
title: '提醒',
content: "计划开始时间大于结束时间",
});
return;
}
if(cspUtils.formatSingleDate(that.model.startPlanTime)<that.model.contStartdate
||cspUtils.formatSingleDate(that.model.endPlanTime)>that.model.contEnddate){
that.$Modal.warning({
title: '提醒',
content: "超出合同有效期<br/>合同有效期:"
+cspUtils.formatSingleDate(that.model.contStartdate)
+" - "+
cspUtils.formatSingleDate(that.model.contEnddate),
});
return;
}
if (!that.isEmpty(that.lastsel)) {
table.saveRow(that.lastsel);
}
var rowData = table.getRowData();
var msg="";
if(rowData.length==0){
that.$Modal.warning({
title: '提醒',
content: "明细为空",
});
return;
}
var tempTotalMap=that.totalPlanCountMap;
for(var i=0;i<rowData.length;i++){
msg=that.checkRowData(rowData[i]);
if(!(/^[\+\-]?(([1-9]\d*)|\d)$/.test(rowData[i].planCount))){
that.$Modal.warning({
title: '提醒',
content: "计划数量需输入数字",
});
return;
}
if(msg!=""){
that.$Modal.warning({
title: '提醒',
content: msg,
});
return;
}
}
//汇总本次新增的数量到temp
for(var i=0;i<rowData.length;i++){
//key:产品Id-标段,value:已有的计划总量
if(that.totalPlanCountMap.hasOwnProperty(rowData[i].productId+"-"+rowData[i].bidSection)){
//存在的累加
tempTotalMap[rowData[i].productId+"-"+rowData[i].bidSection]+=Number(rowData[i].planCount);
}else{
//不存在的添加
tempTotalMap[rowData[i].productId+"-"+rowData[i].bidSection]=Number(rowData[i].planCount);
}
}
//对比temp与contInfo的数量,temp不能超过contInfo中的数量
for(var i=0;i<rowData.length;i++){
if(that.totalContInfoMap.hasOwnProperty(rowData[i].productId+"-"+rowData[i].bidSection)){
//存在的验证数量
if(tempTotalMap[rowData[i].productId+"-"+rowData[i].bidSection]>that.totalContInfoMap[rowData[i].productId+"-"+rowData[i].bidSection]){
var tempMsg="组合计划数量超出合同约定:</br>标段:"+rowData[i].bidSection+","+"产品:"+rowData[i].productName+",本次可录入:";
if(msg.indexOf(tempMsg)==-1){
msg+=tempMsg;
}
}
}else{
msg+="组合不在合同中:</br>标段:"+rowData[i].bidSection+","+"产品:"+rowData[i].productName+";</br>";
}
}
if(msg!=""){
//补偿数量
for(var i=0;i<rowData.length;i++){
//补偿----减少汇总的数量到temp
//key:产品Id-标段,value:已有的计划总量
if(that.totalPlanCountMap.hasOwnProperty(rowData[i].productId+"-"+rowData[i].bidSection)){
//累减
tempTotalMap[rowData[i].productId+"-"+rowData[i].bidSection]-=Number(rowData[i].planCount);
}
}
//提示信息重绘
for(var i=0;i<rowData.length;i++){
var oldMsg="标段:"+rowData[i].bidSection+","+"产品:"+rowData[i].productName+",本次可录入:";
var tempFlagMsg="标段:"+rowData[i].bidSection+","+"产品:"+rowData[i].productName+",本次可录入总量:";
var newMsg="标段:"+rowData[i].bidSection+","+"产品:"+rowData[i].productName+",本次可录入总量:"
+(that.totalContInfoMap[rowData[i].productId+"-"+rowData[i].bidSection]-tempTotalMap[rowData[i].productId+"-"+rowData[i].bidSection])
+";</br>";
if(msg.indexOf(oldMsg)!=-1&&msg.indexOf(tempFlagMsg)==-1){
msg=msg.replace(oldMsg,newMsg);
}
}
that.$Modal.warning({
title: '提醒',
content: msg,
});
return;
}
that.model.insertLineList=rowData;
return "success";
},
doReset:function(){
ileadUtils.reset(this, this.model, "model");
},
//新增行
addRow:function(){
var that = this;
var lineTable = that.$refs["testAddTable"].ileadTable();
if (!this.isEmpty(that.lastsel)) {
var lastRowData=lineTable.getRowData(that.lastsel);
var msg=that.checkRowData(lastRowData[0]);
if(msg!=""){
that.$Modal.warning({
title: '提醒',
content: msg,
});
return;
}
lineTable.saveRow(that.lastsel);
}
var newRowData='';
newRowData = {
"status":that.model.status,
}
var table = that.$refs["testAddTable"].ileadTable();
var option = {};
option.rowData = newRowData;
var marked = table.addEditRowAction(option);
that.lastsel = marked;
that.workAreaSelectFlag=false;
},
checkRowData:function(rowData){
if(rowData.areaId==null||rowData.areaId==""){
return "工区名称为空";
}
if(rowData.productId==null||rowData.productId==""){
return "产品名称为空";
}
if(rowData.planCount==null||rowData.planCount==""){
return "计划数量为空";
}
return "";
},
//双击编辑
doubleClick:function(rowId, cellIndex, cell, e){
var that = this;
var table=that.$refs["testAddTable"].ileadTable();
if(that.lastsel == ""||(rowId && rowId !== that.lastsel)){
table.saveRow(that.lastsel);
that.lastsel=rowId;
table.editRowAction(rowId);
}else{
table.saveRow(that.lastsel);
that.lastsel='';
}
},
//编辑
editRow:function(){
var that = this;
var table=that.$refs["testAddTable"].ileadTable();
var rowId = table.getGridParam("selrow");
if (this.isEmpty(rowId)) {
this.$Message.info("请选择明细");
} else {
if(that.lastsel == ""||(rowId && rowId !== that.lastsel)){
table.saveRow(that.lastsel);
that.lastsel=rowId;
table.editRowAction(rowId);
}else{
table.saveRow(that.lastsel);
that.lastsel='';
}
}
},
//删除行
delRow:function(){
var that = this;
var lineTable=that.$refs["testAddTable"].ileadTable();
var selrow = lineTable.getGridParam("selrow");
var rowData=lineTable.getRowData(selrow);
if (this.isEmpty(selrow)) {
this.$Message.info("请选择明细");
} else {
var ids = [];
ids.push(selrow);
var flg=lineTable.delEditRowActionByIdsNoconfirm(ids);
if(flg){
var msg=rowData.areaId+"-"+rowData.productId+"-"+rowData.bidSection;
if(that.areaAndProdIDString.indexOf(msg) != -1){
that.areaAndProdIDString=that.areaAndProdIDString.replace(','+msg+',',',');
}
}
}
},
//关闭
doClose: function(){
ileadLayer.closeModalById("testAddModal");
},
//时间格式
formatDate: function(cellValue,options,rowObject){
return null==cellValue? "" : ileadUtils.formatDate(new Date(parseInt(cellValue)));
},
// 判空方法
isEmpty:function(value){
if (value == null || value == undefined || value == '') {
return true;
}
return false;
},
gridComplete:function(){
var height = parseFloat($("#testAdd").css("height")) - 320;
$("#gbox_testAddTable").find(".ui-jqgrid-bdiv").eq(0).css("height",height+"px");
},
}
});
new testAdd({
el: '#testAdd',
store : store.store
});
});