2017-01-17 18 views
3

これは私の最初の投稿であり、あまりにも多くの間違いを犯さないことを願っています。Liferay - json responseDataで埋め尽くされた項目を選択

Javaリストに入力してauiに送信する際に問題がありました:オプションとして選択してください。私の目標はauiを記入することです:オプションの1つが変更されたときは必ずを選択してください。たとえば、最初に項目(郡)を選択すると、2番目と3番目の項目(それぞれコミュニティと都市)が空になり、選択された郡に依存するデータが入力されます。

クエリ文字列パラメータに"mvcPath"パラメータがある場合、このコードは基本的にページ全体のコードをコピーするという結論に達しました。このコードは、"mvcPath"がある場合でもうまく機能します。残念ながら、(検索結果から選択した結果の詳細に)ページを変更するには、このパラメータが必要です。

Image showing badly filled select item

Image showing correctly filled select item

Javaコード:

@Override 
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) 
     throws IOException, PortletException { 
    String communityName = ""; 
    long communityId = 0; 
    String cityName = ""; 
    long cityId = 0; 
    String countySelected = ParamUtil.getString(resourceRequest, "countySelected"); 
    long countySelectedId = ParamUtil.getLong(resourceRequest, "countyDictionaryId"); 
    String communitySelected = ParamUtil.getString(resourceRequest, "communitySelected"); 
    long communitySelectedId = ParamUtil.getLong(resourceRequest, "communityDictionaryId"); 
    JSONArray jsonArray = JSONFactoryUtil.createJSONArray(); 
    if (countySelected.equalsIgnoreCase("countySelected") && countySelectedId != 0) { 
     System.out.println("County id: " + countySelectedId); 
     try { 
      int communitiesCount = CommunityDictionaryLocalServiceUtil.getCommunityDictionariesCount(); 
      List<CommunityDictionary> communities = CommunityDictionaryLocalServiceUtil.getCommunityDictionaries(0, 
        communitiesCount); 
      for (CommunityDictionary community : communities) { 
       if (community.getCountyDictionaryId() == countySelectedId) { 
        communityName = community.getCommunityName(); 
        communityId = community.getCommunityDictionaryId(); 
        JSONObject communityObject = JSONFactoryUtil.createJSONObject(); 
        communityObject.put("communityName", communityName); 
        communityObject.put("communityDictionaryId", communityId); 
        jsonArray.put(communityObject); 
        System.out.print(jsonArray.toString()); 
       } 
      } 
     } catch (SystemException e) { 
      e.printStackTrace(); 
     } 
    } else if (countySelected.equalsIgnoreCase("countySelected") && countySelectedId == 0) { 
     System.out.println("No county chosen."); 
     try { 
      int communitiesCount = CommunityDictionaryLocalServiceUtil.getCommunityDictionariesCount(); 
      List<CommunityDictionary> communities = CommunityDictionaryLocalServiceUtil.getCommunityDictionaries(0, 
        communitiesCount); 
      for (CommunityDictionary community : communities) { 
       communityName = community.getCommunityName(); 
       communityId = community.getCommunityDictionaryId(); 
       JSONObject communityObject = JSONFactoryUtil.createJSONObject(); 
       communityObject.put("communityName", communityName); 
       communityObject.put("communityDictionaryId", communityId); 
       jsonArray.put(communityObject); 
      } 
     } catch (SystemException e) { 
      e.printStackTrace(); 
     } 
    } 
    if (communitySelected.equalsIgnoreCase("communitySelected") && communitySelectedId != 0) { 
     System.out.println("Community id: " + communitySelectedId); 
     try { 
      int citiesCount = CityDictionaryLocalServiceUtil.getCityDictionariesCount(); 
      List<CityDictionary> cities = CityDictionaryLocalServiceUtil.getCityDictionaries(0, citiesCount); 
      for (CityDictionary city : cities) { 
       if (city.getCommunityDictionaryId() == communitySelectedId) { 
        cityName = city.getCityName(); 
        cityId = city.getCityDictionaryId(); 
        JSONObject cityObject = JSONFactoryUtil.createJSONObject(); 
        cityObject.put("cityName", cityName); 
        cityObject.put("cityDictionaryId", cityId); 
        jsonArray.put(cityObject); 
        System.out.print(jsonArray.toString()); 
       } 
      } 
     } catch (SystemException e) { 
      e.printStackTrace(); 
     } 
    } else if (communitySelected.equalsIgnoreCase("communitySelected") && communitySelectedId == 0) { 
     System.out.println("No community chosen."); 
     try { 
      int citiesCount = CityDictionaryLocalServiceUtil.getCityDictionariesCount(); 
      List<CityDictionary> cities = CityDictionaryLocalServiceUtil.getCityDictionaries(0, citiesCount); 
      for (CityDictionary city : cities) { 
       cityName = city.getCityName(); 
       cityId = city.getCityDictionaryId(); 
       JSONObject cityObject = JSONFactoryUtil.createJSONObject(); 
       cityObject.put("cityName", cityName); 
       cityObject.put("cityDictionaryId", cityId); 
       jsonArray.put(cityObject); 
      } 
     } catch (SystemException e) { 
      e.printStackTrace(); 
     } 
    } 
    PrintWriter writer = resourceResponse.getWriter(); 
    writer.write(jsonArray.toString()); 
    writer.flush(); 
    super.serveResource(resourceRequest, resourceResponse); 
} 

