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>
相关推荐
一个专注写代码的程序媛3 分钟前
为什么vue的key值,不用index?
前端·javascript·vue.js
vvilkim10 分钟前
React 与 Vue:两大前端框架的深度对比
vue.js·react.js·前端框架
장숙혜14 分钟前
ElementUi的Dropdown下拉菜单的详细介绍及使用
前端·javascript·vue.js
火柴盒zhang16 分钟前
websheet之 编辑器
开发语言·前端·javascript·编辑器·spreadsheet·websheet
某公司摸鱼前端19 分钟前
uniapp 仿企微左边公司切换页
前端·uni-app·企业微信
WKK_22 分钟前
uniapp自定义封装tabbar
前端·javascript·小程序·uni-app
莫问alicia23 分钟前
react 常用钩子 hooks 总结
前端·javascript·react.js
Mintopia32 分钟前
图形学中的数学基础与 JavaScript 实践
前端·javascript·计算机图形学
Mintopia38 分钟前
Three.js 制作飘摇的草:从基础到进阶的全流程教学
前端·javascript·three.js
BillKu39 分钟前
Vue3父子组件数据双向同步实现方法
前端·javascript·vue.js