element tree 点击某个节点,获取该节点的所有子节点的id

时间:2025-03-28 14:37:41

html中

<el-tree :data="list" :props="defaultProps" @node-click="handleNodeClick"></el-tree>

data中注册

list: [{
     id: 1,
     label: '一级 1',
     children: [{
           id: 2,
           label: '二级 1-1',
           children: [{
                  id: 3,
                  label: '三级 1-1-1'
           }, {
                  id: 4,
                  label: '三级 1-1-2'
           }, {
                  id: 5,
                  label: '三级 1-1-3'
           }]
     }]
}, {
     id: 6,
     label: '一级 2',
     children: [{
            id: 7,
            label: '二级 2-1'
     }, {
            id: 8,
            label: '二级 2-2'
     }]
}],
defaultProps: {
     children: 'children',
     label: 'label'
},
childArr: []

methods中

handleNodeClick (data) {
    ('data: ', data)
     = []
    (data)
    ()
},
getAllChild (obj) {
    if ((obj, 'children')) {
          for (const item of ) {
               if ((item, 'children')) {
                    ()
                    (item)
               } else {
                    ()
               }
          }
          ()
    } else {
          return null
    }
}