スクリプト:後

<aui:script> 
AUI().use('aui-base', 'aui-io-request', 'aui-node', 
     function(A) {A.one("#<portlet:namespace />countySelect").on('change', function() { 
      A.io.request('<%= selectionChangedURL %>', 
        { 
       method : 'POST', 
       data : { 
        "<portlet:namespace />countyDictionaryId" : A.one("#<portlet:namespace />countySelect").val(), 
        '<portlet:namespace />countySelected' : 'countySelected' 
        }, 
        dataType : 'json', 
        on : { 
         success : function() { 
          var communitiesList = this.get('responseData'); 
          A.one('#<portlet:namespace />communitySelect').empty(); 
          A.one('#<portlet:namespace />citySelect').empty(); 
          A.one('#<portlet:namespace />communitySelect').prepend("<option value='0'> </option>"); 
          for (var i in communitiesList) { 
           console.info(communitiesList[i]); 
           A.one('#<portlet:namespace />communitySelect').append("<option value='" + communitiesList[i].communityDictionaryId + "'>" + communitiesList[i].communityName + "</option>"); 
           } 
          } 
        } 
        }); 
      }); 
     A.one("#<portlet:namespace />communitySelect").on('change', function() { 
      A.io.request('<%= selectionChangedURL %>', 
        { 
       method : 'POST', 
       data : { 
        "<portlet:namespace />communityDictionaryId" : A.one("#<portlet:namespace />communitySelect").val(), 
        '<portlet:namespace />communitySelected' : 'communitySelected' 
        }, 
        dataType : 'json', 
        on : { 
         success : function() { 
          var citiesList = this.get('responseData'); 
          A.one('#<portlet:namespace />citySelect').empty(); 
          A.one('#<portlet:namespace />citySelect').prepend("<option value='0'> </option>"); 
          for (var i in citiesList) { 
           console.info(citiesList[i]); 
           A.one('#<portlet:namespace />citySelect').append("<option value='" + citiesList[i].cityDictionaryId + "'>" + citiesList[i].cityName + "</option>"); 
           } 
          } 
        } 
        }); 
      }); 
     }); 

+0

は私に混乱に見えますが、私は、配列の一つの要素だとそれがカウンタとして使用思わ...あなたはに印刷された適切な情報を得ていますブラウザコンソール? –

+0

