• JS 树形数据处理


    树形结构数据

    1. let arr = [
    2. {
    3. "platformId":"461417688549658625",
    4. "platformName":"蘑菇小姐的测试平台",
    5. "roles":[
    6. {
    7. "roleId":968589,
    8. "roleName":"平台管理员"
    9. }
    10. ]
    11. },
    12. {
    13. "platformId":"419419609251475533",
    14. "platformName":"昨日头条",
    15. "roles":[
    16. {
    17. "roleId":968576,
    18. "roleName":"丑角"
    19. },
    20. {
    21. "roleId":968574,
    22. "roleName":"平台管理员"
    23. }
    24. ]
    25. },
    26. {
    27. "platformId":"419419609251475530",
    28. "platformName":"今日头条",
    29. "roles":[
    30. {
    31. "roleId":968570,
    32. "roleName":"大主播"
    33. }
    34. ]
    35. },
    36. {
    37. "platformId":"419419609251475529",
    38. "platformName":"张松测试平台",
    39. "roles":[
    40. {
    41. "roleId":968569,
    42. "roleName":"平台管理员"
    43. },
    44. {
    45. "roleId":968568,
    46. "roleName":"test"
    47. }
    48. ]
    49. },
    50. {
    51. "platformId":"419419609251475527",
    52. "platformName":"新建测试平台",
    53. "roles":[
    54. {
    55. "roleId":968560,
    56. "roleName":"平台管理员"
    57. }
    58. ]
    59. },
    60. {
    61. "platformId":"30205329925878161",
    62. "platformName":"wxy的测试平台",
    63. "roles":[
    64. {
    65. "roleId":968587,
    66. "roleName":"运营"
    67. },
    68. {
    69. "roleId":968556,
    70. "roleName":"船长"
    71. },
    72. {
    73. "roleId":968555,
    74. "roleName":"副船长"
    75. }
    76. ]
    77. },
    78. {
    79. "platformId":"30205329925878041",
    80. "platformName":"平台权限管理系统",
    81. "roles":[
    82. {
    83. "roleId":968495,
    84. "roleName":"平台普通用户"
    85. },
    86. {
    87. "roleId":968466,
    88. "roleName":"普通管理员"
    89. },
    90. {
    91. "roleId":2,
    92. "roleName":"平台管理员"
    93. },
    94. {
    95. "roleId":1,
    96. "roleName":"超级管理员"
    97. }
    98. ]
    99. },
    100. {
    101. "platformId":"30205329925877901",
    102. "platformName":"DPS-数字化采购平台-供应商端",
    103. "roles":[
    104. {
    105. "roleId":968565,
    106. "roleName":"平台管理员"
    107. }
    108. ]
    109. },
    110. {
    111. "platformId":"43",
    112. "platformName":"管理",
    113. "roles":[
    114. {
    115. "roleId":968494,
    116. "roleName":"gg"
    117. },
    118. {
    119. "roleId":968493,
    120. "roleName":"gg"
    121. },
    122. {
    123. "roleId":968492,
    124. "roleName":"普通用户"
    125. },
    126. {
    127. "roleId":968491,
    128. "roleName":"平台管理员"
    129. }
    130. ]
    131. },
    132. {
    133. "platformId":"42",
    134. "platformName":"vip",
    135. "roles":[
    136. {
    137. "roleId":968490,
    138. "roleName":"平台管理员"
    139. }
    140. ]
    141. },
    142. {
    143. "platformId":"41",
    144. "platformName":"test平台1",
    145. "roles":[
    146. {
    147. "roleId":968548,
    148. "roleName":"哈_哈%"
    149. },
    150. {
    151. "roleId":968532,
    152. "roleName":"平台管理员"
    153. },
    154. {
    155. "roleId":968477,
    156. "roleName":"平台管理员"
    157. },
    158. {
    159. "roleId":968476,
    160. "roleName":"role1"
    161. }
    162. ]
    163. }
    164. ]

    在vue中的写法:

    1.树形数据转成数组

    1. <template>
    2. <div class="hello">
    3. <a-tree
    4. :expanded-keys="expandedKeys"
    5. :auto-expand-parent="autoExpandParent"
    6. :selected-keys="selectedKeys"
    7. :tree-data="gData"
    8. @expand="onExpand"
    9. @select="onSelect"
    10. checkable
    11. v-model="checkedKeys"
    12. >
    13. <template slot="title" slot-scope="{ title }">
    14. <span v-if="title.indexOf(searchValue) > -1">
    15. {{ title.substr(0, title.indexOf(searchValue)) }}
    16. <span style="color: #f50">{{ searchValue }}</span>
    17. {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
    18. </span>
    19. <span v-else>{{ title }}</span>
    20. </template>
    21. </a-tree>
    22. </div>
    23. </template>
    24. <script>
    25. // json数据如上
    26. import ListData from './data.json'
    27. export default {
    28. name: 'HelloWorld',
    29. props: {
    30. msg: String
    31. },
    32. data(){
    33. return{
    34. expandedKeys: [],
    35. autoExpandParent: true,
    36. gData: [],
    37. selectedKeys:[],
    38. checkedKeys: []
    39. }
    40. },
    41. watch: {
    42. checkedKeys(val) {
    43. console.log('onCheck', val);
    44. },
    45. },
    46. mounted () {
    47. // 把树形结构的属性名称换成key,title
    48. const arr = ListData.result
    49. 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'))
    50. this.gData = newArr
    51. // 树形转成数组
    52. const newList = this.arrayToTree(this.gData)
    53. console.log(newList,'树形转成数组')
    54. },
    55. methods: {
    56. arrayToTree (data) {
    57. const result = []
    58. data.forEach(item => {
    59. const loop = data => {
    60. result.push({
    61. key: data.key,
    62. title: data.title
    63. })
    64. const child = data.children
    65. if (child) {
    66. for (let i = 0; i < child.length; i++) {
    67. loop(child[i])
    68. }
    69. }
    70. }
    71. loop(item)
    72. })
    73. return result
    74. },
    75. onExpand (expandedKeys) {
    76. console.log('onExpand', expandedKeys)
    77. this.expandedKeys = expandedKeys
    78. this.autoExpandParent = false
    79. },
    80. onSelect (selectedKeys, info) {
    81. console.log('onSelect', info)
    82. this.selectedKeys = selectedKeys
    83. },
    84. onCheck(checkedKeys) {
    85. console.log('onCheck', checkedKeys);
    86. this.checkedKeys = checkedKeys;
    87. },
    88. onTreeChange (e) {
    89. // const value = e.target.value
    90. console.log(e)
    91. // const expandedKeys = dataList
    92. // .map(item => {
    93. // if (item.title.indexOf(value) > -1) {
    94. // return getParentKey(item.key, gData)
    95. // }
    96. // return null
    97. // })
    98. // .filter((item, i, self) => item && self.indexOf(item) === i)
    99. // Object.assign(this, {
    100. // expandedKeys,
    101. // searchValue: value,
    102. // autoExpandParent: true
    103. // })
    104. }
    105. }
    106. }
    107. </script>

    2.数组转换成树形

    3.递归使用,遍历查找出树形中是否包含某个特定的值(id)

    1. <template>
    2. <div class="hello">
    3. <h1>{{ msg }}</h1>
    4. <a-input-search style="margin-bottom: 8px" placeholder="Search" @change="onTreeChange" />
    5. <a-tree
    6. :expanded-keys="expandedKeys"
    7. :auto-expand-parent="autoExpandParent"
    8. :selected-keys="selectedKeys"
    9. :tree-data="gData"
    10. @expand="onExpand"
    11. @select="onSelect"
    12. checkable
    13. v-model="checkedKeys"
    14. >
    15. <template slot="title" slot-scope="{ title }">
    16. <span v-if="title.indexOf(searchValue) > -1">
    17. {{ title.substr(0, title.indexOf(searchValue)) }}
    18. <span style="color: #f50">{{ searchValue }}</span>
    19. {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
    20. </span>
    21. <span v-else>{{ title }}</span>
    22. </template>
    23. </a-tree>
    24. </div>
    25. </template>
    26. <script>
    27. import ListData from './data.json'
    28. export default {
    29. name: 'HelloWorld',
    30. props: {
    31. msg: String
    32. },
    33. data(){
    34. return{
    35. expandedKeys: [],
    36. searchValue: '',
    37. autoExpandParent: true,
    38. gData: [],
    39. selectedKeys:[],
    40. checkedKeys: []
    41. }
    42. },
    43. watch: {
    44. checkedKeys(val) {
    45. console.log('onCheck', val);
    46. val.map(item => {
    47. // item相当于id,传的4194196092514755330等值
    48. // 根据id查找树形数据中的某个值
    49. const arr = this.deepQuery(this.gData,item)
    50. console.log(arr,'arr')
    51. })
    52. },
    53. },
    54. mounted () {
    55. // 把树形结构的属性名称换成key,title(修改对象数组里的对象属性名)
    56. const arr = ListData.result
    57. 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'))
    58. this.gData = newArr
    59. },
    60. methods: {
    61. deepQuery (tree, id) {
    62. var isGet = false
    63. var retNode = null
    64. function deepSearch (tree, id) {
    65. for (var i = 0; i < tree.length; i++) {
    66. if (tree[i].children && tree[i].children.length > 0) {
    67. deepSearch(tree[i].children, id)
    68. }
    69. if (id === tree[i].key || isGet) {
    70. tree[i].checkabled = true
    71. isGet || (retNode = tree[i])
    72. isGet = true
    73. break
    74. }
    75. }
    76. }
    77. deepSearch(tree, id)
    78. return retNode
    79. },
    80. onExpand (expandedKeys) {
    81. console.log('onExpand', expandedKeys)
    82. this.expandedKeys = expandedKeys
    83. this.autoExpandParent = false
    84. },
    85. onSelect (selectedKeys, info) {
    86. console.log('onSelect', info)
    87. this.selectedKeys = selectedKeys
    88. },
    89. onCheck(checkedKeys) {
    90. console.log('onCheck', checkedKeys);
    91. this.checkedKeys = checkedKeys;
    92. },
    93. onTreeChange (e) {
    94. // const value = e.target.value
    95. console.log(e)
    96. // const expandedKeys = dataList
    97. // .map(item => {
    98. // if (item.title.indexOf(value) > -1) {
    99. // return getParentKey(item.key, gData)
    100. // }
    101. // return null
    102. // })
    103. // .filter((item, i, self) => item && self.indexOf(item) === i)
    104. // Object.assign(this, {
    105. // expandedKeys,
    106. // searchValue: value,
    107. // autoExpandParent: true
    108. // })
    109. }
    110. }
    111. }
    112. </script>

    4.递归获取树数据对应的值(获取选中的id的对应的全部拿出来,点击时只能获取到key值,要把key值对应的都拿出来放到数组里)

    1. <template>
    2. <div class="hello">
    3. <h1>{{ msg }}</h1>
    4. <a-input-search style="margin-bottom: 8px" placeholder="Search" @change="onTreeChange" />
    5. <a-tree
    6. :expanded-keys="expandedKeys"
    7. :auto-expand-parent="autoExpandParent"
    8. :selected-keys="selectedKeys"
    9. :tree-data="gData"
    10. @expand="onExpand"
    11. @select="onSelect"
    12. checkable
    13. v-model="checkedKeys"
    14. >
    15. <template slot="title" slot-scope="{ title }">
    16. <span v-if="title.indexOf(searchValue) > -1">
    17. {{ title.substr(0, title.indexOf(searchValue)) }}
    18. <span style="color: #f50">{{ searchValue }}</span>
    19. {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
    20. </span>
    21. <span v-else>{{ title }}</span>
    22. </template>
    23. </a-tree>
    24. </div>
    25. </template>
    26. <script>
    27. import ListData from './data.json'
    28. export default {
    29. name: 'HelloWorld',
    30. props: {
    31. msg: String
    32. },
    33. data(){
    34. return{
    35. expandedKeys: [],
    36. searchValue: '',
    37. autoExpandParent: true,
    38. gData: [],
    39. selectedKeys:[],
    40. checkedKeys: []
    41. }
    42. },
    43. watch: {
    44. checkedKeys(val) {
    45. console.log('onCheck', val);
    46. // 递归获取树数据的对应值
    47. const arr = this.recursionChild(this.gData, val)
    48. console.log(arr,'递归获取树数据的对应值')
    49. },
    50. },
    51. mounted () {
    52. // 把树形结构的属性名称换成key,title(修改对象数组里的对象属性名)
    53. const arr = ListData.result
    54. 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'))
    55. this.gData = newArr
    56. },
    57. methods: {
    58. /**
    59. * 获取tree对应code的值
    60. * arr 值数组
    61. * data tree数据
    62. */
    63. recursionChild (arr, data) {
    64. const name = []
    65. let Obj
    66. data.forEach(res => {
    67. Obj = arr.find(item => res === item.key)
    68. if (Obj) {
    69. name.push(Obj)
    70. if (Obj.children) data = Obj.children
    71. }
    72. })
    73. return name
    74. },
    75. onExpand (expandedKeys) {
    76. console.log('onExpand', expandedKeys)
    77. this.expandedKeys = expandedKeys
    78. this.autoExpandParent = false
    79. },
    80. onSelect (selectedKeys, info) {
    81. console.log('onSelect', info)
    82. this.selectedKeys = selectedKeys
    83. },
    84. onCheck(checkedKeys) {
    85. console.log('onCheck', checkedKeys);
    86. this.checkedKeys = checkedKeys;
    87. },
    88. onTreeChange (e) {
    89. // const value = e.target.value
    90. console.log(e)
    91. // const expandedKeys = dataList
    92. // .map(item => {
    93. // if (item.title.indexOf(value) > -1) {
    94. // return getParentKey(item.key, gData)
    95. // }
    96. // return null
    97. // })
    98. // .filter((item, i, self) => item && self.indexOf(item) === i)
    99. // Object.assign(this, {
    100. // expandedKeys,
    101. // searchValue: value,
    102. // autoExpandParent: true
    103. // })
    104. }
    105. }
    106. }
    107. </script>

     5.遍历树形结构(Tree),根据id找到对应的name(递归)

    6.遍历树形结构并返回所有的子节点id值

    1. function lookForAllId(data = [], arr = []) {
    2. for (let item of data) {
    3. arr.push(item.id)
    4. if (item.children && item.children.length) lookForAllId(item.children, arr)
    5. }
    6. return arr
    7. }
    8. console.log(lookForAllId(treeData));
    9. 首先肯定是需要进行循环的,我使用的是for of循环
    10. 假设tree数据是一个[{},{}]这样类型的
    11. 那么直接将id值push进去就可以了
    12. 如果{}中有children这个字段的话
    13. 我们需要判断是否有children并且长度是否大于0
    14. if (item.children && item.children.length)
    15. 如果有的话
    16. 我们需要自己调用自己并且需要传递参数给自己
    17. 第一个参数肯定被遍历的值;也就是 item.children
    18. 第二个参数是arr,用来需要返回的所有id值
    19. -----------------------------------
    20. js遍历树形结构并返回所有的子节点id值

    7.递归给每一项加一个属性值

    1. function lookForAllId(tree) {
    2. for (let i = 0; i < tree.length; i++) {
    3. tree[i].scopedSlots = {title: 'title'}
    4. if(tree[i].children && tree[i].children.length) {
    5. this.formatData(tree[i].children)
    6. }
    7. }
    8. return tree;
    9. }
    10. console.log(lookForAllId(tree));

  • 相关阅读:
    Unity UGUI的Image(图片)组件的介绍及使用
    Lombok详解
    SQL注入的原理分析
    华为机试 - 比较两个版本号的大小
    分布式事务的应用场景
    qt生成帮助文档过程
    VForm3的文件上传方式
    23 张图细讲使用 Devtron 简化 K8S 中应用开发
    JMeter界面和字体的调整
    mlflow详细安装部署
  • 原文地址:https://blog.csdn.net/qq_40055200/article/details/126493131