<el-form :model="tableDataInput" ref="ruleFormRef">
<el-table ref="multipleTableRef" v-loading="loading" :data="tableDataInput.tableData" :border="true"
:header-cell-style="{ 'background-color': '#f2f2f2', color: '#737373' }" max-height="calc(100vh - 330px)"
:span-method="objectOneMethod">
<template v-for="item in column">
<el-table-column :property="item.property" :label="item.label" :width="item.width ?? ''"
:align="item.align ?? 'left'" :show-overflow-tooltip="true" :resizable="false">
<template #default="scope">
<!-- {{scope.row}} -->
<template v-if="showType=='add'&&item.property=='nowGoalValue'">
<el-form-item :prop="`tableData.${scope.$index}.${[item.property]}`" :rules="formRules[item.property]">
<!-- {{formRules.nowGoalValue}} -->
<!-- {{scope.row[item.property]}} -->
<el-input size="small" v-show="item.property=='nowGoalValue'" v-model="scope.row[item.property]"
:type="item.type"></el-input>
</el-form-item>
</template>
<span v-else>{{ scope.row[item.property] }}</span>
</template>
</el-table-column>
</template>
</el-table>
</el-form>
要点就是在循环时动态绑定prop和校验规则:
:prop="`tableData.${scope.$index}.${[item.property]}`"
:rules="formRules[item.property]"
const formRules = reactive({
nowGoalValue: [fv.isRequired({ msg: '请输入指标值' }), validatorNumberOrDot()],
})
try {
console.log(ruleFormRef.value.validate())
const valid = await ruleFormRef.value.validate();
console.log(valid)
if (valid) {
...
}
} catch (error) {
return error;
}