From 6cd58c7e765b19e2fc64c027fbe908aa02bf1b1e Mon Sep 17 00:00:00 2001
From: Bruno Nocera Zanette <brunonzanette@gmail.com>
Date: Fri, 23 May 2014 18:07:12 -0300
Subject: [PATCH] Update Municipio option's list based on the Estado

Update Municipio option's list based on the Estado chosen.

Steps:
1) Client sends a request to the server using estado as parameter.
2) Server receives the request, generates the list based on the Estado
   and sends as response a complete view/page ("orgaoexecutores/create")
   containing that list
3) Client receive the response, processes it to get only the list,
   and exchanges the list of the page for the response list.
---
 .../web/OrgaoExecutorController.java          | 21 +++++++++----------
 .../WEB-INF/views/orgaoexecutores/create.jspx | 21 ++++++++++++-------
 2 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/src/main/java/br/ufpr/c3sl/estacaojuventude/web/OrgaoExecutorController.java b/src/main/java/br/ufpr/c3sl/estacaojuventude/web/OrgaoExecutorController.java
index 0c86f99..4244764 100644
--- a/src/main/java/br/ufpr/c3sl/estacaojuventude/web/OrgaoExecutorController.java
+++ b/src/main/java/br/ufpr/c3sl/estacaojuventude/web/OrgaoExecutorController.java
@@ -64,16 +64,15 @@ public class OrgaoExecutorController {
         return "redirect:/orgaoexecutores";
     }
 	
-	@RequestMapping(method = RequestMethod.GET, produces = "text/html")
-	public String get(@RequestParam(value = "uf") String uf, @Valid OrgaoExecutor orgaoExecutor, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
-		System.out.println("GET_MUNICIPIO COM A ID: "+uf);
+	@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "text/html")
+	public String get(@PathVariable(value = "id") String uf, @Valid OrgaoExecutor orgaoExecutor, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
 		repopulateMunicipios(uf, uiModel, orgaoExecutor);
 		return "orgaoexecutores/create";
 	}
 	
 	void repopulateMunicipios(String uf, Model uiModel, OrgaoExecutor orgaoExecutor) {
-		uiModel.addAttribute("municipios", Municipio.findAllMunicipiosByUf(uf));
-		System.out.println("__AKI__");
+		List<Municipio> lm = Municipio.findAllMunicipiosByUf(uf);
+        uiModel.addAttribute("municipios", lm);
 	}
 	
 	void populateEditForm(Model uiModel, OrgaoExecutor orgaoExecutor) {
@@ -86,12 +85,12 @@ public class OrgaoExecutorController {
         le.add(0, e);
         uiModel.addAttribute("estadoes", le);
         
-        List<Municipio> lm = Municipio.findAllMunicipios();
-        Municipio m = new Municipio();
-        m.setId((long) 0);
-        m.setNome("");
-        lm.add(0, m);
-        uiModel.addAttribute("municipios", lm);
+//	        List<Municipio> lm = Municipio.findAllMunicipios();
+//	        Municipio m = new Municipio();
+//	        m.setId((long) 0);
+//	        m.setNome("");
+//	        lm.add(0, m);
+//	        uiModel.addAttribute("municipios", lm);
     }
 	
 }
diff --git a/src/main/webapp/WEB-INF/views/orgaoexecutores/create.jspx b/src/main/webapp/WEB-INF/views/orgaoexecutores/create.jspx
index b7b1bce..980cd2b 100644
--- a/src/main/webapp/WEB-INF/views/orgaoexecutores/create.jspx
+++ b/src/main/webapp/WEB-INF/views/orgaoexecutores/create.jspx
@@ -33,22 +33,29 @@
 			widgetType : "dijit.form.FilteringSelect",
 			widgetAttrs : {
 				onChange : function() {
+
+					//ID of the chosen "estado"
  					uf=document.getElementsByName("uf")[0].value;
- 					console.log("REPOPULANDO A PARTIR DE UF(ID) = "+uf);
- 					
-// 					var element = document.getElementsByName("_municipio_id_popup");
-// 					console.log(element);
 					
+					//Create and send to server a request of 
+					//the list of "Municipios" that belongs to the "Estado" chosen 
 					xmlhttp = new XMLHttpRequest();
-					xmlhttp.open("GET", "orgaoexecutores?uf="+uf ,true);
+					xmlhttp.open("GET", "orgaoexecutores/"+uf ,false);
 					xmlhttp.send();
+
+					//Create a temporary html document to store the html code
+					//sent by the server containing the list of "Municipios"
+					var responseDoc = document.implementation.createHTMLDocument("XMLHttpResponse");
+					responseDoc.documentElement.innerHTML = xmlhttp.responseText;
 					
-					
+					//Copy the list of the response to the real page 
+					var PageElem = document.getElementById("_c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_municipio_id");
+					var RespElem = responseDoc.getElementById("_c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_municipio_id");
+					PageElem.innerHTML = RespElem.innerHTML;
 				}
 			}
 		}));
 
-
  	</script>
  	
 </div>
-- 
GitLab