Vue3 el-table里input设置为必填

Vue3 el-table里input设置为必填

Vue3 el-table里input设置为必填

页面效果

实现代码

bash 复制代码
<template>
  <el-form :model="tableData" ref="formRef">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="product_name" label="产品名称" width="400"></el-table-column>
      <el-table-column prop="sku" label="SKU" min-width="120"></el-table-column>
      <el-table-column prop="price" label="价格" width=""></el-table-column>
      <el-table-column prop="quantity" label="数量" width=""></el-table-column>
      <el-table-column label="确认数量">
        <template #default="{ row, $index }">
          <el-form-item :prop="'[' + $index + '].accepted_quantity'" :rules="rules.acceptNumber">
            <el-input v-model="row.accepted_quantity"></el-input>
          </el-form-item>
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" @click="submitConfirm">确认订单</el-button>
  </el-form>
</template>
 
<script setup>
import { reactive, ref, unref, toRefs } from 'vue';
import { ElMessage } from 'element-plus';

export default defineComponent({
  name: 'componentName',
  setup(prop, {emit}) {
    const formRef = ref<HTMLElement | null>(null);
    const state = reactive({
      tableData: [
		{
			"accepted_quantity": 0,
			"event": "",
			"id": 39,
			"order_id": 40,
			"price": 200,
			"product_id": 5,
			"product_name": "product name of 3005BK",
			"quantity": 1,
			"ship_time": null,
			"sku": "3005BK",
			"tracking_number": ""
		}
	  ],
	  rules: {
        acceptNumber: [
          {required: true, message: "确认数量不能为空", trigger: "blur"}
        ],
      }
    });

	const submitConfirm = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          ElMessage.error('验证成功');
        } else {
          ElMessage.error('确认数量不能为空');
          return false;
        }
      });
    };

    return {
      formRef,
	  submitConfirm,
	  ...toRefs(state),
    };
  },
});
</script>
相关推荐
鱼与宇2 小时前
前端Web(html+css+js+vue3)
前端
孙克旭_3 小时前
Docker 镜像构建|Vue+SpringBoot+Go 多语言项目 Dockerfile 案例
linux·运维·vue.js·spring boot·docker·dockerfile
雪芽蓝域zzs4 小时前
(六)打包优化 + Nginx 部署完整配置 + 项目收尾
前端·vue.js
研☆香4 小时前
聊一聊前端的常见字 关键字
开发语言·前端·javascript
东风破_4 小时前
JWT 1:从一个登录请求开始,理解 React 项目里的 API 层与 Mock
前端·后端
东风破_4 小时前
JWT 3:为什么 Token 要放进 Authorization?Axios 拦截器到底解决了什么?
前端·后端
东风破_4 小时前
JWT 5:路由守卫是什么?把整个 JWT 登录鉴权流程串起来
前端·后端
东风破_4 小时前
JWT 2:HTTP 是无状态的,为什么登录成功后还要给 Token?
前端·后端
东风破_5 小时前
JWT 4:Zustand 到底解决了什么?为什么登录状态要放进 Store?
前端·后端
IT_陈寒5 小时前
Vite动态导入差点让我秃头,原来问题出在这
前端·人工智能·后端