vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > vue3 elementPlus table输入框校验

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

作者:再希

这篇文章主要介绍了vue3+elementPlus table里添加输入框并提交校验,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

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

<template>
  <div style="display: flex; align-items: center">
    <h3 style="margin-right: 20px">成员信息</h3>
  </div>
  <el-form :model="info" ref="forms">
    <el-table
      :data="info.membersList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="55" />
      <el-table-column label="成员号码" prop="userNumber">
        <template #default="row">
          <el-form-item
            :prop="'membersList.' + row.$index + '.userNumber'"
            :rules="formRules.userNumber"
          >
            <el-input
              placeholder="请输入成员号码"
              v-model="info.membersList[row.$index].userNumber"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="机顶盒侧mac" prop="age">
        <template #default="row">
          <el-form-item
            :prop="'membersList.' + row.$index + '.macCode'"
            :rules="formRules.macCode"
          >
            <el-input
              placeholder="请输入机顶盒侧mac"
              v-model="info.membersList[row.$index].macCode"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="机顶盒stbid" prop="snCode">
        <template #default="row">
          <el-form-item
            :prop="'membersList.' + row.$index + '.snCode'"
            :rules="formRules.snCode"
          >
            <el-input
              placeholder="请输入机顶盒stbid"
              v-model="info.membersList[row.$index].snCode"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="电视账号" prop="tvAccount">
        <template #default="row">
          <el-form-item
            :prop="'membersList.' + row.$index + '.tvAccount'"
            :rules="formRules.tvAccount"
          >
            <el-input
              placeholder="请输入电视账号"
              v-model="info.membersList[row.$index].tvAccount"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注(如房间号)" prop="remark">
        <template #default="row">
          <el-form-item
            :prop="'membersList.' + row.$index + '.remark'"
            :rules="formRules.remark"
          >
            <el-input
              placeholder="请输入备注(如房间号)"
              v-model="info.membersList[row.$index].remark"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="row">
          <el-form-item :prop="'membersList.' + row.$index + '.oprcode'">
            <el-select
              placeholder="请选择操作类型"
              style="margin-right: 10px"
              v-model="info.membersList[row.$index].oprcode"
            >
              <el-option label="添加" value="01" />
              <el-option label="删除" value="02" />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
</template>
<script setup name="index" lang="ts">
  import {
    Search,
    Plus,
    Upload,
    Minus,
    Download,
  } from '@element-plus/icons-vue'
  import type { FormInstance } from 'element-plus'
  const { proxy } = getCurrentInstance()
  let info: any = reactive({
    membersList: [],
  })
  const formRules = reactive({
    userNumber: [
      { required: true, message: '请输入成员号码', trigger: 'blur' },
    ],
    macCode: [
      { required: true, message: '请输入机顶盒侧mac', trigger: 'blur' },
    ],
    snCode: [{ required: true, message: '请输入机顶盒stbid', trigger: 'blur' }],
  })
  const forms = ref<FormInstance>()
  const save = async () => {
    if (!forms) return
    await forms.value?.validate((valid: any) => {
      if (valid) {
      }
    })
  }
</script>

到此这篇关于vue3+elementPlus table里添加输入框并提交校验的文章就介绍到这了,更多相关vue3 elementPlus table输入框校验内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文