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>
相关推荐
来一颗砂糖橘23 分钟前
pnpm:现代前端开发的高效包管理器
前端·pnpm
前端摸鱼匠24 分钟前
Vue 3 的defineProps编译器宏:详解<script setup>中defineProps的使用
前端·javascript·vue.js·前端框架·ecmascript
木斯佳25 分钟前
前端八股文面经大全: 美团财务科技前端一面 (2026-04-09)·面经深度解析
前端·实习面经·前端初级
天外天-亮25 分钟前
Vue2.0 + jsmind:开发思维导图
javascript·vue.js·jsmind
LIO28 分钟前
React 零基础入门,一篇搞懂核心用法(适合新手)
前端·react.js
TeamDev42 分钟前
JxBrowser 8.18.2 版本发布啦!
java·前端·跨平台·桌面应用·web ui·jxbrowser·浏览器控件
netkiller-BG7NYT43 分钟前
yoloutils - Openclaw Agent Skill
前端·webpack·node.js
北城笑笑1 小时前
FPGA 51,基于 ZYNQ 7Z010 的 FPGA 高速路由转发加速系统架构设计(Xilinx ZYNQ-MINI 7Z010 CLG400 -1)
前端·fpga开发·系统架构·fpga
蜡台1 小时前
JavaScript async和awiat 使用
开发语言·前端·javascript·async·await
tzy2331 小时前
AI 对话的流式输出详解——不止于SSE
javascript·ai·llm·sse·readablestream