Vue3+ElementPlus表格table/v-for表单校验

Element Plus 如果需要对表格/列表数据进行表单校验,首先需要更改 form 绑定的 model 数据

const form = ref({
	tableData: [] // 表格数据/需要v-for遍历的数据
})

然后将 el-form-item 的 props 修改为如下(格式:表格数据 + 索引 + 需要校验的值),最后加上 rules。v-for 渲染的表单同此

<el-form-item
  :prop="'tableData.' + scope.$index + '.value'"
  :rules="formRules.value"
>
  <el-input v-model="scope.row.value"></el-input>
</el-form-item>

完整代码

<template>
  <div class="p-10">
    <div class="mb-4">
      <el-button type="primary" @click="handleAdd">Add</el-button>
      <el-button @click="handleValidate">Valid</el-button>
    </div>
    <el-form ref="formRef" :model="form">
      <el-table :data="form.tableData" border>
        <el-table-column prop="id" label="Id"></el-table-column>
        <el-table-column prop="name" label="Name"></el-table-column>
        <el-table-column label="Value">
          <template #default="scope">
            <el-form-item
              :prop="'tableData.' + scope.$index + '.value'"
              :rules="formRules.value"
            >
              <el-input v-model="scope.row.value"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { type FormInstance } from 'element-plus'

const form = ref({
  tableData: [
    {
      id: 1,
      name: '1',
      value: ''
    },
    {
      id: 2,
      name: '2',
      value: ''
    },
    {
      id: 3,
      name: '3',
      value: ''
    }
  ]
})
const formRules = ref({
  value: [
    {
      required: true,
      message: 'Please input value',
      trigger: 'blur'
    }
  ]
})

const handleAdd = () => {
  form.value.tableData.push({
    id: form.value.tableData.length + 1,
    name: '' + (form.value.tableData.length + 1),
    value: ''
  })
}

const formRef = ref<FormInstance>()
const handleValidate = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      console.log('success')
    } else {
      console.log('error')
    }
  })
}
</script>

示例:

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
根据提供的引用内容,ant-design-vue可以通过内置的Form组件和Table组件实现表格内部字段验证功能。具体步骤如下: 1. 在表格中添加需要验证的字段,例如下面的代码中的name和age字段: ```html <a-form :form="form"> <a-form-item label="Name" :rules="[{ required: true, message: 'Please input name' }]"> <a-input v-decorator="['name']" /> </a-form-item> <a-form-item label="Age" :rules="[{ required: true, message: 'Please input age' }]"> <a-input-number v-decorator="['age']" /> </a-form-item> </a-form> <a-table :columns="columns" :dataSource="dataSource" :pagination="false" /> ``` 2. 在表格中添加操作列,例如下面的代码中的操作列包含了编辑和删除按钮: ```html <a-table :columns="columns" :dataSource="dataSource" :pagination="false"> <template #action="text, record"> <a-button @click="edit(record)">Edit</a-button> <a-button @click="delete(record)">Delete</a-button> </template> </a-table> ``` 3. 在编辑操作中打开,并将当前行的数据绑定到中: ```javascript edit(record) { this.form.setFieldsValue(record); this.editingKey = record.key; } ``` 4. 在中添加保存按钮,并在点击保存按钮时进行验证和数据更新: ```html <a-form :form="form"> <a-form-item label="Name" :rules="[{ required: true, message: 'Please input name' }]"> <a-input v-decorator="['name']" /> </a-form-item> <a-form-item label="Age" :rules="[{ required: true, message: 'Please input age' }]"> <a-input-number v-decorator="['age']" /> </a-form-item> <a-form-item> <a-button type="primary" @click="save">Save</a-button> </a-form-item> </a-form> ``` ```javascript save() { this.form.validateFields((err, values) => { if (!err) { const newData = [...this.dataSource]; const index = newData.findIndex((item) => this.editingKey === item.key); if (index > -1) { const item = newData[index]; newData.splice(index, 1, { ...item, ...values }); this.dataSource = newData; this.editingKey = ''; } else { newData.push(values); this.dataSource = newData; this.editingKey = ''; } } }); } ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值