応答ありがとう! 2番目の添付写真では、正しく印刷された情報が表示されます。 mvcPathを使用しない限り、意図したとおりに動作します。 – Agred

答えて

0

サーバー側のコードのいくつかの変更私はこの問題を解決するために管理しました。私は本当に助けられた人たちの小さな変化がどういうものだったのかは分かりませんが、このスレッドを閉じるのはうれしいです。

これは、サーバー側のコードです:javascriptのコード内のforループ

public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) 
     throws IOException, PortletException { 
    String communityName = ""; 
    long communityId = 0; 
    String cityName = ""; 
    long cityId = 0; 
    String countySelected = ParamUtil.getString(resourceRequest, "countySelected"); 
    long countySelectedId = ParamUtil.getLong(resourceRequest, "countyDictionaryId"); 
    String communitySelected = ParamUtil.getString(resourceRequest, "communitySelected"); 
    long communitySelectedId = ParamUtil.getLong(resourceRequest, "communityDictionaryId"); 
    JSONArray jsonArray = JSONFactoryUtil.createJSONArray(); 
    if (countySelected.equalsIgnoreCase("countySelected")) { 
     try { 
      int communitiesCount = CommunityDictionaryLocalServiceUtil.getCommunityDictionariesCount(); 
      List<CommunityDictionary> communities = CommunityDictionaryLocalServiceUtil.getCommunityDictionaries(0, 
        communitiesCount); 
      if (countySelectedId == 0) { 
       for (CommunityDictionary community : communities) { 
        communityName = community.getCommunityName(); 
        communityId = community.getCommunityDictionaryId(); 
        JSONObject communityObject = JSONFactoryUtil.createJSONObject(); 
        communityObject.put("communityName", communityName); 
        communityObject.put("communityDictionaryId", communityId); 
        jsonArray.put(communityObject); 
       } 
      } else { 
       for (CommunityDictionary community : communities) { 
        if (community.getCountyDictionaryId() == countySelectedId) { 
         communityName = community.getCommunityName(); 
         communityId = community.getCommunityDictionaryId(); 
         JSONObject communityObject = JSONFactoryUtil.createJSONObject(); 
         communityObject.put("communityName", communityName); 
         communityObject.put("communityDictionaryId", communityId); 
         jsonArray.put(communityObject); 
        } 
       } 
      } 
     } catch (SystemException e) { 
      e.printStackTrace(); 
     } 
    } 
    if (communitySelected.equalsIgnoreCase("communitySelected")) { 
     try { 
      int citiesCount = CityDictionaryLocalServiceUtil.getCityDictionariesCount(); 
      List<CityDictionary> cities = CityDictionaryLocalServiceUtil.getCityDictionaries(0, citiesCount); 
      if (communitySelectedId == 0) { 
       for (CityDictionary city : cities) { 
        cityName = city.getCityName(); 
        cityId = city.getCityDictionaryId(); 
        JSONObject cityObject = JSONFactoryUtil.createJSONObject(); 
        cityObject.put("cityName", cityName); 
        cityObject.put("cityDictionaryId", cityId); 
        jsonArray.put(cityObject); 
       } 
      } else { 
       for (CityDictionary city : cities) { 
        if (city.getCommunityDictionaryId() == communitySelectedId) { 
         cityName = city.getCityName(); 
         cityId = city.getCityDictionaryId(); 
         JSONObject cityObject = JSONFactoryUtil.createJSONObject(); 
         cityObject.put("cityName", cityName); 
         cityObject.put("cityDictionaryId", cityId); 
         jsonArray.put(cityObject); 
        } 
       } 
      } 
     } catch (SystemException e) { 
      e.printStackTrace(); 
     } 
    } 
    PrintWriter writer = new PrintWriter(resourceResponse.getPortletOutputStream()); 
    writer.write(jsonArray.toString()); 
    writer.flush(); 
    super.serveResource(resourceRequest, resourceResponse); 
} 
関連する問題