有一个列是动态的,有时候是校验人员,有时候是校验账号。怎么实现动态字段校验?

怎么校验【付款账户/人员】列?这列分别是好几个字段,要分别校验
有一个列是动态的,有时候是校验人员,有时候是校验账号。怎么实现动态字段校验?

怎么校验【付款账户/人员】列?这列分别是好几个字段,要分别校验
上午好☀️☀️☀️️
本答案参考通义千问
在使用 Element Plus 的 el-table 时,如果表格的列是动态变化的(比如某一列可能是“校验人员”,也可能是“校验账号”),并且该列包含多个字段(如“付款账户/人员”列中包含“账户名”、“账号”、“姓名”等子字段),那么需要实现动态字段校验。
为了实现动态列的校验,我们需要:
rules 属性);el-form 和 el-table 实现整体表单校验。el-table 的 columns 动态生成;prop、label 和 rules;el-form 提供的 validate 方法进行整体校验。data() {
return {
tableData: [
{
id: 1,
// 根据列动态展示的内容
name: '张三',
account: 'ZS123456',
personName: '张三',
paymentAccount: 'P123456',
paymentPerson: '李四'
}
],
columns: [], // 动态列
rules: {
name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
personName: [{ required: true, message: '请输入人员名称', trigger: 'blur' }],
paymentAccount: [{ required: true, message: '请输入付款账户', trigger: 'blur' }],
paymentPerson: [{ required: true, message: '请输入付款人员', trigger: 'blur' }]
},
formRef: null
};
}
mounted() {
this.generateColumns();
}
methods: {
generateColumns() {
const dynamicColumn = {
label: '付款账户/人员',
prop: 'paymentAccount',
rules: this.rules.paymentAccount,
render: (h, { row }) => {
return h('div', [
h('el-input', {
props: { value: row.paymentAccount },
on: {
input: (value) => {
row.paymentAccount = value;
}
}
}),
h('el-input', {
props: { value: row.paymentPerson },
on: {
input: (value) => {
row.paymentPerson = value;
}
}
})
]);
}
};
// 可以根据条件切换列
if (this.isPaymentMode) {
this.columns = [
{ label: '姓名', prop: 'name', rules: this.rules.name },
{ label: '账号', prop: 'account', rules: this.rules.account },
dynamicColumn
];
} else {
this.columns = [
{ label: '姓名', prop: 'name', rules: this.rules.name },
{ label: '人员名称', prop: 'personName', rules: this.rules.personName }
];
}
}
}
注意: 如果你希望将“付款账户/人员”作为一个单元格内多个字段并行展示,可以通过
render函数自定义渲染内容。
el-form 和 el-table<template>
<el-form ref="formRef" :model="tableData" :rules="rules">
<el-table :data="tableData" border>
<el-table-column
v-for="(col, index) in columns"
:key="index"
:label="col.label"
:prop="col.prop"
>
<template #default="scope">
<!-- 如果有自定义渲染 -->
<div v-if="col.render">
{{ col.render(h, scope) }}
</div>
<!-- 否则默认渲染 -->
<el-input
v-model="scope.row[col.prop]"
@input="onInput(scope.row, col.prop)"
/>
</template>
</el-table-column>
</el-table>
<el-button @click="submitForm">提交</el-button>
</el-form>
</template>
methods: {
submitForm() {
this.$refs.formRef.validate((valid) => {
if (valid) {
alert('校验通过');
} else {
alert('校验失败');
return false;
}
});
}
}
prop 必须与 tableData 中的字段对应;rules 中的字段必须与 prop 匹配;rules 中分别设置;render 函数时,注意事件绑定和数据更新;| 步骤 | 内容 |
|------|------|
| 1 | 定义动态列和对应的校验规则 |
| 2 | 使用 render 自定义复杂列的渲染 |
| 3 | 将 el-table 与 el-form 结合使用 |
| 4 | 调用 formRef.validate() 进行整体校验 |
<template>
<el-form ref="formRef" :model="tableData" :rules="rules">
<el-table :data="tableData" border>
<el-table-column
v-for="(col, index) in columns"
:key="index"
:label="col.label"
:prop="col.prop"
>
<template #default="scope">
<el-input
v-if="!col.render"
v-model="scope.row[col.prop]"
@input="onInput(scope.row, col.prop)"
/>
<div v-else>
{{ col.render(h, scope) }}
</div>
</template>
</el-table-column>
</el-table>
<el-button @click="submitForm">提交</el-button>
</el-form>
</template>
<script>
export default {
data() {
return {
tableData: [
{
id: 1,
name: '',
account: '',
personName: '',
paymentAccount: '',
paymentPerson: ''
}
],
columns: [],
rules: {
name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
personName: [{ required: true, message: '请输入人员名称', trigger: 'blur' }],
paymentAccount: [{ required: true, message: '请输入付款账户', trigger: 'blur' }],
paymentPerson: [{ required: true, message: '请输入付款人员', trigger: 'blur' }]
},
isPaymentMode: true,
formRef: null
};
},
mounted() {
this.generateColumns();
},
methods: {
generateColumns() {
const dynamicColumn = {
label: '付款账户/人员',
prop: 'paymentAccount',
rules: this.rules.paymentAccount,
render: (h, { row }) => {
return h('div', [
h('el-input', {
props: { value: row.paymentAccount },
on: {
input: (value) => {
row.paymentAccount = value;
}
}
}),
h('el-input', {
props: { value: row.paymentPerson },
on: {
input: (value) => {
row.paymentPerson = value;
}
}
})
]);
}
};
if (this.isPaymentMode) {
this.columns = [
{ label: '姓名', prop: 'name', rules: this.rules.name },
{ label: '账号', prop: 'account', rules: this.rules.account },
dynamicColumn
];
} else {
this.columns = [
{ label: '姓名', prop: 'name', rules: this.rules.name },
{ label: '人员名称', prop: 'personName', rules: this.rules.personName }
];
}
},
submitForm() {
this.$refs.formRef.validate((valid) => {
if (valid) {
alert('校验通过');
} else {
alert('校验失败');
return false;
}
});
}
}
};
</script>
如果你还有更复杂的场景(如多层级嵌套字段、动态列数量等),也可以继续扩展这个方案。欢迎继续提问!