VUE-ELEMENT-Tree-全加载树,后端排序


/**
 * 树类
 *
 */
@Data
public class TreeInfoVO extends BaseVO {
	/**节点ID*/
	private String id;
	/**父ID*/
	private String parentId;
	/**名称*/
	private String name;

	private List<TreeInfoVO> children;

}

//业务层
package com.jysoft.service;

import com.jysoft.dao.entity.Company;
import com.jysoft.dao.entity.Device;
import com.jysoft.dao.entity.Station;
import com.jysoft.dao.mapper.CompanyMapper;
import com.jysoft.dao.mapper.DeviceMapper;
import com.jysoft.dao.mapper.StationMapper;
import com.jysoft.dao.mapper.VoiceprintMapper;
import com.jysoft.entity.company.vo.CompanyVO;
import com.jysoft.entity.company.vo.TreeInfoVO;
import com.jysoft.resp.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class CompanyService {

    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    StationMapper stationMapper;
    @Autowired
    DeviceMapper deviceMapper;
    @Autowired
    VoiceprintMapper voicePrintMapper;

    public ResponseResult<?> queryCompanyTreeMainInfo() {
        Device device = new Device();
        Station station = new Station();
        List<Company> companyList = companyMapper.queryAllCompany();
        List<Station> stationList = stationMapper.queryAllStations(station);
        List<Device> deviceList = deviceMapper.queryDevices(device);
        List<TreeInfoVO> treeInfoVOListFromComany = companyBuildEntity(companyList);
        List<TreeInfoVO> treeInfoVOListFromStation = stationBuildEntity(stationList);
        List<TreeInfoVO> treeInfoVOListFromDevice = deviceBuildEntity(deviceList);
        int lengthNumberOne = treeInfoVOListFromComany.size();
        for (int i = 0; i < treeInfoVOListFromStation.size(); i++) {
            treeInfoVOListFromComany.add(lengthNumberOne + i - 1, treeInfoVOListFromStation.get(i));
        }
        int lengthNumberTwo = treeInfoVOListFromComany.size();
        for (int i = 0; i < treeInfoVOListFromDevice.size(); i++) {
            treeInfoVOListFromComany.add(lengthNumberTwo + i - 1, treeInfoVOListFromDevice.get(i));
        }
        // 把所有的数据都放到map中
        Map<String, TreeInfoVO> treeMap = new HashMap<>();
        for (int i = 0; i < treeInfoVOListFromComany.size() && !treeInfoVOListFromComany.isEmpty(); i++) {
            // 元素的id为键,元素本身为值
            treeMap.put(treeInfoVOListFromComany.get(i).getId(), treeInfoVOListFromComany.get(i));
        }
        // 将所有顶层节点存入result中
        List<TreeInfoVO> result = new ArrayList<>();
        // 遍历map得到顶层节点或者游离节点
        for (TreeInfoVO treeInfoVO : treeInfoVOListFromComany) {
            if (!treeMap.containsKey(treeInfoVO.getParentId())) {
                result.add(treeInfoVO);
            }
        }
        setParentChildRelative(treeInfoVOListFromComany, treeMap);
        return ResponseResult.success(result);
    }

    private List<TreeInfoVO> companyBuildEntity(List<Company> companyList) {
        List<TreeInfoVO> treeInfoVOList = new ArrayList<>();
        companyList.forEach(company -> {
            TreeInfoVO treeInfoVO = new TreeInfoVO();
            treeInfoVO.setId(company.getId());
            treeInfoVO.setName(company.getName());
            treeInfoVO.setChildren(new ArrayList<>());
            treeInfoVO.setParentId(company.getParentId());
            treeInfoVOList.add(treeInfoVO);
        });
        return treeInfoVOList;

    }

    private List<TreeInfoVO> stationBuildEntity(List<Station> stationList) {
        List<TreeInfoVO> treeInfoVOList = new ArrayList<>();
        stationList.forEach(station -> {
            TreeInfoVO treeInfoVO = new TreeInfoVO();
            treeInfoVO.setId(station.getId());
            treeInfoVO.setName(station.getName());
            treeInfoVO.setChildren(new ArrayList<>());
            treeInfoVO.setParentId(station.getCompanyId());
            treeInfoVOList.add(treeInfoVO);
        });
        return treeInfoVOList;
    }

    private List<TreeInfoVO> deviceBuildEntity(List<Device> deviceList) {
        List<TreeInfoVO> treeInfoVOList = new ArrayList<>();
        deviceList.forEach(device -> {
            TreeInfoVO treeInfoVO = new TreeInfoVO();
            treeInfoVO.setId(device.getId());
            treeInfoVO.setName(device.getDeviceName());
            treeInfoVO.setChildren(new ArrayList<>());
            treeInfoVO.setParentId(device.getStationId());
            treeInfoVOList.add(treeInfoVO);
        });
        return treeInfoVOList;
    }


    public void setParentChildRelative(List<TreeInfoVO> list, Map<String, TreeInfoVO> treeMap) {
        for (int i = 0; i < list.size() && !list.isEmpty(); i++) {
            TreeInfoVO treeInfoVO = treeMap.get(list.get(i).getParentId());
            if (treeInfoVO != null) {
                if (treeInfoVO.getChildren() == null) {
                    treeInfoVO.setChildren(new ArrayList<>());
                }
                treeInfoVO.getChildren().add(list.get(i));
            }
        }
    }

}

思路:
将所有结果集先转换为TreeInfo类,再将集合合并为一个集合,并转换为Map类;并且循环集合,根据对象的parentId来设置父子关系

前端代码

  <el-tree
              ref="treeBox"
              :data="treeList"
              :props="defaultProps"
              accordion
              :default-expanded-keys="expandedKeys"
              node-key="id"
          >
          </el-tree>


  • treeBox ||this.$refs可以直接指向该标签
  • treeLisrt || 后端传过来的数据 注意是个集合 不能直接将树的根类传过来
  • defaultProps
    id: ‘id’, //标识每个结点
    label: ‘name’, //结点的文本展示
    children: ‘children’ //结点的子节点
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值