Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. <p:growl id="msgs" showDetail="true" />
  2.  
  3. <p:panel header="Select a Location" style="margin-bottom:10px;">
  4. <h:panelGrid columns="2" cellpadding="5">
  5. <p:outputLabel for="country" value="Country: " />
  6. <p:selectOneMenu id="country" value="#{dropdownView.country}" style="width:150px">
  7. <p:ajax listener="#{dropdownView.onCountryChange}" update="city" />
  8. <f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
  9. <f:selectItems value="#{dropdownView.countries}" />
  10. </p:selectOneMenu>
  11.  
  12. <p:outputLabel for="city" value="City: " />
  13. <p:selectOneMenu id="city" value="#{dropdownView.city}" style="width:150px">
  14. <f:selectItem itemLabel="Select City" itemValue="" noSelectionOption="true" />
  15. <f:selectItems value="#{dropdownView.cities}" />
  16. </p:selectOneMenu>
  17. </h:panelGrid>
  18.  
  19. <p:separator />
  20.  
  21. <p:commandButton value="Submit" update="msgs" action="#{dropdownView.displayLocation}" icon="pi pi-check" />
  22. </p:panel>
  23. </h:form>```
  24.  
  25.  
  26.  
  27. dropdownView.java
  28.  
  29.  
  30. ```@ManagedBean
  31. @ViewScoped
  32. public class DropdownView implements Serializable {
  33.  
  34. private Map<String,Map<String,String>> data = new HashMap<String, Map<String,String>>();
  35. private String country;
  36. private String city;
  37. private Map<String,String> countries;
  38. private Map<String,String> cities;
  39.  
  40. @PostConstruct
  41. public void init() {
  42. countries = new HashMap<String, String>();
  43. countries.put("USA", "USA");
  44. countries.put("Germany", "Germany");
  45. countries.put("Brazil", "Brazil");
  46.  
  47. Map<String,String> map = new HashMap<String, String>();
  48. map.put("New York", "New York");
  49. map.put("San Francisco", "San Francisco");
  50. map.put("Denver", "Denver");
  51. data.put("USA", map);
  52.  
  53. map = new HashMap<String, String>();
  54. map.put("Berlin", "Berlin");
  55. map.put("Munich", "Munich");
  56. map.put("Frankfurt", "Frankfurt");
  57. data.put("Germany", map);
  58.  
  59. map = new HashMap<String, String>();
  60. map.put("Sao Paulo", "Sao Paulo");
  61. map.put("Rio de Janerio", "Rio de Janerio");
  62. map.put("Salvador", "Salvador");
  63. data.put("Brazil", map);
  64. }
  65.  
  66. public Map<String, Map<String, String>> getData() {
  67. return data;
  68. }
  69.  
  70. public String getCountry() {
  71. return country;
  72. }
  73.  
  74. public void setCountry(String country) {
  75. this.country = country;
  76. }
  77.  
  78. public String getCity() {
  79. return city;
  80. }
  81.  
  82. public void setCity(String city) {
  83. this.city = city;
  84. }
  85.  
  86. public Map<String, String> getCountries() {
  87. return countries;
  88. }
  89.  
  90. public Map<String, String> getCities() {
  91. return cities;
  92. }
  93.  
  94. public void onCountryChange() {
  95. if(country !=null && !country.equals(""))
  96. cities = data.get(country);
  97. else
  98. cities = new HashMap<String, String>();
  99. }
  100.  
  101. public void displayLocation() {
  102. FacesMessage msg;
  103. if(city != null && country != null)
  104. msg = new FacesMessage("Selected", city + " of " + country);
  105. else
  106. msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid", "City is not selected.");
  107.  
  108. FacesContext.getCurrentInstance().addMessage(null, msg);
  109. }
  110. }```
  111.  
  112.  
  113. Expected is on selecting any country, city should load into city dropdown.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement