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>

效果图:

相关推荐
永乐春秋31 分钟前
WEB攻防-通用漏洞&文件上传&js验证&mime&user.ini&语言特性
前端
鸽鸽程序猿33 分钟前
【前端】CSS
前端·css
ggdpzhk35 分钟前
VUE:基于MVVN的前端js框架
前端·javascript·vue.js
小曲曲2 小时前
接口上传视频和oss直传视频到阿里云组件
javascript·阿里云·音视频
学不会•3 小时前
css数据不固定情况下,循环加不同背景颜色
前端·javascript·html
EasyNTS4 小时前
H.264/H.265播放器EasyPlayer.js视频流媒体播放器关于websocket1006的异常断连
javascript·h.265·h.264
活宝小娜5 小时前
vue不刷新浏览器更新页面的方法
前端·javascript·vue.js
程序视点5 小时前
【Vue3新工具】Pinia.js:提升开发效率,更轻量、更高效的状态管理方案!
前端·javascript·vue.js·typescript·vue·ecmascript
coldriversnow5 小时前
在Vue中,vue document.onkeydown 无效
前端·javascript·vue.js
我开心就好o5 小时前
uniapp点左上角返回键, 重复来回跳转的问题 解决方案
前端·javascript·uni-app