Vant ui组件实现动态二级联动

效果展示:

数据库数据

后端实现

 

 

 封装成需要的数据

package cms.web.action;

import cms.bean.company.Company;
import cms.bean.company.CompanyMultistage;
import cms.service.company.CompanyService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@RestController
public class CompanyAction {

    @Autowired
    private CompanyService companyService;

    //二级联动数据
    @GetMapping("/company/data")
    public String findHobby() {
        List<Company> companyList = companyService.findCompany();
        HashMap map = new HashMap();//存教育单位
        List<CompanyMultistage> hobbyMultistageList = new ArrayList<>();

        HashMap mapx = new HashMap();//存湘钢单位
        List<CompanyMultistage> hobbyMultistageListw = new ArrayList<>();

        HashMap mapy = new HashMap();//存医疗单位
        List<CompanyMultistage> hobbyMultistageListy = new ArrayList<>();

        HashMap mapz = new HashMap();//存政府单位
        List<CompanyMultistage> hobbyMultistageListz = new ArrayList<>();

        for (Company company : companyList) {
            CompanyMultistage companyMultistage = new CompanyMultistage();

            if (company.getParentid() == 1) {//判断是否为湘钢单位
                companyMultistage.setText(company.getName());
                companyMultistage.setValue(String.valueOf(company.getWid()));
                hobbyMultistageListw.add(companyMultistage);

                mapx.put("text", company.getParentname());
                mapx.put("value", company.getParentid());
                mapx.put("children", hobbyMultistageListw);
            } else if (company.getParentid() == 2) {//判断是否为医疗单位
                companyMultistage.setText(company.getName());
                companyMultistage.setValue(String.valueOf(company.getWid()));
                hobbyMultistageListy.add(companyMultistage);

                mapy.put("text", company.getParentname());
                mapy.put("value", company.getParentid());
                mapy.put("children", hobbyMultistageListy);
            } else if (company.getParentid() == 3) {//判断是否为教育单位
                companyMultistage.setText(company.getName());
                companyMultistage.setValue(String.valueOf(company.getWid()));
                hobbyMultistageList.add(companyMultistage);

                map.put("text", company.getParentname());
                map.put("value", company.getParentid());
                map.put("children", hobbyMultistageList);

            }else if (company.getParentid() == 4) {//判断是否为政府单位
                companyMultistage.setText(company.getName());
                companyMultistage.setValue(String.valueOf(company.getWid()));
                hobbyMultistageListz.add(companyMultistage);

                mapz.put("text", company.getParentname());
                mapz.put("value", company.getParentid());
                mapz.put("children", hobbyMultistageListz);

            }
        }
        //将所有数据存hobbyLists里
        List<HashMap> companyLists = new ArrayList<>();
                companyLists.add(mapz);
                companyLists.add(mapx);
                companyLists.add(map);
                companyLists.add(mapy);

            return JSON.toJSONString(companyLists);
    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值