一.首先定义一个赋默认值的方法,然后初始化时调用,新增时调用
二.新增与更新,入参一致,所以可以共用一个接口,一个方法,根据是否有id判断
三.生成表单尽量用map不用push,因为push可能会出现重复添加的情况,但map每次会生成新的数组不用考虑这种情况
<el-button type="primary" @click="switcSave()">保存</el-button>
<el-button type="primary" @click="doPdf">生成报表</el-button>
<el-button type="primary" @click="switcSave('add')">新增</el-button>
<el-button type="danger" :disabled="this.flag" plain @click="doDelete">删除</el-button>
data(){
flag:false
}
export function switchFormData(data) {
if(data.id){
return request({
url: "/api/TaskFormData/Update",
method: "put",
data
});
}else{
return request({
url: "/api/TaskFormData/Insert",
method: "post",
data
});
}
}
getFrom() {
this.form = {
id: "",
remark01:"",
remark02: "",
remark03:"",
remark07: "",
remark08: "",
remark09: "",
remark10: "",
remark11: "",
remark12: "",
remark13: "",
remark14: "",
remark16: moment(new Date()).format('YYYY-MM-DD'),
user_remark10: ""
}
},
async init() {
const { data } = await GetFormData({ taskId: this.conditions.remark01, formName: this.formTableName })
if (data.data.length == 0) {
this.getFrom()
this.formList = []
this.flag = true
this.formList.push(this.form)
return false
}
if (data.statusCode == 200) {
this.flag = false
data.data.forEach((el, index) => {
if (this.currentIndex == index) {
this.form = el.value
this.form.id = el.id
}
})
const values = data.data.map(item => item.value)
this.formList = values
console.log( this.form ," this.form ");
console.log("formList",this.formList);
}
},
async switcSave(val) {
if(val=='add'){
this.getFrom()
}
this.form.remark01 = this.conditions.remark01;
this.form.remark03 = this.conditions.remark03;
let isFullBoxRemark = this.isFullBox == 0 ? "空箱" : "满箱"
let formData = {
id: this.form.id,
taskId: this.taskId,
formName: this.formTableName,
formNameRemark: `${this.conditions.remark03}_${isFullBoxRemark}_` + `测量_关键参数设置表`,
key: {
isFullBox: this.isFullBox,
XYZDirection: this.conditions.remark03,
step: 'X'
},
value: this.form,
};
this.form.id?formData['updatedUserId']=this.loginUserId:formData['createdUserId']=this.loginUserId
const{data}= await switchFormData(formData)
if (data.statusCode == 200) {
this.$message.success(this.form.id?'保存成功':' 新增成功');
this.init()
} else {
this.$message.error(this.form.id?'保存失败':' 新增失败');
}
},