json 的使用 关于多级下拉菜单的选择代码

1.dwr中的action

public String getInsByAppCode(String appCode) throws Exception{
  JSONArray obj ;
  if (appCode != null && !"".equals(appCode) && !"null".equalsIgnoreCase(appCode)) {
   //String xmlCode = fsAppvtreeDefService.getAppCodeById(appCode);
   boolean isIns = false;//appTypeService.isInsurancecode(xmlCode);
   if (!isIns) {
    obj= this.fsAppvtreeDefService.getAppVtreeJson(appCode);   2.的方法内容
   } else {
    obj = this.fsAppvtreeDefService.getInsJson(appCode);
   }
   return obj.toString() + "="+ isIns;
  }
  return "";
 }

 

 

 

2.。getAppVtreeJson(String insCode) 方法内容

public JSONArray getAppVtreeJson(String insCode) throws Exception {
  List resultList = getFsAppvtreeDef(insCode);
  JSONArray jsonarr = new JSONArray();
  JSONObject json = null;
  JSONArray jsonnewarr = new JSONArray();

  for (int i = 0; i < resultList.size(); i++) {
      FsAppvtreeDef fapp = (FsAppvtreeDef) resultList.get(i);
      if (fapp.getChildFlag() == 0) {
   json = new JSONObject();
   fapp = (FsAppvtreeDef) resultList.get(i);
   json.put("vtreeCode", fapp.getAppvtreeCode());
   json.put("vtreeName", fapp.getNodeName());
   json.put("parentCode", fapp.getParentNode());
   json.put("xmlCode", fapp.getXmlCode());
   jsonarr.put(json);

      } else {
       jsonnewarr = getAppVtreeJson(fapp.getAppvtreeCode() + "");
    for (int j = 0; j < jsonnewarr.length(); j++) {
        jsonarr.put(jsonnewarr.get(j));
    }
      }
  }
  return jsonarr;
     }

 

 

3.jsp中的将   下面的东东, //json字符串转化为对象

JSONArray jsonarr = new JSONArray(); 
  JSONObject json = null;

 

代码如下

 

  //参数传入一个业务类型代码
   changeType = function (appCode) {
    appCodeTemp = appCode;
    appType.getInsByAppCode(appCode,callbackType);
   }
   callbackType = function (data) {
    if (data != null && data != "") {
     var objType = null;
     var busiValue = null;
     var datas = data.split("=");

     var insType = document.getElementById("insType");
     var length = insType.options.length;
     //添加之前将原有的险种元素删除
     for (var i = length - 1; i >= 1; i--) {
      insType.options.remove(i);
     }
     //添加之前将原有的产品小类元素删除
     var prod = document.getElementById("prod");
     var length = prod.options.length;
     for (var i = length - 1; i >= 1; i--) {
       prod.options.remove(i);
     }
     var subType = document.getElementById("subType");
     var length = subType.options.length;
     //添加之前将原有的单证元素删除
     for (var i = length - 1; i >= 1; i--) {
       subType.options.remove(i);
     }
     var widgetIdType = document.getElementById("widgetId");
     var length = widgetIdType.options.length;
     //添加之前将原有的录入字段元素删除
     for (var i = length - 1; i >= 1; i--) {
      widgetIdType.options.remove(i);
     }
     if(datas[1] != "false") {
      document.getElementById("insTypeDiv").style.display = "inline";
      objType = insType;
      busiValue = "<%=insType%>";
      isInsType = true;
     } else {
      document.getElementById("insTypeDiv").style.display = "none";
      objType = subType;
      busiValue = "<%=subType%>";
      isInsType = false;
      changeProdType(appCodeTemp);
     }
     //json字符串转化为对象
     var myObject = eval("(" + datas[0] + ")");
     for (var i = 0; i < myObject.length; i++) {
            var nodeCode = myObject[i].vtreeCode;
      var nodeName = myObject[i].vtreeName;
      var option=new Option(nodeName,nodeCode);
      if(nodeCode==busiValue){
       option.selected=true;
      }
      objType.options.add(option);
     }
    }
   }
   //选择险种
   changeInsType = function (insCode) {
    setTimeout('loadMethod('+insCode+')',1);
   }
   function loadMethod(insCode) {
    appType.getAppvtreeByInsCode(insCode, callbackInsuranceType);
    appType.getProdByInsCode(insCode, callbackInsType);
   }
   callbackInsType = function (data) {
     if (data != null) {
      var prod = document.getElementById("prod");
      var length = prod.options.length;
      //添加之前将原有的产品小类元素删除
      for (var i = length - 1; i >= 1; i--) {
       prod.options.remove(i);
      }
      var prodValue='<%=prod%>';
      var myObject = eval("(" + data + ")");
      for (var i = 0; i < myObject.length; i++) {
       var prodCode = myObject[i].prod;
       var prodName = myObject[i].prodName;
       var option=new Option(prodName,prodCode);
       if(prodValue==prodCode){
        option.selected=true;
       }
        prod.options.add(option);
       }
         }
    }
   callbackInsuranceType = function (data) {
     if (data != null) {
     var subType = document.getElementById("subType");
      var length = subType.options.length;
     //添加之前将原有的单证元素删除
     for (var i = length - 1; i >= 1; i--) {
       subType.options.remove(i);
     }
     //json字符串转化为对象
     var myObject = eval("(" + data + ")");
     var subValue='<%=subType%>';
     for (var i = 0; i < myObject.length; i++) {
            var nodeCode = myObject[i].vtreeCode;
      var nodeName = myObject[i].vtreeName;
      var option=new Option(nodeName,nodeCode);
      if(subValue==nodeCode){
       option.selected=true;
      }
      subType.options.add(option);
     }
    }
   }
   //选择产品小类加载字段
   changeProdType=function(prod){
     var insCode=document.getElementById('insType').value;
     if(insCode==''||insCode==null){
      insCode='<%=insType%>';
     }
    appType.getWidgetByInsCode(insCode,prod,callbackProdType);
   }
    callbackProdType=function(data){
       if(data!=null){
        var widgetIdType = document.getElementById("widgetId");
        var length = widgetIdType.options.length;
        //添加之前将原有的录入字段元素删除
        for (var i = length - 1; i >= 1; i--) {
         widgetIdType.options.remove(i);
        }
        //json字符串转化为对象
        var widgetValue='<%=widgetId%>';
        var myObject = eval("(" + data + ")");
        for (var i = 0; i < myObject.length; i++) {
           var nodeCode = myObject[i].vtreeCode;
        var nodeName = myObject[i].vtreeName;
        var tt=new Option(nodeName,nodeCode);
          if(nodeCode==widgetValue){
         tt.selected=true;
          }
         widgetIdType.options.add(tt);
        }
       }
        }

 

 

你可能感兴趣的:(JSON,代码区)