Ajax 下拉框聯動

 頁面JShtml

  
  
           
  
  
  1. function ajaxClient(_id,_type){ 
  2.                         $.ajax({ 
  3.                                    type: "POST", 
  4.                                    url:"${pageContext.request.contextPath}/projectManage/project!queryClientSort.action", 
  5.                                   dataType: "json", 
  6.                                    data: "ids=" + _id + "&type=" + _type, 
  7.                                     success: function(data){ 
  8.                                           var json = eval(data); //eval("(" + data + ")"); 
  9.                                          
  10.                                             if(_type==1){ 
  11.                                                       
  12.                                                      $("#clientBasic").html('<option value="-1">請選擇客戶名稱</option>'); 
  13.                                                     // $("#contract").html('<option value="-1"> 請選擇合同編號</option>'); 
  14.                                              } 
  15.                                             else if(_type==2){ 
  16.                                                  $("#clientBasic").html('<option value="-1">請選擇客戶名稱</option>'); 
  17.                                                  //$("#contract").html('<option value="-1"> 請選擇合同編號</option>'); 
  18.                                             }else if(_type=3){ 
  19.                                                 $("#contract").html('<option value="-1"> 請選擇合同編號</option>'); 
  20.                                             } 
  21.                                               $.each(json, function(idx, item) { 
  22.                                              //   var user = item.UserName; 
  23.                                            //     var pass = item.PWD; 
  24.                                      
  25.                                                 if(_type==1){ 
  26.                                                       //$("#clientSort").append('<option value="'  + item.stId + '"> ' + item.name + '</option>'); 
  27.                                                 }    
  28.                                                 else if(_type==2){ 
  29.                                                  $("#clientBasic").append('<option value="'  + item.clientId + '"> ' + item.clientName + '</option>'); 
  30.                                                 }else if(_type==3){ 
  31.                                                     $("#contract").append('<option value="'  + item.contractId + '"> ' + item.contractNumber + '</option>'); 
  32.                                                 } 
  33.                                             }); 
  34.                                    } 
  35.                                 }); 
  36.             } 

 

  
  
           
  
  
  1.    
  2. //頁面元素的驗證    
  3. $(function(){ 
  4. /** 
  5. *Ajax獲取數據  start 
  6. */ 
  7.  
  8.                $('#clientSort').change(function() { 
  9.                     if($('#clientSort').val()>0){ 
  10.                         ajaxClient($('#clientSort').val(),2); 
  11.                     }else{ 
  12.                         $('#clientBasic :not(option:eq(0))').remove(); 
  13.                     } 
  14.                 }); 
  15.                 $('#clientBasic').change(function() { 
  16.                     if($('#clientBasic').val()>0){ 
  17.                         ajaxClient($('#clientBasic').val(),3); 
  18.                     }else{ 
  19.                         $('#contract :not(option:eq(0))').remove(); 
  20.                     } 
  21.                 }); 
  22.                  
  23.                 $('#list_table tr:even').addClass('even_list'); 
  24. /** 
  25. *Ajax獲取數據  stop 
  26. */ 
  27. }) 

ProjectActionajax

 

  
  
           
  
  
  1. /** 
  2.      * ajax得到數據 
  3.      *  
  4.      * @return 
  5.      * @throws Exception 
  6.      */ 
  7.  
  8.     public String queryClientSort() throws Exception { 
  9.  
  10.         JsonConfig cfg = new JsonConfig(); 
  11.         cfg.setJsonPropertyFilter(new PropertyFilter() { 
  12.             //取消這些屬性封裝成json 
  13.             public boolean apply(Object source, String name, Object value) { 
  14.                 if (name.equals("staticType") ||name.equals("agencyCompany") || name.equals("contracts") 
  15.                         || name.equals("users") || name.equals("projects") || name.equals("clientBasic")) { 
  16.                     return true; 
  17.                 } else { 
  18.                     return false; 
  19.                 } 
  20.             } 
  21.         }); 
  22.  
  23.         JSONArray jsonArray = null
  24.         if (type != null && "1".equals(type)) { 
  25.  
  26.             clientSortList=clientSortSTService.queryAll(); 
  27.             if(clientSortList != null) 
  28.             { 
  29.                 jsonArray = JSONArray.fromObject(clientSortList, cfg); 
  30.             } 
  31.         } else if (type != null && "2".equals(type)) { 
  32.             //查詢客戶分類ID下的客戶集合 
  33.             clientBasicList=clientBasicService.queryClientBasic_ClientSortId(Integer.valueOf(ids)); 
  34.             System.out.println("客戶集合大小:"+clientBasicList.size()); 
  35.             if(clientBasicList != null) 
  36.             { 
  37.                 jsonArray = JSONArray.fromObject(clientBasicList, cfg); 
  38.             } 
  39.      
  40.         }else if(type!=null && "3".equals(type)){ 
  41.             //查詢客戶下的合同信息集合 
  42.             contractList=clientBasicService.queryContract(Integer.valueOf(ids)); 
  43.             if(contractList !=null){ 
  44.                 jsonArray = JSONArray.fromObject(contractList, cfg); 
  45.             } 
  46.         } 
  47.  
  48.         // JSONArray json = 
  49.         // JSONArray.fromObject(topicService.getClientBasic(1)); 
  50.         if (jsonArray != null) { 
  51.             PrintWriter pt = null
  52.             try { 
  53.                 ActionContext ctx = ActionContext.getContext(); 
  54.                 HttpServletResponse response = (HttpServletResponse) ctx 
  55.                         .get(ServletActionContext.HTTP_RESPONSE); 
  56.                 response.setContentType("text/html; charset=UTF-8"); 
  57.                 pt = response.getWriter(); 
  58.                 pt.write(jsonArray.toString()); 
  59.                 pt.flush(); 
  60.             } catch (IOException e) { 
  61.                 e.printStackTrace(); 
  62.             } 
  63.         } 
  64.         return null; 
  65.     } 
相關文章
相關標籤/搜索