在el-table-column
中使用两个input
进行数据验证时,可以通过Vue的双向数据绑定和Element UI的表单验证机制来实现。以下是一个简单的示例:
-
使用
el-form
和el-form-item
来包裹el-table
,以便使用表单验证功能。 -
在
el-table-column
中使用template
或者scoped slot
来定义两个input
。 -
使用Vue的
v-model
来创建双向数据绑定。 -
为每个
input
添加验证规则。
<template>
<el-form :model="form" :rules="rules" ref="tableForm">
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="name" label="名称">
<template slot-scope="scope">
<el-form-item :prop="'[' + scope.$index + '].name'" :rules="rules.name">
<el-input v-model="scope.row.name"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button @click="validateRow(scope.$index)">验证</el-button>
</template>
</el-table-column>
</el-table>
</el-form>
</template>
<script>
export default {
data() {
return {
tableData: [
{ name: '' } // 初始化数据
],
form: {},
rules: {
name: [
{ required: true, message: '请输入名称', trigger: 'blur' }
],
}
};
},
methods: {
validateRow(index) {
this.$refs.tableForm.validateField(`[${index}].name`, valid => {
if (valid) {
console.log('验证通过');
} else {
console.log('验证失败');
}
});
}
}
};
</script>
在这个示例中,每当你在el-input
中输入内容并移出焦点,对应的行会进行名称的验证。如果输入不符合规则,会显示错误信息。你可以为每个input
添加不同的验证规则,并在validateRow
方法中调用this.$refs.tableForm.validateField
来验证特定行的特定字段。