computed: { treeData() { let cloneData = JSON.parse(JSON.stringify(this.tableData)); // 对源数据深度克隆 return cloneData.filter(father => { let branchArr = cloneData.filter(child => father.id == child.pid); //返回每一项的子级数组 branchArr.length > 0 ? (father.children = branchArr) : ""; //如果存在子级,则给父级添加一个children属性,并赋值 return father.pid == 0; //返回第一层 }); } },
作者:吊儿郎当小元元