首页 > 其他分享 >el-form中关于添加el-table后动态添加el-input后怎么设置校验

el-form中关于添加el-table后动态添加el-input后怎么设置校验

时间:2024-10-31 14:18:54浏览次数:3  
标签:el form trigger required item 添加 为空 true

个人笔记,欢迎指正

场景复现
如何实现动态表单满足rules规则
在这里插入图片描述
在这里插入图片描述
实现代码

<el-form ref="form" :model="form" :rules="rules" label-width="80px">
   <el-col :span="24">
     <el-form-item label="客户名称" prop="customerId">
       <el-select
         v-model="form.customerId"
         placeholder="请选择客户名称"
         style="width: 100%"
       >
         <el-option
           v-for="customer in customerList"
           :key="customer.customerId"
           :label="customer.customerName"
           :value="customer.customerId"
         />
       </el-select>
     </el-form-item>
   </el-col>
   <el-col :span="12">
     <el-form-item label="商机名称" prop="orderId">
       <el-select
         v-model="form.orderId"
         placeholder="请选择商机名称"
         style="width: 100%"
       >
         <el-option
           v-for="order in orderList"
           :key="order.orderId"
           :label="order.orderName"
           :value="order.orderId"
         />
       </el-select>
     </el-form-item>
   </el-col>
   <el-col :span="12">
     <el-form-item label="申请日期" prop="applyTime">
       <el-date-picker
         style="width: 100%"
         clearable
         v-model="form.applyTime"
         type="date"
         value-format="yyyy-MM-dd"
         placeholder="请选择申请日期"
       />
     </el-form-item>
   </el-col>
   
   <!-- 动态表单处 -->
   <el-col :span="24">
     <el-form-item label="费用明细" prop="details" class="details">
       <el-table :data="detailList" style="width: 100%">
         <el-table-column label="报销项目" prop="project" width="280">
           <template slot-scope="scope">
             <el-input
               v-model="scope.row.detailsName"
               placeholder="请输入报销项目"
             />
           </template>
         </el-table-column>
         <el-table-column label="摘要" prop="detailsSummary" width="280">
           <template slot-scope="scope">
             <el-input
               v-model="scope.row.detailsSummary"
               placeholder="请输入摘要"
             />
           </template>
         </el-table-column>
         <el-table-column label="金额" prop="detailsAmount" width="280">
           <template slot-scope="scope">
             <el-input
               v-model="scope.row.detailsAmount"
               type="number"
               placeholder="请输入金额"
             />
           </template>
         </el-table-column>
         <el-table-column
           label="操作"
           align="center"
           class-name="small-padding fixed-width"
         >
           <template slot-scope="scope">
             <el-button
               v-if="scope.$index === detailList.length - 1"
               size="mini"
               type="text"
               icon="el-icon-plus"
               @click="addExpenses"
               >新增</el-button
             >
             <el-button
               v-else
               size="mini"
               type="text"
               icon="el-icon-delete"
               @click="delExpenses(scope.$index)"
               >删除</el-button
             >
           </template>
         </el-table-column>
       </el-table>
     </el-form-item>
   </el-col>
   <!-- 动态表单处 -->

   <el-col :span="24">
     <el-form-item label="合计金额" prop="amount">
       <el-input
         v-model="form.amount"
         placeholder="请输入合计金额"
         readonly
       />
     </el-form-item>
   </el-col>
   <el-col :span="12">
     <el-form-item label="原借款" prop="loan">
       <el-input v-model="form.loan" placeholder="请输入原借款" />
     </el-form-item>
   </el-col>
   <el-col :span="12">
     <el-form-item label="应退款" prop="refund">
       <el-input v-model="form.refund" placeholder="请输入应退款" />
     </el-form-item>
   </el-col>
   <el-col :span="24">
     <el-form-item label="备注" prop="remark">
       <el-input
         v-model="form.remark"
         type="textarea"
         placeholder="请输入内容"
       />
     </el-form-item>
   </el-col>
   <el-col :span="24">
     <el-form-item>
       <el-button type="primary" @click="submitForm">确 定</el-button>
       <el-button @click="cancel">取 消</el-button>
     </el-form-item>
   </el-col>
 </el-form>
