This website requires JavaScript.

ElementUI组件Cascader级联选择器数据后台处理

Cascader级联选择器数据数据格式不知道的可以去官网看下;这里我就不表示什么了。

部门实体类:

import lombok.Data;

@Data
public class Department {
    private Integer id;

    private Integer parentId;

    private String name;

    public Department(Integer id, Integer parentId, String name) {
        this.id = id;
        this.parentId = parentId;
        this.name = name;
    }

    public static TreeItem deptNode(Department dept) {
        TreeItem treeItem = new TreeItem();
        treeItem.setId(dept.getId());
        treeItem.setName(dept.getName());
        treeItem.setParentId(dept.getParentId());
        return treeItem;
    }
}

用户实体类:

import lombok.Data;

@Data
public class Employee {

    private Integer id;

    private String name;

    private Integer deptId;


    public Employee(Integer id, String name, Integer deptId) {
        this.id = id;
        this.name = name;
        this.deptId = deptId;
    }

    public static TreeItem empNode(Employee emp) {
        TreeItem treeItem = new TreeItem();
        treeItem.setId(emp.getId());
        treeItem.setName(emp.getName());
        treeItem.setParentId(emp.getDeptId());
        return treeItem;
    }
}

返回数据实体类:

import lombok.Data;

import java.util.List;

@Data
public class TreeItem {
    private Integer id;

    private Integer parentId;

    private String name;

    private List<TreeItem> children;
}

数据处理操作,这里是用java8新的特性写的。不懂的可以学习下,挺好用的

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class App {
    public static void main(String[] args) {
        //创建部门
        List<Department> deptList = Arrays.asList(
                new Department(1, 0, "星宇"),
                new Department(2, 1, "金3"),
                new Department(3, 2, "金3/1"),
                new Department(4, 2, "金3/2"),
                new Department(5, 1, "总1"),
                new Department(6, 5, "总1/2")

        );
        //创建员工
        List<Employee> empList = Arrays.asList(
                new Employee(1, "张三", 5),
                new Employee(2, "李四", 6),
                new Employee(3, "王五", 2),
                new Employee(4, "赵六", 2),
                new Employee(5, "田七", 3),
                new Employee(6, "牛八", 4)
        );
        //转换成结点
        List<TreeItem> allDept = deptList.stream()
                .map(Department::deptNode)
                .collect(Collectors.toList());
        //按照上级部门分组
        Map<Integer, List<TreeItem>> deptMap = allDept.stream()
                .collect(Collectors.groupingBy(TreeItem::getParentId));
        //给每个部门绑定子部门
        allDept.forEach(node ->
                node.setChildren(deptMap.get(node.getId()))
        );

        List<TreeItem> treeItem = deptMap.get(0);

        Map<Integer, List<TreeItem>> empMap = empList.stream()
                .filter(emp -> emp.getDeptId() != null)
                .map(Employee::empNode)
                .collect(Collectors.groupingBy(TreeItem::getParentId));
        //BFS辅助队列
        List<TreeItem> queue = new ArrayList<>();
        queue.addAll(treeItem);
        for (int i = 0; i < queue.size(); i++) {
            TreeItem node = queue.get(i);
            //遍历时先将子部门放入队列中
            if (node.getChildren() != null) {
                queue.addAll(node.getChildren());
            } else {
                node.setChildren(new ArrayList<>());
            }
            //再将部门成员放入子结点中
            List<TreeItem> children = empMap.get(node.getId());
            if (children != null) {
                node.getChildren().addAll(children);
            }
        }

        treeItem.forEach(x -> System.out.println(x));

    }
}
TreeItem(id=1, parentId=0, name=星宇, children=[TreeItem(id=2, parentId=1, name=金3, children=[TreeItem(id=3, parentId=2, name=金3/1, children=[TreeItem(id=5, parentId=3, name=田七, children=null)]), TreeItem(id=4, parentId=2, name=金3/2, children=[TreeItem(id=6, parentId=4, name=牛八, children=null)]), TreeItem(id=3, parentId=2, name=王五, children=null), TreeItem(id=4, parentId=2, name=赵六, children=null)]), TreeItem(id=5, parentId=1, name=总1, children=[TreeItem(id=6, parentId=5, name=总1/2, children=[TreeItem(id=2, parentId=6, name=李四, children=null)]), TreeItem(id=1, parentId=5, name=张三, children=null)])])

上面是我打印出来的数据,其实你们只要返回treeItem这个集合就可以了,这个数据结构符合Cascader级联选择器。

本文转自 https://www.cnblogs.com/jiangweichao/p/10497354.html

0条评论
avatar