El formulario está en blanco de forma predeterminada, asigna un valor predeterminado cuando no hay datos y asigna un valor predeterminado al agregar uno nuevo

1. Primero defina un método para asignar un valor predeterminado, luego llámelo al inicializar y llámelo cuando agregue uno nuevo

2. Agregando y actualizando, los parámetros de entrada son los mismos, por lo que puede compartir una interfaz, un método y juzgar según si hay una identificación

3. Intente usar map en lugar de push para generar el formulario, porque push puede causar adiciones repetidas, pero map generará una nueva matriz cada vez sin considerar esta situación.

  <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 })
      // 如果没有数据就清空form,重新加载
      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?'保存失败':' 新增失败');
        }
    },

Supongo que te gusta

Origin blog.csdn.net/Ybittersweet/article/details/129704148
Recomendado
Clasificación