<script>
export default {
  name: "Apply",
  data() {
    return {
      detailList: [
        { detailsName: "", detailsSummary: "", detailsAmount: null },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [
          { required: true, message: "客户名称不能为空", trigger: "change" },
        ],
        orderId: [
          { required: true, message: "商机名称不能为空", trigger: "change" },
        ],
        applyTime: [
          { required: true, message: "申请时间不能为空", trigger: "change" },
        ],
        amount: [
          { required: true, message: "费用金额不能为空", trigger: "bulr" },
        ],
        details: {
          required: true,
          validator: this.validateDetails,
          trigger: 'blur',
        },
      },
    };
  },
  methods: {
    validateDetails(rule, value, callback) {
      const isValid = this.detailList.every(item => {
      	// 检查每一项必填
        return item.detailsName && item.detailsSummary && item.detailsAmount; 
      });
      if (!isValid) {
        callback(new Error('每一项都必须填写完整'));
      } else {
        callback();
      }
    },
    addExpenses() {
      this.detailList.push({
        detailsName: "",
        detailsSummary: "",
        detailsAmount: "",
      });
    },
    delExpenses(index) {
      this.detailList.splice(index, 1);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.applyId != null) {
            updateApply(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApply(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>

标签:el,form,trigger,required,item,添加,为空,true
From: https://blog.csdn.net/qq_48071978/article/details/143397002

相关文章

  • 管家婆工贸ERP BB067.销售订单修改BOM类型+BB068.销售单按模板导出Excel
    BB067.销售订单修改BOM类型最低适用版本:工贸系列23.0插件简要功能说明:销售订单任意状态下,支持通过应用按钮将焦点行的BOM类型修改为订单BOM更多细节描述见下方详细文档插件操作视频:进销存类定制插件--销售订单修改BOM类型插件详细功能文档:销售订单增加应......
  • DIFFUSIONSAT: A GENERATIVE FOUNDATION MODEL FOR SATELLITE IMAGERY(2024-ICLR)
    论文:DIFFUSIONSAT:AGENERATIVEFOUNDATIONMODELFORSATELLITEIMAGERY(2024-ICLR)习惯用飞书做笔记了,大家见谅Diffusionsat:卫星图像生成基础模型......
  • Mysql慢日志转Excel
    最近公司生产环境需要排查慢SQL,导出日志txt文件后排查混乱,查找相关资料后并没有找到方便快捷的格式化处理工具,于是自己编写了一套Java读取慢SQL日志转为Excel小工具。@DatapublicclassSlowQuery{privatedoublequeryTime;privatedoublelockTime;privateS......
  • Seinfeld on Blu-ray, 4k UHD
    nowforpre-orderonAmazonDVD: https://www.amazon.com/Seinfeld-Complete-Box-Set-Repackage/dp/B07FVFL6K5/ref=tmm_dvd_title_0?_encoding=UTF8&amp&qid=1702581322&amp&sr=1-1Blu-ray: https://www.amazon.com/dp/B0DLF65P9R4kUHD: https://www.am......
  • windows 在词库中添加自定义短语,比如输入now可以选择当前日期加时间
    本文只记录添加在词库中添加自定义短语1、右击任务栏中的中英文切换的图标,选择“设置” 2、在”词库和自学习“中找到“用户定义短语”,点击添加或编辑自定义短语 3、点击“添加” 4、需要输入拼音、候选位置和短语拼音:now侯选位置:固定的选择序号短语:%yyyy%年%MM%月......
  • Delphi10.3下SimpleGraph v2.92的安装,使用
     下载 通过百度网盘分享的文件:simple-graph-master.zip链接:https://pan.baidu.com/s/19KHlGaitim21qcgXHl_HgQ提取码:n3zq安装将C:\Users\Administrator\Downloads\simple-graph-master\simple-graph-master\Source加入到第一步:点击“File”-“New”菜单中的“Packag......
  • 记录NPOI导出Excel并使用不同颜色标注特定条件相同的行
    一、环境概述开发工具:VisualStudio2022.Net环境:.NetCore6.0相关依赖:NPOI二、基本概述需求要实现导出的数据里如果连续行满足某个条件就用颜色标记,但是标记的区间要用不同颜色区分开,用了双指针,但是是记录了起始位置和偏移量,封装比较屎,这里主要记录一下思想。三、代码实......
  • springboot 整合 activiti 搭配 vue3 element-plus activiti-modeler bpmn-js
    springboot整合activiti搭配vue3element-plusactiviti-modelerbpmn-js配合使用ElementPlus+Vue3(idea开发,需要安装lombok插件)相关依赖版本后台:依赖版本spring-boot2.7.18knife4j-spring-boot-starter3.0.3pagehelper-spring-boot-starter1.3.0......
  • k8s~为pod添加节点的资源限制
    CPU单位CPU资源以CPU核心数为单位进行度量的。在Kubernetes中,一个CPU相当于:1AWSvCPU1GCPCore1AzurevCore一个超线程(在使用超线程的裸金属Intel处理器上)请求0.5CPU的容器所保证的CPU核数是请求节点上的1个CPU的一半。你可以用后缀m表示milli。例如100mCPU、100m......
  • PbootCMS后台ueditor编辑器上传图片如何去掉自动添加的title和alt属性
    修改ueditor.all.min.js文件:打开 \core\extend\ueditor\ueditor.all.min.js 文件搜索 "imageUrlPrefix",找到以下代码:javascript g.setAttribute("title",f.title||"");g.setAttribute("alt",f.original||"");修......