需求:将后台传递的集合转为组件需要的树形结构
结果如下:
具体转化:
<el-form-item label="xxxxx" prop="xx">
<el-tree-select
node-key="label"
v-model="from.xxId"
:data="treedData"
:props="defaultProps"
:render-after-expand="false"
/>
</el-form-item>
const treedData = ref([])
const defaultProps = {
children: 'children',
label: 'label'
}
async function getXxxInfo() {
const data = await xxApi.geXxxInfo(params)
console.log(data.list)
let obj = {}
data.list.forEach((item) => {
if (obj.hasOwnProperty(item['xx'])) {
obj[item['xx']].push({ ...item, label: item.ebankId })
} else {
obj[item['xx']] = []
obj[item['xx']].push({ ...item, label: item.ebankId })
}
})
for (let x in obj) {
treedData.value.push({ label: x, children: obj[x] })
}
}
getXxxInfo()
记录一下,大家有其他更好的办法也可以贴出来最近太忙了,抽空记录下,需要的可以参考下