ant-design-vue Table+Form表单实现表格内置表单验证,可自定义验证规则,触发必填项

代码示例如下:

复制代码
<!--  -->
<template>
  <a-button type="primary" style="padding-left: 10px; padding-right: 10px" @click="handleAddRow">
    <template #icon><plus-outlined /></template>新增
  </a-button>
  <a-form :model="dataSource" ref="AddModalForm" :labelCol="{ style: { width: '10px' } }">
    <a-table :dataSource="dataSource" :columns="columns" size="small" rowKey="id">
      <template #bodyCell="{ column, index, record }">
        <template v-if="column.dataIndex === 'nation'">
          <a-form-item label=" " name="nation" :rules="[{ required: true, message: '请输入名称,长度小于20字符', validator: () => customCheck(index, 'nation') }]">
            <a-input v-model:value="record.nation" placeholder="请输入国家(地区)名称" :maxlength="20" />
          </a-form-item>
        </template>
        <template v-if="column.dataIndex === 'code'">
          <a-form-item label=" " name="code" :rules="[{ required: true, message: '请输入名称,长度小于20字符', validator: () => customCheck(index, 'code'), trigger: 'blur' }]">
            <a-select v-model:value="record.code" style="width: 100%" :options="basicTypeOptions" :field-names="{ value: 'typeId', label: 'label' }"></a-select>
          </a-form-item>
        </template>
        <template v-if="column.dataIndex === 'desc'">
          <a-form-item>
            <a-input v-model:value="record.desc" placeholder="请输入描述" />
          </a-form-item>
        </template>
      </template>
    </a-table>
  </a-form>
  <a-button @click="handleOk">提交</a-button>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import { PlusOutlined } from "@ant-design/icons-vue";
import { FormInstance } from "ant-design-vue";
import { SelectProps } from "ant-design-vue";

const columns = [
  {
    title: "测试",
    dataIndex: "nation",
    width: 200,
  },
  {
    title: "测试A",
    dataIndex: "code",
    width: 200,
  },
  {
    title: "测试c",
    dataIndex: "desc",
    width: 200,
  },
];
// const useForm = Form.useForm;
const AddModalForm = ref<FormInstance>();
const basicTypeOptions = ref<SelectProps["options"]>([
  { typeId: "system_model_BasicDataType_Date", name: "日期", label: "date", nameSpace: "kld.core.basicDataType" },
  { typeId: "system_model_BasicDataType_Double", name: "小数", label: "Double", nameSpace: "kld.core.basicDataType" },
  { typeId: "system_model_BasicDataType_Text", name: "大文本", label: "largeText", nameSpace: "kld.core.basicDataType" },
  { typeId: "system_model_BasicDataType_String", name: "字符串", label: "string", nameSpace: "kld.core.basicDataType" },
  { typeId: "system_model_BasicDataType_Int", name: "整数", label: "int", nameSpace: "kld.core.basicDataType" },
]);
const visible = ref(false);
const dataSource = ref([{ nation: "", code: "", desc: "" }]);

/**
 * 新增一行空数据
 */
function handleAddRow() {
  dataSource.value.push({ nation: "", code: "", desc: "" });
}

async function handleOk() {
  try {
    await AddModalForm.value?.validateFields();
  } catch (errorInfo) {}
}
function show() {
  visible.value = true;
}
function hide() {
  visible.value = false;
}

defineExpose({ show, hide });

function customCheck(index: number, key: any) {
  if (dataSource.value[index][key]) {
    return Promise.resolve();
  }
  return Promise.reject();
}
</script>
<style lang="scss" scoped></style>

效果图:

相关推荐
清风细雨_林木木22 分钟前
Css样式中设置gap: 12px以后左右出现距离问题解析
css·vue.js·elementui
陈随易23 分钟前
薪资跳动,VSCode实时显示今日打工收入
前端·后端·程序员
七灵微27 分钟前
【前端】SPA v.s. MPA
前端
fqq335 分钟前
CSS级联样式(基础知识)备忘录
前端·css
前端小巷子35 分钟前
JS深拷贝与浅拷贝
前端·javascript·面试
用户214118326360236 分钟前
N8N教程-手把手教你搭建 N8N 自动化工作流:从安装到云部署全流程实战
前端·vue.js
Mintopia1 小时前
Three.js 环境贴图:给你的 3D 世界加个梦幻滤镜
前端·javascript·three.js
Mintopia1 小时前
JavaScript 里的光影魔术师:光线投射
前端·javascript·计算机图形学
呆呆的心2 小时前
深入探索 JavaScript 字符串处理:从基础到高阶 🚀
前端·javascript
zhangbao90s2 小时前
react-window:学习如何高效地渲染大型列表
前端·javascript·react.js