当前位置:   article > 正文

vue3 ant-design-vue3 tree树形控件的使用_antdesign vue tree

antdesign vue tree

使用场景

        在前端日常开发中,经常遇到菜单列表,权限列表等等涉及到多层级结构的需求,那么这时候使用树形控件结构,就可以很容易的解决这些问题,当前使用的ant-design-vue3.0版本中,使用树形结构进行展示。

        原文档说明:文件夹、组织架构、生物分类、国家地区等等,世间万物的大多数结构都是树形结构。使用树控件可以完整展现其中的层级关系,并具有展开收起选择等交互功能。

设计方案

    场景一:项目呈树形结构展开。

  1. <template>
  2. <div class="practice" style="margin:0 auto;">
  3. <div @click="showCheckedTree" style="margin:0 auto;height: 100px;width: 100px;display: flex;justify-content: center; align-items: center;background: green;">展开树</div>
  4. <div style="margin:0 auto;width: 400px;display: flex;justify-content: center; align-items: center;">
  5. <a-tree :tree-data="treeData" defaultExpandAll v-model:selectedKeys="selectedKeys" @select="selectClass" />
  6. </div>
  7. </div>
  8. </template>
  9. <script setup>
  10. import { ref, onBeforeMount } from 'vue';
  11. const collageList = ref(
  12. [
  13. {
  14. "title": "训练平台",
  15. "key": 480306,
  16. "children": [
  17. {
  18. "title": "运营部",
  19. "key": "4186473299879919616",
  20. "children": [
  21. {
  22. "title": "运营部美术专业",
  23. "key": "4186473887581601792",
  24. "isLeaf": true
  25. },
  26. {
  27. "title": "运营部研发专业",
  28. "key": "4186474029177110528",
  29. "isLeaf": true
  30. },
  31. {
  32. "title": "运营部决策专业",
  33. "key": "4186474205962829824",
  34. "isLeaf": true
  35. },
  36. {
  37. "title": "运营部综合专业",
  38. "key": "4186480772389011456",
  39. "isLeaf": true
  40. }
  41. ]
  42. },
  43. {
  44. "title": "市场部",
  45. "key": "4186473362115002368",
  46. "children": [
  47. {
  48. "title": "市场部决策专业",
  49. "key": "4186474363236646912",
  50. "isLeaf": true
  51. },
  52. {
  53. "title": "市场部产品专业",
  54. "key": "4186474440822882304",
  55. "isLeaf": true
  56. },
  57. {
  58. "title": "市场部运营专业",
  59. "key": "4186474560486375424",
  60. "isLeaf": true
  61. }
  62. ]
  63. },
  64. ]
  65. }
  66. ]
  67. )
  68. const treeData = ref([])
  69. const expandedKeys = ref([])
  70. const selectedKeys = ref([])
  71. const checkedKeys = ref([])
  72. //选中事件 当点击标题时,触发事件
  73. const selectClass = ([selectedKeys], e) => {
  74. //输出选中的key值
  75. console.log(selectedKeys)
  76. }
  77. const showCheckedTree = ()=>{
  78. treeData.value = collageList.value
  79. }
  80. onBeforeMount(()=>{
  81. //场景一 屏蔽该代码,点击展开树,发现未展开 defaultExpandAll 未生效
  82. //场景二 在渲染生命周期前加载数据,defaultExpandAll 生效
  83. showCheckedTree()
  84. })
  85. </script>
  86. <style lang="less" scoped></style>

    场景二:树形结构+复选框展开,关闭,父级和子级之间有关联关系。

<a-tree :tree-data="treeData" defaultExpandAll v-model:selectedKeys="selectedKeys" @select="selectClass" checkable v-model:checkedKeys="checkedKeys"/>

    场景三:树形结构+复选框展开,关闭,父级和子级之间无关联关系。

<a-tree :tree-data="treeData" defaultExpandAll v-model:selectedKeys="selectedKeys" @select="selectClass" checkable checkStrictly v-model:checkedKeys="checkedKeys"/>

   

遇到问题

  1、树形控件在使用 defaultExpandAll 不展开问题?

    在使用defaultExpandAll时,需要在生命周期onMounted之前获取到完成的数据,渲染完成之后,再点击事件,加载数据,defaultExpandAll 失效,未展开。

  2、在使用 checkable 时,选择之后,容易导致子元素被选择,如何区分开来?

   在使用 checkable 是,默认选中父级元素,子级元素也会被选中,相当于全选的效果,需要需要去除,可以在 checkStrictly 

  3、树形控件使用是建议使用绑定treedata数据结构的形式,不要使用遍历循环的模式,如果数据格式和上图代码中的结构不一致,可以循环处理,成对应的数据结构,再进行渲染。

  4、在使用checkable模式的时候,checkedKeys 是复选框打钩的key值集合,不要使用 selectedKeys 作为选中的值。

  5、select 是点击树节点触发事件,当点击对应的标题,就会触发对应的时间,返回的 selectedKeys 是一个数组集合。

  6、第一行只能展开,不能收缩起来。

   主要原因是使用了 :autoExpandParent="true" 这个方法,当使用这个方法时,首行就自动展开,收缩不起来了,不论怎么点击都没有用;建议使用该方法时,慎用。

  7、在获取选中的数据是,是否使用 checkStrictly 选择数据的区别在哪里?

    在未使用 checkStrictly 时,获取选中的数据,直接使用 treeData.value.checkedKeys;

    在使用时,获取选中的数据,用 treeData.value.checkedKeys.checked;

总结分享

       在实践中学习,在学习中成长,在成长中实践,不断循环迭代。在使用一种框架时,不仅仅是使用该框架本身,更多的是要思考,在使用框架的过程中,如何将这种运用框架的能力移植到其它的框架上,毕竟互联网技术更新快,前端框架也是层出不穷,如何将这种能力梳理成一种底层的能力,这才是核心的核心。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/231585?site
推荐阅读
相关标签
  

闽ICP备14008679号