赞
踩
<a-form ref="validationFormRef" :model="validationForm">
<template v-for="(item, index) in validationForm.list" :key="item.id">
<a-form-item :name="['list', index, 'num']" label="个数" :rules="[intRule]">
<a-input-number v-model:value="item.num" />
</a-form-item>
</template>
</a-form>
import type { FormInstance } from 'ant-design-vue'; import type { Rule } from 'ant-design-vue/es/form'; export default defineComponent({ setup() { const validationFormRef = ref<FormInstance | null>(null); const validationForm = reactive<any>({ list: [.......], // 循环的列表 }); const isInteger = (_: Rule, value: any) => { // 判断是否为正整数 const r = /^[1-9][0-9]*$/; if (value && !r.test(value.toString())) { return Promise.reject(new Error('请输入正整数!')); } return Promise.resolve(); }; const intRule: Rule = { validator: isInteger, trigger: 'change' }; const hasFormError = async () => { // 表单验证 let values; try { values = await validationFormRef.value?.validateFields(); } catch (errorInfo) { console.log(errorInfo); } return !values; }, return { validationFormRef, validationForm, intRule, } }
没有提示了,但是输入小数会自动四舍五入
<a-input-number
v-model:value="item.size"
placeholder="请输入"
type="number"
:min="1"
:default-value="1"
:formatter="formatterInt"
addon-after="MB"
class="sg-margin-l-4 sg-width-full-100"
/>
formatterInt: (value: number) => Math.round(value),
组件也可以直接格式化保留小数位数:precision="0"
,但是需要focus离开之后才格式化
<a-input-number
v-model:value="item.size"
placeholder="请输入"
type="number"
:min="1"
:default-value="1"
:precision="0"
addon-after="MB"
class="sg-margin-l-4 sg-width-full-100"
/>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。