vue+elementui v-for循环表格 添加表单校验 (包含表格树形数据)

68 阅读1分钟

前言,原数据是二维数组,含树形数据效果

效果:

只有一级的情况:

4.png 原数据:

3.png

代码:

1.png

2.png

二维数组中有树形结构的情况:

数据: image.png 效果图: image.png image.png 代码:

:prop="`caseList.${ItemIndex}.${findTableIndex(item,scope.row.unique)}.caseFileName`" :rules="rules.caseFileName"
 // 校验表单的方法
    findTableIndex(tree, unique, path = '') {
      for (let i = 0; i < tree.length; i++) {
        const node = tree[i]
        if (node.unique === unique) {
          return path + i
        }
        if (node.children && node.children.length > 0) {
          const childPath = `${path}${i}.children.`
          const result = this.findTableIndex(node.children, unique, childPath)
          if (result !== null) {
            return result
          }
        }
      }
      return null
    },