vxe-grid table 校验指定行单元格的字段,只校验某个列的字段
Vxe UI vue vxe-table 中校验表格行是非常简单的,只需要配置好校验规则,然后调用 validate 方法就可以自动完成校验,但是由于项目淡色特殊需求,在某个单元格的值修改后需要对另一个列的值就行校验,这个时候又不需要全部进行校验,所以只需要调用 validateField 对指定行的单元格就行校验就可以了
参数传说:
row 可以传单行也可以是数组多行
field 可以传单列也可以是数组多列字段名
validateField(row, field)
<template>
<div>
<vxe-grid ref="gridRef" v-bind="gridOptions">
<template #action="{ row }">
<vxe-button mode="text" status="primary" @click="validField(row)">校验 role</vxe-button>
<vxe-button mode="text" status="primary" @click="validMultiFiled(row)">校验 name,role</vxe-button>
</template>
</vxe-grid>
</div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { VxeUI } from 'vxe-table'
const gridRef = ref()
const gridOptions = reactive({
border: true,
showOverflow: true,
keepSource: true,
height: 300,
editConfig: {
trigger: 'click',
mode: 'cell',
showStatus: true
},
editRules: {
name: [
{ required: true, message: '必须填写' }
],
role: [
{ required: true, message: '必须填写' }
]
},
columns: [
{ type: 'checkbox', width: 60 },
{ type: 'seq', width: 70 },
{ field: 'name', title: 'Name', editRender: { name: 'VxeInput' } },
{ field: 'role', title: 'Role', editRender: { name: 'VxeInput' } },
{ field: 'sex', title: 'Sex', editRender: { name: 'VxeInput' } },
{ field: 'age', title: 'Age', editRender: { name: 'VxeInput' } },
{ field: 'date', title: 'Date', editRender: { name: 'VxeInput' } },
{ title: '操作', width: 260, slots: { default: 'action' } }
],
data: [
{ id: 10001, name: 'Test1', role: '', sex: '0', age: 28, address: 'test abc' },
{ id: 10002, name: '', role: 'Test', sex: '1', age: 22, address: 'Guangzhou' },
{ id: 10003, name: '', role: '', sex: '', age: 32, address: 'Shanghai' },
{ id: 10004, name: 'Test4', role: 'Designer', sex: '', age: 23, address: 'test abc' },
{ id: 10005, name: '', role: '', sex: '1', age: 30, address: 'Shanghai' },
{ id: 10006, name: 'Test6', role: 'Designer', sex: '1', age: 21, address: 'test abc' }
]
})
const validField = async (row) => {
const $grid = gridRef.value
if ($grid) {
const errMap = await $grid.validateField(row, 'role')
if (errMap) {
VxeUI.modal.message({ status: 'error', content: '校验不通过!' })
} else {
VxeUI.modal.message({ status: 'success', content: '校验成功!' })
}
}
}
const validMultiFiled = async (row) => {
const $grid = gridRef.value
if ($grid) {
const errMap = await $grid.validateField(row, ['name', 'role'])
if (errMap) {
VxeUI.modal.message({ status: 'error', content: '校验不通过!' })
} else {
VxeUI.modal.message({ status: 'success', content: '校验成功!' })
}
}
}
</script>
https://gitee.com/xuliangzhan/vxe-table