工具类大致作用是:将数据库中取出的数据,重新组装成适应cascader级联选择的:options参数格式,即JSONArray格式,代码如下:
首先,定义一个实体类,提供如cascader中相同的参数信息,
@Data
public class AllChannelVo {
/**
* label名称-》省名,城市名,线路名
*/
public String label;
/**
* value信息,省code,城市id,线路id
*/
public String value;
/**
* 子节点内容
*/
public List<AllChannelVo> children;
然后就可以写工具类了,值得注意的是,工具类调用service方法,需要以下步骤:
1.添加@Component注解
2.挂载service类
3.声明一个静态的本工具类对象
4.init()方法,@PostConstruct注解
5.使用时需要用静态的工具类对象调用service对象
参考自这位老哥的文章
@Component
public class RetCascaderJsonUtil {
//静态的本类
private static RetCascaderJsonUtil rcj;
@Autowired
private IMetCityService cityService;
@Autowired
private IMetRouteService routeService;
@Autowired
private IMetSiteService siteService;
@Autowired
private ISysDictDataService dictDataService;
//字典类型确定
static final String DICT_TYPE = "met_province_type";
@PostConstruct
public void init(){
rcj = this;
rcj.cityService = this.cityService;
rcj.dictDataService = this.dictDataService;
rcj.routeService = this.routeService;
rcj.siteService = this.siteService;
}
/**
* 通过cascaderLevel判断返回二级或三级级联Json数据
* @param cascaderLevel
* @return
*/
public JSONArray CascaderJson(int cascaderLevel){
//查询字典中的 省份
List<SysDictData> dictList = new ArrayList<>();
dictList = rcj.dictDataService.selectDictDataByType(DICT_TYPE);
//查询城市表中的 城市名
List<MetCity> cityList = new ArrayList<>();
cityList = rcj.cityService.list(new LambdaQueryWrapper<MetCity>()
.eq(MetCity::getIsDeleted,0));
//查询线路表中的 线路名
List<MetRoute> routeList = new ArrayList<>();
routeList = rcj.routeService.list(new LambdaQueryWrapper<MetRoute>()
.eq(MetRoute::getIsDeleted,0)
.eq(MetRoute::getIsEnable,0));
//最上级list,针对省份
List<AllChannelVo> topList = new ArrayList<>();
for (SysDictData dictData: dictList
) {
AllChannelVo channel1 = new AllChannelVo();
channel1.setValue(dictData.getDictCode().toString());
channel1.setLabel(dictData.getDictLabel());
//第二级的list,针对城市
List<AllChannelVo> sedList = new ArrayList<>();
for (MetCity city: cityList
) {
if (city.getProvinceName().equals(dictData.getDictLabel())){
AllChannelVo channel2 = new AllChannelVo();
channel2.setValue(city.getCityId().toString());
channel2.setLabel(city.getCityName());
if(cascaderLevel == 3){
//第三级的list,针对线路
List<AllChannelVo> trdList = new ArrayList<>();
for (MetRoute route: routeList
) {
if (route.getCityName().equals(city.getCityName())){
AllChannelVo channel3 = new AllChannelVo();
channel3.setLabel(route.getRouteName());
channel3.setValue(route.getRouteId().toString());
channel3.setChildren(null);
trdList.add(channel3);
}
}
channel2.setChildren(trdList);
sedList.add(channel2);
}else {
channel2.setChildren(null);
sedList.add(channel2);
}
}
}
channel1.setChildren(sedList);
topList.add(channel1);
}
JSONArray json = JSONArray.parseArray(JSON.toJSONString(topList));
return json;
}
ok,大功告成
追加一个前端实现的方式(相对来说更简洁,也可以减少服务器资源占用)
this.cityNameOptions.forEach(item1 => {
let city = {
value : item1.cityCode,
label : item1.cityName,
children : []
}
this.routeNameOptions.forEach(item2 => {
let route = {
value : item2.routeId,
label : item2.routeName,
children : []
}
this.siteNameOptions.forEach(item3 => {
let site = {
value : item3.siteCode,
label : item3.siteName
}
if(item2.routeId === item3.routeId){
route.children.push(site)
}
})
if(item1.cityId === item2.cityId){
city.children.push(route)
}
})
this.objs.push(city)
})