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

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

部门实体类:

 1 import lombok.Data;
 2 
 3 @Data
 4 public class Department {
 5     private Integer id;
 6 
 7     private Integer parentId;
 8 
 9     private String name;
10 
11     public Department(Integer id, Integer parentId, String name) {
12         this.id = id;
13         this.parentId = parentId;
14         this.name = name;
15     }
16 
17     public static TreeItem deptNode(Department dept) {
18         TreeItem treeItem = new TreeItem();
19         treeItem.setId(dept.getId());
20         treeItem.setName(dept.getName());
21         treeItem.setParentId(dept.getParentId());
22         return treeItem;
23     }
24 }

用户实体类:

 1 import lombok.Data;
 2 
 3 @Data
 4 public class Employee {
 5 
 6     private Integer id;
 7 
 8     private String name;
 9 
10     private Integer deptId;
11 
12 
13     public Employee(Integer id, String name, Integer deptId) {
14         this.id = id;
15         this.name = name;
16         this.deptId = deptId;
17     }
18 
19     public static TreeItem empNode(Employee emp) {
20         TreeItem treeItem = new TreeItem();
21         treeItem.setId(emp.getId());
22         treeItem.setName(emp.getName());
23         treeItem.setParentId(emp.getDeptId());
24         return treeItem;
25     }
26 }

返回数据实体类:

 1 import lombok.Data;
 2 
 3 import java.util.List;
 4 
 5 @Data
 6 public class TreeItem {
 7     private Integer id;
 8 
 9     private Integer parentId;
10 
11     private String name;
12 
13     private List<TreeItem> children;
14 }

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

 1 import java.util.ArrayList;
 2 import java.util.Arrays;
 3 import java.util.List;
 4 import java.util.Map;
 5 import java.util.stream.Collectors;
 6 
 7 public class App {
 8     public static void main(String[] args) {
 9         //创建部门
10         List<Department> deptList = Arrays.asList(
11                 new Department(1, 0, "星宇"),
12                 new Department(2, 1, "金3"),
13                 new Department(3, 2, "金3/1"),
14                 new Department(4, 2, "金3/2"),
15                 new Department(5, 1, "总1"),
16                 new Department(6, 5, "总1/2")
17 
18         );
19         //创建员工
20         List<Employee> empList = Arrays.asList(
21                 new Employee(1, "张三", 5),
22                 new Employee(2, "李四", 6),
23                 new Employee(3, "王五", 2),
24                 new Employee(4, "赵六", 2),
25                 new Employee(5, "田七", 3),
26                 new Employee(6, "牛八", 4)
27         );
28         //转换成结点
29         List<TreeItem> allDept = deptList.stream()
30                 .map(Department::deptNode)
31                 .collect(Collectors.toList());
32         //按照上级部门分组
33         Map<Integer, List<TreeItem>> deptMap = allDept.stream()
34                 .collect(Collectors.groupingBy(TreeItem::getParentId));
35         //给每个部门绑定子部门
36         allDept.forEach(node ->
37                 node.setChildren(deptMap.get(node.getId()))
38         );
39 
40         List<TreeItem> treeItem = deptMap.get(0);
41 
42         Map<Integer, List<TreeItem>> empMap = empList.stream()
43                 .filter(emp -> emp.getDeptId() != null)
44                 .map(Employee::empNode)
45                 .collect(Collectors.groupingBy(TreeItem::getParentId));
46         //BFS辅助队列
47         List<TreeItem> queue = new ArrayList<>();
48         queue.addAll(treeItem);
49         for (int i = 0; i < queue.size(); i++) {
50             TreeItem node = queue.get(i);
51             //遍历时先将子部门放入队列中
52             if (node.getChildren() != null) {
53                 queue.addAll(node.getChildren());
54             } else {
55                 node.setChildren(new ArrayList<>());
56             }
57             //再将部门成员放入子结点中
58             List<TreeItem> children = empMap.get(node.getId());
59             if (children != null) {
60                 node.getChildren().addAll(children);
61             }
62         }
63 
64         treeItem.forEach(x -> System.out.println(x));
65 
66     }
67 }
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级联选择器。

posted @ 2019-03-08 17:56  Java偷走了我的微笑  阅读(10562)  评论(9编辑  收藏  举报