el-form里面嵌套el-table表格,可以进行新增数据和删除并对input框进行校验

文章描述了在Vue应用中使用ElementUI的el-dialog和el-table组件创建一个可编辑表格,实现实时添加和删除行的功能,同时包含数据验证功能,适用于IT技术中的表单管理和数据录入场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
     <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-button  type="primary" @click="submitAdd" style="float: right;margin-bottom:20px">新增一行</el-button>
      <el-form ref="form" :model="form" :rules="rules">
        <el-table stripe :data="form.tableData" border>
          <el-table-column label="序号" align="center" type="index" />
          <el-table-column align="center">
            <template slot="header">
              <span><span class="colorRed">*</span>名称</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="`tableData.${scope.$index}.name`" :rules="rules.name" >
                <el-input v-model="scope.row.name" placeholder="请输入名称" class="input"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column  align="center">
            <template slot="header">
              <span><span class="colorRed">*</span>支架</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="`tableData.${scope.$index}.holderData`" :rules="rules.holderData">
                <el-input v-model="scope.row.holderData" placeholder="请输入支架" class="input"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column  align="center">
            <template slot="header">
              <span><span class="colorRed">*</span>已知容量</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="`tableData.${scope.$index}.capacityKnown`" :rules="rules.capacityKnown">
                <el-input v-model="scope.row.capacityKnown" placeholder="请输入已知容量" class="input"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column  align="center">
            <template slot="header">
              <span><span class="colorRed">*</span>容量</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="`tableData.${scope.$index}.capacityData`" :rules="rules.capacityData">
                <el-input v-model="scope.row.capacityData" placeholder="请输入容量" class="input"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column  align="center">
            <template slot="header">
              <span><span class="colorRed">*</span>长距离</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="`tableData.${scope.$index}.isLong`" :rules="rules.isLong">
                <el-input v-model="scope.row.isLong" placeholder="请输入长距离" class="input"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column  align="center">
            <template slot="header">
              <span><span class="colorRed">*</span>压力损失数据</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="`tableData.${scope.$index}.pressureData`" :rules="rules.pressureData">
                <el-input v-model="scope.row.pressureData" placeholder="请输入压力损失数据" class="input"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="130px" >
            <template slot-scope="scope">
              <el-button type="text" @click="tableDataDelete(scope.$index)">移除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="form.tableData.length >= 1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
</template>
export default{
   data(){
      return{
         form: {
           tableData: [],
          // 表单校验
         rules: {
            name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
            holderData: [{ required: true, message: '请输入支架', trigger: 'blur' }],
            capacityKnown: [{ required: true, message: '请输入已知容量', trigger: 'blur' }],
            capacityData: [{ required: true, message: '请输入容量', trigger: 'blur' }],
            isLong: [{ required: true, message: '请输入长距离', trigger: 'blur' }],
            pressureData: [{ required: true, message: '请输入压力损失数据', trigger: 'blur'}],
        }
      },
    }
  },
  methods:{
    //添加
    submitAdd() {
      this.form.tableData.push({
        name: '',
        holderData: '',
        capacityKnown: '',
        capacityData: '',
        isLong: '',
        pressureData: ''
      })
      console.log(this.form.tableData)
    },
    //删除
    tableDataDelete(index) {
      this.form.tableData.splice(index, 1)
    }
  }
}

效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值