zoukankan      html  css  js  c++  java
  • 下拉菜单的级联操作

    在开发中常常会遇到菜单的级联操作,比如:国家、城市、乡镇的选择等。当选中某个国家的时候,后面的菜单会把该国家内的城市罗列出来,当选中城市的时候,后面的菜单会把对应的乡镇列出来。

    解决这种菜单的级联操作的办法,我理解的有两种:①使用js来实现,把页面所用到的级联数据放到js内,当页面加载完成后,通过js显示到对应的select内,这种方法的解决办法有很多种,最为直观的一种是放到多维数组中,每个人的思维不一样,这里就不详细解说。②使用ajax异步动态加载,然后显示到对应的select内,这种方法很便捷,建议在开发中使用。下面看一个开发中的小例子:

    JSP简短页面:

                <div class="form-group">
                    <label class="col-sm-2 control-label">设备类别</label>
                    <div class="col-sm-4">
                        <select class="basic-single" name="codeCategory" onchange="showCodeSubCate()" id="codeCategory" style=" 100%">
                        
                        </select>
                    </div>
                    <label class="col-sm-2 control-label">设备子类</label>
                    <div class="col-sm-4">
                        <select class="basic-single" name="codeSubCategory"  id="codeSubCate" disabled="disabled" style=" 100%">
                            <option value="">--请选择--</option>
                        </select>
                    </div>
                </div>

    该页面内涉及到了两个select,分别为:设备分类和设备子类,其中设备分类为一级菜单,设备子类为二级菜单,设备子类的显示内容由设备分类决定。

    下面来看ajax代码段:

    function addCodeCategory(){
            $.ajax({
                url: "<%=request.getContextPath()%>/facilitydict/showCodeCategory",
                async: false, //请求是否异步,默认为异步,这也是ajax重要特性
                type: "GET",   //请求方式
                success: function(result) {
                    result = $.parseJSON(result);
                    var data = result.data;
                    var codeCates = data.split(",");
                    str ='<option value="6801">--请选择--</option>';
                    for(x in codeCates){
                        str+='<option value="'+codeCates[x]+'">'+codeCates[x]+'</option>';
                    }
                    $("#codeCategory").html(str);
                    
                }
            });
        }
        
        function showCodeSubCate(){
            $("#codeSubCate").prop("disabled","");//将设备子类的select解除锁定
            var target = $("#codeCategory  option:selected").text();
            $.ajax({
                url: "<%=request.getContextPath()%>/facilitydict/showCodeSubCategory",
                data : {codeCategory:target},
                async: false, //请求是否异步,默认为异步,这也是ajax重要特性
                type: "GET",   //请求方式
                success: function(result) {
                    result = $.parseJSON(result);
                    var data = result.data;
                    var codeCates = data.split(",");
                    var str="";
                    for(x in codeCates){
                        str+='<option value="'+codeCates[x]+'">'+codeCates[x]+'</option>';
                    }
                    $("#codeSubCate").html(str);
                }
            });
        }

    不难看出,当设备分类选择器内的内容发生改变后,触发showCodeSubCate函数来请求后台获取数据,然后把请求到的数据添加到设备子类对应的select内。后台代码的实现如下(只贴出controller的方法):

    @RequestMapping("/showCodeCategory")
        @ResponseBody
        public Result<String> searchCodeCategory() {
    
            Result<String> rs = new Result<>();
            List<String> codeCategorys = facilityDictService.searchCodeCategory();
            String codeCate = StringUtil.collectionToCommaDelimitedString(codeCategorys);
            rs.setData(codeCate);
            return rs;
    
        }
    
        @RequestMapping("/showCodeSubCategory")
        @ResponseBody
        public Result<String> searchCodeSubCategory(HttpServletRequest request) {
            String codeCategory = request.getParameter("codeCategory");
            Result<String> rs = new Result<>();
            List<String> codeSubCategorys = facilityDictService.searchCodeSubCategory(codeCategory);
            String codeCate = StringUtil.collectionToCommaDelimitedString(codeSubCategorys);
            rs.setData(codeCate);
            return rs;
        }

    这两个方法分别对应上面的两个ajax请求,值得介绍的是后台返回的数据,返回值类型为Result<String>,该返回值类型是一个工具类,具体实现可以在我的博客中查看,链接为:

    http://www.cnblogs.com/blog411032/p/5799669.html

    该分享如有不足之处,还请各位指出。

  • 相关阅读:
    Nginx模块fastcgi_cache的几个注意点
    Nginx的fastcgi_cache
    Nginx Location 语法,与简单配置[转]
    nginx location 匹配顺序
    Amoeba基本配置
    LVS的DR模式配置
    Keepalived安装及初步使用
    HAProxy安装及初步使用
    lvs nginx HAProxy优缺点
    Redis安装及初步使用
  • 原文地址:https://www.cnblogs.com/blog411032/p/5821149.html
Copyright © 2011-2022 走看看