springMVC+easyUI实现combobox动态级联查询

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yulsh/article/details/52598257

html

<p>
   <label>责任区域</label><input id="areaId" name="areaId" class="query e-input" type="text" />
</p>
<p>
    <label>小区</label><input id="communityId" name="communityId" class="query e-input" type="text" />
</p>

js代码

$('#areaId').combobox({
        url : '../../data/area/getArea.jhtml',  //后台请求地址
        editable:false, //不可编辑状态
        cache: false,
        panelHeight: 'auto',
        valueField:'id',
        textField:'name',
        onSelect : function(rec){
            $('#communityId').combobox('clear');
            var url = '../../data/community/getCommunity.jhtml?areaId=' + rec.id;
            $('#communityId').combobox('reload', url);
        }
    });

    $('#communityId').combobox({
        panelHeight : 'auto',
        valueField:'id',
        textField:'name'
    });

后台请求代码:

@RequestMapping(value = "/getArea", produces = "application/json")
    @ResponseBody
    public List<View> getArea(){
        Long committeeId = null;
        List<Area> areaList = service.getAreaByCommittee(committeeId);
        List<View> view = new ArrayList<View>();
        try {
            if (areaList != null) {
                view.add(new View(null, "请选择"));
                for (Area area : areaList) {
                    view.add(new View(area.getId(), area.getName()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

@RequestMapping(value = "/getCommunity", produces = "application/json")
    @ResponseBody
    public List<View> getCommunity(String areaId) {
        List<Community> cList = new ArrayList<>();
            if (!"null".equals(areaId)) {//不选的时候前台会传一个null字符串过来
            cList = service.getCommunityByArea(Long.parseLong(areaId));
        } else {
            cList = service.findAll(Community.class);
        }
        List<View> view = new ArrayList<View>();
        try {
            if (cList != null) {
                view.add(new View(null, "请选择"));
                for (Community community : cList) {
                    view.add(new View(community.getId(), community.getName()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

View类:

public class View {

    private Long id;

    private String name;

    public View() {

    }

    public View(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
展开阅读全文

没有更多推荐了,返回首页