树形结构数据
let arr = [
{
"platformId":"461417688549658625",
"platformName":"蘑菇小姐的测试平台",
"roles":[
{
"roleId":968589,
"roleName":"平台管理员"
}
]
},
{
"platformId":"419419609251475533",
"platformName":"昨日头条",
"roles":[
{
"roleId":968576,
"roleName":"丑角"
},
{
"roleId":968574,
"roleName":"平台管理员"
}
]
},
{
"platformId":"419419609251475530",
"platformName":"今日头条",
"roles":[
{
"roleId":968570,
"roleName":"大主播"
}
]
},
{
"platformId":"419419609251475529",
"platformName":"张松测试平台",
"roles":[
{
"roleId":968569,
"roleName":"平台管理员"
},
{
"roleId":968568,
"roleName":"test"
}
]
},
{
"platformId":"419419609251475527",
"platformName":"新建测试平台",
"roles":[
{
"roleId":968560,
"roleName":"平台管理员"
}
]
},
{
"platformId":"30205329925878161",
"platformName":"wxy的测试平台",
"roles":[
{
"roleId":968587,
"roleName":"运营"
},
{
"roleId":968556,
"roleName":"船长"
},
{
"roleId":968555,
"roleName":"副船长"
}
]
},
{
"platformId":"30205329925878041",
"platformName":"平台权限管理系统",
"roles":[
{
"roleId":968495,
"roleName":"平台普通用户"
},
{
"roleId":968466,
"roleName":"普通管理员"
},
{
"roleId":2,
"roleName":"平台管理员"
},
{
"roleId":1,
"roleName":"超级管理员"
}
]
},
{
"platformId":"30205329925877901",
"platformName":"DPS-数字化采购平台-供应商端",
"roles":[
{
"roleId":968565,
"roleName":"平台管理员"
}
]
},
{
"platformId":"43",
"platformName":"管理",
"roles":[
{
"roleId":968494,
"roleName":"gg"
},
{
"roleId":968493,
"roleName":"gg"
},
{
"roleId":968492,
"roleName":"普通用户"
},
{
"roleId":968491,
"roleName":"平台管理员"
}
]
},
{
"platformId":"42",
"platformName":"vip",
"roles":[
{
"roleId":968490,
"roleName":"平台管理员"
}
]
},
{
"platformId":"41",
"platformName":"test平台1",
"roles":[
{
"roleId":968548,
"roleName":"哈_哈%"
},
{
"roleId":968532,
"roleName":"平台管理员"
},
{
"roleId":968477,
"roleName":"平台管理员"
},
{
"roleId":968476,
"roleName":"role1"
}
]
}
]
在vue中的写法:
1.树形数据转成数组
<template>
<div class="hello">
<a-tree
:expanded-keys="expandedKeys"
:auto-expand-parent="autoExpandParent"
:selected-keys="selectedKeys"
:tree-data="gData"
@expand="onExpand"
@select="onSelect"
checkable
v-model="checkedKeys"
>
<template slot="title" slot-scope="{ title }">
<span v-if="title.indexOf(searchValue) > -1">
{{ title.substr(0, title.indexOf(searchValue)) }}
<span style="color: #f50">{{ searchValue }}</span>
{{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
</span>
<span v-else>{{ title }}</span>
</template>
</a-tree>
</div>
</template>
<script>
// json数据如上
import ListData from './data.json'
export default {
name: 'HelloWorld',
props: {
msg: String
},
data(){
return{
expandedKeys: [],
autoExpandParent: true,
gData: [],
selectedKeys:[],
checkedKeys: []
}
},
watch: {
checkedKeys(val) {
console.log('onCheck', val);
},
},
mounted () {
// 把树形结构的属性名称换成key,title
const arr = ListData.result
const newArr = JSON.parse(JSON.stringify(arr).replace(/roleId/g, 'key').replace(/roleName/g, 'title').replace(/roles/g, 'children').replace(/platformName/g, 'title').replace(/platformId/g, 'key'))
this.gData = newArr
// 树形转成数组
const newList = this.arrayToTree(this.gData)
console.log(newList,'树形转成数组')
},
methods: {
arrayToTree (data) {
const result = []
data.forEach(item => {
const loop = data => {
result.push({
key: data.key,
title: data.title
})
const child = data.children
if (child) {
for (let i = 0; i < child.length; i++) {
loop(child[i])
}
}
}
loop(item)
})
return result
},
onExpand (expandedKeys) {
console.log('onExpand', expandedKeys)
this.expandedKeys = expandedKeys
this.autoExpandParent = false
},
onSelect (selectedKeys, info) {
console.log('onSelect', info)
this.selectedKeys = selectedKeys
},
onCheck(checkedKeys) {
console.log('onCheck', checkedKeys);
this.checkedKeys = checkedKeys;
},
onTreeChange (e) {
// const value = e.target.value
console.log(e)
// const expandedKeys = dataList
// .map(item => {
// if (item.title.indexOf(value) > -1) {
// return getParentKey(item.key, gData)
// }
// return null
// })
// .filter((item, i, self) => item && self.indexOf(item) === i)
// Object.assign(this, {
// expandedKeys,
// searchValue: value,
// autoExpandParent: true
// })
}
}
}
</script>
2.数组转换成树形
3.递归使用,遍历查找出树形中是否包含某个特定的值(id)
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<a-input-search style="margin-bottom: 8px" placeholder="Search" @change="onTreeChange" />
<a-tree
:expanded-keys="expandedKeys"
:auto-expand-parent="autoExpandParent"
:selected-keys="selectedKeys"
:tree-data="gData"
@expand="onExpand"
@select="onSelect"
checkable
v-model="checkedKeys"
>
<template slot="title" slot-scope="{ title }">
<span v-if="title.indexOf(searchValue) > -1">
{{ title.substr(0, title.indexOf(searchValue)) }}
<span style="color: #f50">{{ searchValue }}</span>
{{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
</span>
<span v-else>{{ title }}</span>
</template>
</a-tree>
</div>
</template>
<script>
import ListData from './data.json'
export default {
name: 'HelloWorld',
props: {
msg: String
},
data(){
return{
expandedKeys: [],
searchValue: '',
autoExpandParent: true,
gData: [],
selectedKeys:[],
checkedKeys: []
}
},
watch: {
checkedKeys(val) {
console.log('onCheck', val);
val.map(item => {
// item相当于id,传的419419609251475533,0等值
// 根据id查找树形数据中的某个值
const arr = this.deepQuery(this.gData,item)
console.log(arr,'arr')
})
},
},
mounted () {
// 把树形结构的属性名称换成key,title(修改对象数组里的对象属性名)
const arr = ListData.result
const newArr = JSON.parse(JSON.stringify(arr).replace(/roleId/g, 'key').replace(/roleName/g, 'title').replace(/roles/g, 'children').replace(/platformName/g, 'title').replace(/platformId/g, 'key'))
this.gData = newArr
},
methods: {
deepQuery (tree, id) {
var isGet = false
var retNode = null
function deepSearch (tree, id) {
for (var i = 0; i < tree.length; i++) {
if (tree[i].children && tree[i].children.length > 0) {
deepSearch(tree[i].children, id)
}
if (id === tree[i].key || isGet) {
tree[i].checkabled = true
isGet || (retNode = tree[i])
isGet = true
break
}
}
}
deepSearch(tree, id)
return retNode
},
onExpand (expandedKeys) {
console.log('onExpand', expandedKeys)
this.expandedKeys = expandedKeys
this.autoExpandParent = false
},
onSelect (selectedKeys, info) {
console.log('onSelect', info)
this.selectedKeys = selectedKeys
},
onCheck(checkedKeys) {
console.log('onCheck', checkedKeys);
this.checkedKeys = checkedKeys;
},
onTreeChange (e) {
// const value = e.target.value
console.log(e)
// const expandedKeys = dataList
// .map(item => {
// if (item.title.indexOf(value) > -1) {
// return getParentKey(item.key, gData)
// }
// return null
// })
// .filter((item, i, self) => item && self.indexOf(item) === i)
// Object.assign(this, {
// expandedKeys,
// searchValue: value,
// autoExpandParent: true
// })
}
}
}
</script>
4.递归获取树数据对应的值(获取选中的id的对应的全部拿出来,点击时只能获取到key值,要把key值对应的都拿出来放到数组里)
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<a-input-search style="margin-bottom: 8px" placeholder="Search" @change="onTreeChange" />
<a-tree
:expanded-keys="expandedKeys"
:auto-expand-parent="autoExpandParent"
:selected-keys="selectedKeys"
:tree-data="gData"
@expand="onExpand"
@select="onSelect"
checkable
v-model="checkedKeys"
>
<template slot="title" slot-scope="{ title }">
<span v-if="title.indexOf(searchValue) > -1">
{{ title.substr(0, title.indexOf(searchValue)) }}
<span style="color: #f50">{{ searchValue }}</span>
{{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
</span>
<span v-else>{{ title }}</span>
</template>
</a-tree>
</div>
</template>
<script>
import ListData from './data.json'
export default {
name: 'HelloWorld',
props: {
msg: String
},
data(){
return{
expandedKeys: [],
searchValue: '',
autoExpandParent: true,
gData: [],
selectedKeys:[],
checkedKeys: []
}
},
watch: {
checkedKeys(val) {
console.log('onCheck', val);
// 递归获取树数据的对应值
const arr = this.recursionChild(this.gData, val)
console.log(arr,'递归获取树数据的对应值')
},
},
mounted () {
// 把树形结构的属性名称换成key,title(修改对象数组里的对象属性名)
const arr = ListData.result
const newArr = JSON.parse(JSON.stringify(arr).replace(/roleId/g, 'key').replace(/roleName/g, 'title').replace(/roles/g, 'children').replace(/platformName/g, 'title').replace(/platformId/g, 'key'))
this.gData = newArr
},
methods: {
/**
* 获取tree对应code的值
* arr 值数组
* data tree数据
*/
recursionChild (arr, data) {
const name = []
let Obj
data.forEach(res => {
Obj = arr.find(item => res === item.key)
if (Obj) {
name.push(Obj)
if (Obj.children) data = Obj.children
}
})
return name
},
onExpand (expandedKeys) {
console.log('onExpand', expandedKeys)
this.expandedKeys = expandedKeys
this.autoExpandParent = false
},
onSelect (selectedKeys, info) {
console.log('onSelect', info)
this.selectedKeys = selectedKeys
},
onCheck(checkedKeys) {
console.log('onCheck', checkedKeys);
this.checkedKeys = checkedKeys;
},
onTreeChange (e) {
// const value = e.target.value
console.log(e)
// const expandedKeys = dataList
// .map(item => {
// if (item.title.indexOf(value) > -1) {
// return getParentKey(item.key, gData)
// }
// return null
// })
// .filter((item, i, self) => item && self.indexOf(item) === i)
// Object.assign(this, {
// expandedKeys,
// searchValue: value,
// autoExpandParent: true
// })
}
}
}
</script>
5.遍历树形结构(Tree),根据id找到对应的name(递归)
6.遍历树形结构并返回所有的子节点id值
function lookForAllId(data = [], arr = []) {
for (let item of data) {
arr.push(item.id)
if (item.children && item.children.length) lookForAllId(item.children, arr)
}
return arr
}
console.log(lookForAllId(treeData));
首先肯定是需要进行循环的,我使用的是for of循环
假设tree数据是一个[{},{}]这样类型的
那么直接将id值push进去就可以了
如果{}中有children这个字段的话
我们需要判断是否有children并且长度是否大于0
if (item.children && item.children.length)
如果有的话
我们需要自己调用自己并且需要传递参数给自己
第一个参数肯定被遍历的值;也就是 item.children
第二个参数是arr,用来需要返回的所有id值
-----------------------------------
js遍历树形结构并返回所有的子节点id值
7.递归给每一项加一个属性值
function lookForAllId(tree) {
for (let i = 0; i < tree.length; i++) {
tree[i].scopedSlots = {title: 'title'}
if(tree[i].children && tree[i].children.length) {
this.formatData(tree[i].children)
}
}
return tree;
}
console.log(lookForAllId(tree));