• 下拉菜单的级联操作


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

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

    JSP简短页面:

     1         <div class="form-group">
     2                 <label class="col-sm-2 control-label">设备类别</label>
     3                 <div class="col-sm-4">
     4                     <select class="basic-single" name="codeCategory" onchange="showCodeSubCate()" id="codeCategory" style=" 100%">
     5                     
     6                     </select>
     7                 </div>
     8                 <label class="col-sm-2 control-label">设备子类</label>
     9                 <div class="col-sm-4">
    10                     <select class="basic-single" name="codeSubCategory"  id="codeSubCate" disabled="disabled" style=" 100%">
    11                         <option value="">--请选择--</option>
    12                     </select>
    13                 </div>
    14             </div>

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

    下面来看ajax代码段:

     1 function addCodeCategory(){
     2         $.ajax({
     3             url: "<%=request.getContextPath()%>/facilitydict/showCodeCategory",
     4             async: false, //请求是否异步,默认为异步,这也是ajax重要特性
     5             type: "GET",   //请求方式
     6             success: function(result) {
     7                 result = $.parseJSON(result);
     8                 var data = result.data;
     9                 var codeCates = data.split(",");
    10                 str ='<option value="6801">--请选择--</option>';
    11                 for(x in codeCates){
    12                     str+='<option value="'+codeCates[x]+'">'+codeCates[x]+'</option>';
    13                 }
    14                 $("#codeCategory").html(str);
    15                 
    16             }
    17         });
    18     }
    19     
    20     function showCodeSubCate(){
    21         $("#codeSubCate").prop("disabled","");//将设备子类的select解除锁定
    22         var target = $("#codeCategory  option:selected").text();
    23         $.ajax({
    24             url: "<%=request.getContextPath()%>/facilitydict/showCodeSubCategory",
    25             data : {codeCategory:target},
    26             async: false, //请求是否异步,默认为异步,这也是ajax重要特性
    27             type: "GET",   //请求方式
    28             success: function(result) {
    29                 result = $.parseJSON(result);
    30                 var data = result.data;
    31                 var codeCates = data.split(",");
    32                 var str="";
    33                 for(x in codeCates){
    34                     str+='<option value="'+codeCates[x]+'">'+codeCates[x]+'</option>';
    35                 }
    36                 $("#codeSubCate").html(str);
    37             }
    38         });
    39     }

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

     1 @RequestMapping("/showCodeCategory")
     2     @ResponseBody
     3     public Result<String> searchCodeCategory() {
     4 
     5         Result<String> rs = new Result<>();
     6         List<String> codeCategorys = facilityDictService.searchCodeCategory();
     7         String codeCate = StringUtil.collectionToCommaDelimitedString(codeCategorys);
     8         rs.setData(codeCate);
     9         return rs;
    10 
    11     }
    12 
    13     @RequestMapping("/showCodeSubCategory")
    14     @ResponseBody
    15     public Result<String> searchCodeSubCategory(HttpServletRequest request) {
    16         String codeCategory = request.getParameter("codeCategory");
    17         Result<String> rs = new Result<>();
    18         List<String> codeSubCategorys = facilityDictService.searchCodeSubCategory(codeCategory);
    19         String codeCate = StringUtil.collectionToCommaDelimitedString(codeSubCategorys);
    20         rs.setData(codeCate);
    21         return rs;
    22     }

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

    http://www.cnblogs.com/blog411032/p/5799669.html
  • 相关阅读:
    vue-cli(vue脚手架)简单流程
    windows环境之node.js安装与环境配置
    fiddler的下载和简单使用
    Linux 配置nginx 代理tomcat,配置ssl
    来聊一聊导出数据问题
    作为一个开发人员应该具备怎么样技术栈和职业素养
    NODEJS的误打误撞
    聊一下程序员的日常
    openstack安装部署私有云详细图文
    openstack:OpenStack架构详解,
  • 原文地址:https://www.cnblogs.com/sho560/p/5821263.html
Copyright © 2020-2023  润新知