vue element-ui tree组件,根据不同的状态显示不同的字体颜色添加自定义图标或者图片

介绍

        实例代码使用的ruoyi微服务项目

一、前端

        主要是添加 :render-content="renderContent"

<div class="tree-container" >
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
            :render-content="renderContent"
          />
 </div>

 renderContent(h, { node, data, store }) {
      // 确定节点颜色
      const nodeColor = data.isKeynote === "1" ? "red" : "#606266";
      let  nodeContent = "  " + node.label;
      let extraContent;
      // 根据节点层级确定要显示的图标
      if (node.level === 1) {
        extraContent = <svg-icon icon-class="tree1" class-name="custom-class"/>;
      } else if (node.level === 2) {
        extraContent = <svg-icon icon-class="tree2" class-name="custom-class"/>;
      }else if (node.level === 3) {
        extraContent = <svg-icon icon-class="tree3" class-name="custom-class"/>;
      }else if (node.level === 4) {
        extraContent = <svg-icon icon-class="tree4" class-name="custom-class" />;
      }
      // 构建节点内容
      // 返回带有相应样式的 JSX 元素
      return (
        <span style={`color:${nodeColor}; font-size: 14px`}>
          {extraContent}
          {nodeContent}
        </span>
      );
    },

二、后端

后端采用若依微服务架构

首先找到TreeSelect.java实体类文件 添加字段 isKeynote (自定义)

package com.ruoyi.system.domain.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.domain.SysMenu;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Treeselect树结构实体类
 *
 * @author ruoyi
 */
public class TreeSelectVo implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 节点ID */
    private Long id;

    /** 节点名称 */
    private String label;

    /** 是否为重点区域 */
    private String isKeynote;

    /** 子节点 */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<TreeSelectVo> children;

    public TreeSelectVo()
    {

    }

    public TreeSelectVo(SysDept dept)
    {
        this.id = dept.getDeptId();
        this.label = dept.getDeptName();
        this.isKeynote = dept.getIsKeynote();
        this.children = dept.getChildren().stream().map(TreeSelectVo::new).collect(Collectors.toList());
    }

    public TreeSelectVo(SysMenu menu)
    {
        this.id = menu.getMenuId();
        this.label = menu.getMenuName();
        this.children = menu.getChildren().stream().map(TreeSelectVo::new).collect(Collectors.toList());
    }

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getIsKeynote() {
        return isKeynote;
    }

    public void setIsKeynote(String isKeynote) {
        this.isKeynote = isKeynote;
    }

    public String getLabel()
    {
        return label;
    }

    public void setLabel(String label)
    {
        this.label = label;
    }

    public List<TreeSelectVo> getChildren()
    {
        return children;
    }

    public void setChildren(List<TreeSelectVo> children)
    {
        this.children = children;
    }
}

然后修改 “构建前端所需要下拉树结构” 方法  将 TreeSelect 修改为 TreeSelectVo即可 

当然,返回的List集合中的类  也是TreeSelectVo

/**
     * 构建前端所需要下拉树结构 多几个字段
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelectVo> buildDeptTreeVoSelect(List<SysDept> depts)
    {
        List<SysDept> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(TreeSelectVo::new).collect(Collectors.toList());
    }

效果图

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值