• VueTreeselect在使用过程中遇到的问题及解决方法


    一、简单介绍

    vue-treeselect 是一个多选组件,具有对 Vue.js嵌套选项支持

    • 支持嵌套选项的单选和多选
    • 模糊匹配
    • 异步搜索
    • 延迟加载(仅在需要时加载深度选项的数据)
    • 键盘支持(使用Arrow Up & Arrow Down键导航,使用键选择选项Enter等)
    • 丰富的选项和高度可定制的
    • 支持 多种浏览器

    需要Vue 2.2+

    具体样式展示:

     

    二、安装及使用

    2.1、安装

    npm install --save @riophae/vue-treeselect

    2.2、引入组件和样式

    1. import treeSelect from "@riophae/vue-treeselect";
    2. import "@riophae/vue-treeselect/dist/vue-treeselect.css";

    2.3、注册

    1. components: {
    2. treeSelect
    3. },

    2.4、使用

    1. <treeSelect v-model="value"
    2. :options="options"
    3. :show-count="true"
    4. :normalizer="normalizer"
    5. placeholder="please select……" />
    1. data() {
    2. return {
    3. // define the default value
    4. value: null,
    5. // define options
    6. options: [{
    7. id: 'a',
    8. label: 'a',
    9. children: [{
    10. id: 'aa',
    11. label: 'aa'
    12. }, {
    13. id: 'ab',
    14. label: 'ab'
    15. }],
    16. }, {
    17. id: 'b',
    18. label: 'b'
    19. }, {
    20. id: 'c',
    21. label: 'c'
    22. }]
    23. },
    24. normalizer(node) {
    25. if (node.children && !node.children.length) {
    26. delete node.children;
    27. }
    28. return {
    29. id: node.id, //自定义id
    30. label: node.label, //自定义label
    31. children: node.children && node.children.length > 0 ? node.children: 0 //处理children,当children为[]或null时,子节点会展示No sub-options,一般子节点不需要展示这个,所以这样处理
    32. };
    33. }
    34. }
    35. }

    三、遇到的问题及解决方法

    3.1、后台返回的数据格式不是标准的,即没有id和label

    解决方法:增加normalizer属性,将id和label字段转换,比如部门接口返的数据是deptId和deptName,需要做如下处理

    1. normalizer(node) {
    2. if (node.children && !node.children.length) {
    3. delete node.children;
    4. }
    5. return {
    6. id: node.deptId, //自定义id
    7. label: node.deptName, //自定义label
    8. }
    9. }

    3.2、叶子结点不期望展示下拉角标和提示文字(No sub-options)

     

    原因分析:是由于children[ ]null 造成的

    解决方法children[ ]或null时,将children删除,可以自己写方法遍历删除,也可以在normalizer中处理。以下是最简单的处理方法:

    1. normalizer(node) {
    2. if (node.children && !node.children.length) {
    3. delete node.children;
    4. }
    5. return {
    6. id: node.id, //自定义id
    7. label: node.label, //自定义label
    8. //处理children,当children为[]或null时,子节点会展示No sub-options,一般子节点不需要展示这个,所以将children置为0
    9. children: node.children && node.children.length > 0 ? node.children: 0
    10. };
    11. }
    12. }

  • 相关阅读:
    leecode面试题 04.10. 检查子树
    2022年下半年软考报名常见问题都在这里啦!
    Nginx优化与防盗链
    实例解释遇到前端报错时如何排查问题
    始祖双碳新闻 | 2022年8月15日碳中和行业早知道
    2022-11-16 Acwing每日一题
    信号的机制——信号处理函数的注册
    What does rail-to-rail mean (Rail-to-Rail Op amp) ?
    人大金仓分析型数据库系统扩容(一)
    用MicroPython开发ESP32-文件传输工具-ampy
  • 原文地址:https://blog.csdn.net/chenwei1113/article/details/125634677