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

<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)
    }
  }
}

效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 el-form 嵌套表格并添加校验,可以按照以下步骤进行: 1. 在 el-form嵌套 el-table 组件,并设置表格的 columns 和 data 属性。 2. 在需要校验表格中,使用 scoped slot 的方式自定义表格单元格内容,并在其中添加 el-form-item 组件。 3. 在 el-form-item 中设置需要校验表单控件,例如 el-inputel-select 等,并设置相应的校验规则。 4. 在 el-form 中使用 ref 属性获取表单的引用,并在需要提交表单时调用表单的 validate 方法进行校验。 以下是一个简单的示例代码: ```html <template> <el-form ref="form" :model="formData" :rules="formRules"> <el-table :data="tableData" style="width: 100%"> <el-table-column prop="name" label="姓名"> <template slot-scope="{ row }"> <el-form-item prop="name"> <el-input v-model="row.name"></el-input> </el-form-item> </template> </el-table-column> <el-table-column prop="age" label="年龄"> <template slot-scope="{ row }"> <el-form-item prop="age"> <el-input v-model.number="row.age"></el-input> </el-form-item> </template> </el-table-column> </el-table> <el-button type="primary" @click="submitForm">提交</el-button> </el-form> </template> <script> export default { data() { return { formData: {}, formRules: { name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }], age: [{ required: true, message: '年龄不能为空', trigger: 'blur' }] }, tableData: [ { name: '张三', age: 18 }, { name: '李四', age: 20 } ] } }, methods: { submitForm() { this.$refs.form.validate(valid => { if (valid) { // 表单校验通过,可以提交表单 } }) } } } </script> ``` 在上述示例中,我们在 el-table 的每一中使用了 scoped slot,并在其中添加了 el-form-item 组件来实现表单校验。然后,在 el-form 中设置了表单的引用,并在提交按钮的点击事件中调用了表单的 validate 方法来进行表单校验

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值