Java构建Cascader 二级数据
创建构建对象
public class productCodeTree {
private String value;
private String label;
private List<productCodeTree> children;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public List<productCodeTree> getChildren() {
return children;
}
public void setChildren(List<productCodeTree> children) {
this.children = children;
}
}
构建数据
public List<productCodeTree> ListPlan() {
// 查询一级数据
List<CloudstorageProduct> cloudstorageProducts = cloudstorageProductMapper.selectAll();
// 一级list
final List<productCodeTree> treeAll = new ArrayList<>();
// 判空
if (CollectionUtils.isNotEmpty(cloudstorageProducts)) {
cloudstorageProducts.forEach(s -> {
// 存储对象
productCodeTree productCodeTree = new productCodeTree();
// 查询二级数据
List<CloudstoragePlan> plans = cloudstoragePlanMapper.selectPlanByCode(s.getProductCode());
// 判空
if (CollectionUtils.isNotEmpty(plans)){
// 二级list
List<productCodeTree> tree = new ArrayList<>();
// 循环set二级数据
plans.forEach(r -> {
productCodeTree codeTree = new productCodeTree();
codeTree.setLabel(r.getPlanName());
codeTree.setValue(String.valueOf(r.getPlanId()));
tree.add(codeTree);
});
productCodeTree.setLabel(s.getProductName());
productCodeTree.setValue(s.getProductName());
productCodeTree.setChildren(tree);
treeAll.add(productCodeTree);
}
});
}
return treeAll;
}
三级四级以此类推就行
一级的话一个循环就行