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>
相关推荐
前端小咸鱼一条24 分钟前
React组件化的封装
前端·javascript·react.js
随便起的名字也被占用32 分钟前
leaflet中绘制轨迹线的大量轨迹点,解决大量 marker 绑定 tooltip 同时显示导致的性能问题
前端·javascript·vue.js·leaflet
南方kenny38 分钟前
TypeScript + React:让前端开发更可靠的黄金组合
前端·react.js·typescript
Cache技术分享41 分钟前
149. Java Lambda 表达式 - Lambda 表达式的序列化
前端·后端
LaoZhangAI1 小时前
GPT-5推理能力全解析:o3架构、链式思考与2025年8月发布
前端·后端
JuneXcy1 小时前
11.Layout-Pinia优化重复请求
前端·javascript·css
子洋1 小时前
快速目录跳转工具 zoxide 使用指南
前端·后端·shell
天下无贼!1 小时前
【自制组件库】从零到一实现属于自己的 Vue3 组件库!!!
前端·javascript·vue.js·ui·架构·scss
CF14年老兵2 小时前
✅ Next.js 渲染速查表
前端·react.js·next.js
司宸2 小时前
学习笔记八 —— 虚拟DOM diff算法 fiber原理
前端