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>
相关推荐
树上有只程序猿20 分钟前
终于有人把数据库讲明白了
前端
猩兵哥哥26 分钟前
前端面向对象设计原则运用 - 策略模式
前端·javascript·vue.js
司宸26 分钟前
Prompt设计实战指南:三大模板与进阶技巧
前端
RoyLin29 分钟前
TypeScript设计模式:抽象工厂模式
前端·后端·typescript
华仔啊34 分钟前
Vue3+CSS 实现的 3D 卡片动画,让你的网页瞬间高大上
前端·css
江城开朗的豌豆42 分钟前
解密React虚拟DOM:我的高效渲染秘诀 🚀
前端·javascript·react.js
vivo互联网技术1 小时前
拥抱新一代 Web 3D 引擎,Three.js 项目快速升级 Galacean 指南
前端·three.js
江城开朗的豌豆1 小时前
React应用优化指南:让我的项目性能“起飞”✨
前端·javascript·react.js
会飞的青蛙1 小时前
GIT 配置别名&脚本自动化执行
前端·git