iview填坑之Tree树型组件

本文主要介绍了如何处理iview Tree组件的数据,当后端提供的数据非树形结构时,需要前端进行转换。同时,针对iview官方样式的局限,文章提供了使用render函数自定义样式的方法,包括点击查询、选中效果以及自定义鼠标样式等功能,以满足实际业务需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<Tree ref="tree" :data="treeContents" :render="renderContent"></Tree>

1. 先来说说数据

iview的Tree组件的data数据是树结构的,例如下面代码所示:

 export default {
        data () {
            return {
                treeContents: [
                    {
                        title: '一级目录',
                        expand: true,
                        children: [
                            {
                                title: '二级目录1',
                                expand: true,
                                children: [
                                    {
                                        title: '三级目录1'
                                    },
                                    {
                                        title: '三级目录2'
                                    }
                                ]
                            },
                            {
                                title: '二级目录2',
                                expand: true,
                                children: [
                                    {
                                        title: '三级目录1'
                                    },
                                    {
                                        title: '三级目录2'
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        }

但是,如果后端提供的数据,不是上面的树型结构数据时,就需要前端对后端提供的数据进行“树型处理”。用到数据结构中树的遍历相关知识。this.convertTree(data.root, this.map);对后端提供的数据进行“树型处理”。比如要构造处下面这种数据形式的tree目录:

 export default {
        data () {
            return {
                treeContents: [
                    {
                        id: 'id',
                        parentId: 'parentId',
                        code: 'code',
                        name: '一级目录',
                        expand: true,
                        children: [
                            {
                                id: 'id',
                                parentId: 'parentId',
                                code: 'code',
                                name: '二级目录1',
                                expand: true,
                                children: [
                                    {
                                        id: 'id',
                                        parentId: 'parentId',
                                        code: 'code',
                                        name: '三级目录1',
                                        expand: true,
                                        children: []
                                    },
                                    {
                                        id: 'id',
                                        parentId: 'parentId',
                                        code: 'code',
                                        name: '三级目录2',
                                        expand: true,
                                        children: []
                                    }
                                ]
                            },
                            {
                                id: 'id',
                                parentId: 'parentId',
                                code: 'code',
                                name: '二级目录2',
                                expand: true,
                                children: [
                                    {
                                        id: 'id',
                                        parentId: 'parentId',
                                        code: 'code',
                                        name: '三级目录1',
                                        expand: true,
                                        children: []
                                    },
                                    {
                                        id: 'id',
                                        parentId: 'parentId',
                                        code: 'code',
                                        name: '三级目录2',
                                        expand: true,
                                        children: []
                                    }
                                ]
                            },
                        ]
                    }
                ]
            }
        }

构造代码如下: 

export default {
    data() {
      return {
        treeContents: [],
        map: {
          id: 'id',
          parentId: 'parentId',
          code: 'code',
          name: 'name',
          children: 'subSort'
        },
        expand: true,
      }
    },
    methods: {
      ...mapActions('serviceSort', ['getTreeContents']),
      // 获取树形目录
      doTreeContents() {
        this.getTreeContents({username: this.user.username}).then((data) => {
          if (data.success) {
            this.treeContents = this.convertTree(data.root, this.map);
          } else {
            this.treeContents = []
          }
        }).catch((error) => {
          console.log('error', error);
        });
      },
      // 树形数据转换
      convertTree(tree, map) {
        const result = [];
        if (tree) {
          tree.forEach((item) => {
            const id = item[map.id];
            const parentId = item[map.parentId];
            const code = item[map.code];
            const name = item[map.name];
            let children = item[map.children];
            const expand = this.expand;
            if (children) {
              children = this.convertTree(children, map)
            }
            result.push({id, parentId, code, name, children, expand})
          });
        } else {
          return []
        }
        return result
      },
    },
    mounted() {
      this.doTreeContents();
    }
  }

如果你希望一级目录是自己手动构造(静态),而是不是后端提供的,你可以这样:

      treeContents() {
        let result = [{
          nameame: '我管理的目录',
          expand: true,
          children: []
        }];
        if (!_.isEmpty(this.contentsData)) {
          this.contentsData.forEach((item) => {
            const id = item.id;
            const name = item.name;
            const code= item.code;
            result[0].children.push({id, name, code});
          });
        } else {
          return result = []
        }
        return result
      }

2. 再来说说样式

通常,iview官网提供的样式不适用于具体业务需求,这时就需要用render函数自定义,官网也提供了一个render自定义的例子,但是样式单一,比如没用图标,点击不了数据等等。下面这个例子就简单的解决了这些,点击目录查询,点击时有选中效果,如果您想默认选中第一个二级目录,请浏览最后一个例子:

      // 点击目录查询
      renderContent(h, {root, node, data}) {
        return h('Tooltip', {
          props: {
            content: data.name,
            maxWidth: '20px',
            delay: 500
          },
        }, [
          h('span', [
            h('Icon', {
              props: {
                type: 'md-compass',
                color: node.node.selected ? '#ff9900' : 'inherit'
              },
              style: {
                marginRight: '4px',
              },
            }),
            h('span', {
              style: {
                color: node.node.selected ? '#2d8cf0' : '#1a2846',
                fontWeight: node.node.selected ? 'bold' : 'normal',
              },
              on: {
                click: () => {
                  if (!node.node.selected) {
                    this.$refs.tree.handleSelect(node.nodeKey);
                    this.searchText(data.name);
                  }
                },
              }
            }, data.name)]),
        ]);
      },

如果一级目录是我们自己静态手动写的,不需要点击,鼠标放上面不是‘手型’,你可以这样:下面的例子当鼠标悬停在一级目录是,鼠标不是‘手型’,而是“文本型”:cursor: node.nodeKey !== 0 ? 'pointer' : 'text',。此外还实现初始时选中第一个二级目录的效果。

      // 点击目录查询
      renderContent(h, {root, node, data}) {
        //这条if代码块设置flag,只执行一次,实现初始时选中第一个二级目录
        if (node.nodeKey === 1 && this.flag) {
          this.flag = false;
          this.$refs.tree.handleSelect(node.nodeKey);
          this.searchText(data.name)
        }
        return h('Tooltip', {
          props: {
            content: data.name,
            maxWidth: '20px',
            delay: 500
          },
        }, [
          h('span', [
            h('Icon', {
              props: {
                type: 'md-compass',
                color: node.node.selected ? '#ff9900' : 'inherit'
              },
              style: {
                marginRight: '4px',
              },
            }),
            h('span', {
              style: {
                cursor: node.nodeKey !== 0 ? 'pointer' : 'text',
                color: node.node.selected ? '#2d8cf0' : '#1a2846',
                fontWeight: node.node.selected ? 'bold' : 'normal',
              },
              on: {
                click: () => {
                  if (!node.node.selected && node.nodeKey !== 0) {
                    this.$refs.tree.handleSelect(node.nodeKey);
                    this.searchText(data.name)
                  }
                },
              }
            }, data.name)]),
        ]);
      },

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值