vue3+elementPlus table里添加输入框并提交校验

发布时间:2024年01月16日

<template>
  <div>
      <el-form :model="info" ref="forms">
          <el-table
          ref="tableRef"
          :data="info.data"
          border>
          <el-table-column align="center" property="name" label="*姓名">
              <template #default="row">
                  <el-form-item :prop="'data.' + row.$index + '.name'" :rules="formRules.name">
                      <el-input placeholder="请输入姓名" v-model="info.data[row.$index].name" />
                  </el-form-item>
              </template>
          </el-table-column>
          <el-table-column align="center" property="role" label="角色">
              <template #default="row">
                  <el-form-item :prop="'data.' + row.$index + '.role'" :rules="formRules.role">
                      <el-input placeholder="请输入角色" v-model="info.data[row.$index].role" />
                  </el-form-item>
              </template>
          </el-table-column>
          </el-table>
      </el-form>
    <el-button type="primary" @click="submitForm()">Submit</el-button>
  </div>
</template>
<script setup lang="ts">
import {ref, reactive} from 'vue'
import type { FormInstance } from 'element-plus'
let info:any = reactive({
  data:[
      {
      id: 0,
      name: '',
      role:''
      },{
        id: 1,
        name: '',
        role:''
      }
  ]
})
const formRules = reactive({
  name: [{ required: true, message: '请输入姓名', trigger: 'change' }],
  role: [{ required: true, message: '请输入角色', trigger: 'change' }]
})
const forms = ref<FormInstance>()
const submitForm = async () => {
  if (!forms) return
  return await forms.value?.validate((valid: any) => {
      if (valid) {
      console.log('submit!')
      } else {
      console.log('error submit!')
      return false
      }
  })
}
</script>

文章来源:https://blog.csdn.net/wz990223/article/details/135625299
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。