直接上代码:
//基于element ui 树形表格校验
<el-table-column
#default="{ row }"
prop="paramName"
label="中文名称"
width="200"
show-overflow-tooltip
>
<template v-if="isFirst(row) && !row?.ref"><span></span></template>
<template v-else>
<el-form-item
v-if="row.editable && isObjArrRefer(row)"
:prop="'rows.' + findProp(grid.rows, row.id) + '.paramName'"
:rules="isResultBody ? rules.paramName : null"
>
<el-input
v-model="row.paramName"
placeholder="请输入"
></el-input>
</el-form-item>
<template v-else>{{ row.paramName }}</template>
</template>
</el-table-column>
findProp(treeData, targId, path = '') {
const len = treeData.length
for(let i = 0; i < len; i++) {
const node = treeData[i]
if (node.id === targId) {
return path + i
}
if (node.children && node.children.length > 0) {
const childPath = `${path}${i}.children.`
const result = this.findProp(node.children, targId, childPath)
if (result) {
return result
}
}
}
},
树形表格索引
// 生成树形数组索引
indexList() {
let indexs = []
function traverse(node, index, parentIndex) {
const currentIndex = parentIndex
? `${parentIndex}.${index + 1}`
: `${index + 1}`
indexs.push(currentIndex)
if (node.children) {
for (let i = 0; i < node.children.length; i++) {
traverse(node.children[i], i, currentIndex)
}
}
}
for (let i = 0; i < this.grid.rows.length; i++) {
traverse(this.grid.rows[i], i)
}
return indexs
}