1.如何获取目录子级的id来进行传值
效果图和数据格式:
数据格式:
answers: [ { label:"顶级目录", value:"undefined", key:1 children:[ { label:'二级目录', value:'undefined' key:2, children:[ { label:'子级目录', value:'ed1eb230-6549-11e8-bf8a-2f7bf305f9b8', key:3 } ] } ] } ]
注:key值得唯一性
解决思路:递归函数=>满足条件,跳出
let xz_ml = this.state.xz_ml; let ml_arr = []; let selectKeyArr_arr = this.state && this.state.selectKeyArr;//目录key值 const ml = (data) => { data && data.map((item, index) => { console.log(item.children,'item.children'); //!item.children是最子级节点。不满足提交走else继续调用自身,直到满足条件 if (!item.children) { if (selectKeyArr_arr && (selectKeyArr_arr.indexOf(item.key) != -1)) { item.value && ml_arr.push(item.value) } } else { ml(item.children); } }) } console.log(answers,'answers'); console.log(selectKeyArr_arr,'selectKeyArr_arr'); console.log(xz_ml,'zzz'); if (xz_ml == "顶级目录") { ml(answers); }