Ajax - URGENTE

1 resposta
L

Ola pessoal…

Estou fazendo uma página que retorna duas tabelas…departamentos e empregados…existe um relacionamento entre essas tabelas que eh pelo numero do departamento… o que eu quero eh que quando o usuario selecione um departamento a tabela de funcionarios seja atualizada…

Por favor me ajudem…eu fiz um eskema parecido só que sem ajax…e tbm naum deu certo…vou postar em outro topico…muito obrigado…

Olhem o codigo:

<%@ page contentType=text/html;charset=windows-1252%>

<%@ taglib uri=<a href="http://java.sun.com/jsf/html">http://java.sun.com/jsf/html</a>” prefix=“h”%>

<%@ taglib uri=<a href="http://java.sun.com/jsf/core">http://java.sun.com/jsf/core</a>” prefix=“f”%>

<%@ page import=conexao.Conexao%>

<%@ page import=java.sql.*%>
Consulta function getFuncinario( idDepto ){ getFuncAjax( idDepto ); //window.location='consulta.jsp?idDepto=' + idDepto; } function getFuncAjax( idDepto ){ url = "consultaAjax.jsp?idDepto=" + idDepto; // code for Mozilla, etc. if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest() xmlhttp.onreadystatechange=xmlhttpChange xmlhttp.open("POST",url,true) xmlhttp.send(null) } // code for IE else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") if (xmlhttp) { xmlhttp.onreadystatechange=xmlhttpChange xmlhttp.open("POST",url,true) xmlhttp.send() } } }
function xmlhttpChange()
	{
		if(xmlHttp.readyState == 4){
			if (xmlHttp.status == 200){
				if(xmlHttp.responseXML.getElementsByTagName("idDepto").length > 0){
					var string = "<table  cellspacing=\"0\" border=\"1\" width=\"700\" align=\"center\">" +
						"<caption>FUNCIONARIOS</caption>												" + 
						"	<tr>																		" +
						"		<th scope=\"col\">SELECIONAR</th>										" + 
						"		<th scope=\"col\">MATRICULA</th>										" +
						"		<th scope=\"col\">NOME</th>												" + 
						"		<th scope=\"col\">SOBRENOME</th>										" +
						"		<th scope=\"col\">EMAIL</th>											" +
						"		<th scope=\"col\">TELEFONE</th>											" +
						"		<th scope=\"col\">SALARIO</th>											" +
						"		<th scope=\"col\">IDENTIIFCAÇÃO DO GERENTE</th>							" +
						"		<th scope=\"col\">DEPTO Nº</th>											" +
						"	</tr>																		" +
						"	<tr class='alt''>															";
						var count = xmlHttp.responseXML.getElementsByTagName("id").length;
						for( i=0; i>count; i++ ) {
							var id			= xmlHttp.responseXML.getElementsByTagName("id")[i].firstChild.nodeValue;
							var nome		= xmlHttp.responseXML.getElementsByTagName("nome")[i].firstChild.nodeValue; 			     
							var sobrenome	= xmlHttp.responseXML.getElementsByTagName("sobrenome")[i].firstChild.nodeValue;
							var email		= xmlHttp.responseXML.getElementsByTagName("email")[i].firstChild.nodeValue;
							var telefone	= xmlHttp.responseXML.getElementsByTagName("telefone")[i].firstChild.nodeValue;
							var salario		= xmlHttp.responseXML.getElementsByTagName("telefone")[i].firstChild.nodeValue;
							var gerente		= xmlHttp.responseXML.getElementsByTagName("gerente")[i].firstChild.nodeValue;
							var	dept_id		= xmlHttp.responseXML.getElementsByTagName("dept_id")[i].firstChild.nodeValue;
							string +=	"<td width='8%' align='center' border='1'> <input type='radio' name='idDepto' value='" + id + "' onclick='getFuncinario( this.value );' >  </td></font> " +
										"<td width='12%'align='center' border='1''>"	+ id		+ "</td></font>		" +
										"<td width='12%'align='center' border='1''>"	+ nome		+ "</td></font>		" +
										"<td width='10%'align='center' border='1''> " + sobrenome	+ "</td></font>		" +
										"<td width='12%'align='center' border='1''>"	+ email		+ "</td></font>    	" +
										"<td width='12%'align='center' border='1''>"	+ telefone	+ "</td></font> 	" +
										"<td width='12%'align='center' border='1''>"	+ salario	+ "</td></font> 	" +
										"<td width='12%'align='center' border='1''>"	+ gerente	+ "</td></font>  	" +
										"<td width='12%'align='center' border='1''>"	+ dept_id	+ "</td></font>  	";
						}
						string +=	"</tr>		" +
									"</table>	";
						document.getElementById( "tbFuncionario" ).innerHTML = string;
				}
			}
			else alert("Erro ao carregar a ajax");
		}
	}
  </script>
</head>
<body>

<table  cellspacing="0" border="1" width="700" align="center">
				
					<caption>DEPARTAMENTOS</caption>
					
					<tr>
                                                    <th scope="col">SELECIONAR</th>
						<th scope="col">DEPTO </th>
						<th scope="col">DEPARTAMENTO</th>
						<th scope="col">INDETIFICAÇÃO DO GERENTE</th>
						
                                                    
                                                  
					</tr>
                                          
 
				                                          
                                            
					
                                            <%

int contador = 0;
int quantidade = 0;

Conexao conexao = new Conexao();
String cod;
String query  = "select * from departamentos WHERE dept_id is not null ";
if( request.getParameter("idDepto") != null )
	query += " and dept_id = " + request.getParameter("idDepto");
query += " order by id";
ResultSet rs = conexao.execSelect( query );
rs.next();


    while (contador <= quantidade && (!rs.isAfterLast())) {

out.println("<tr class=‘alt’’>

</font)");
out.println(" " + rs.getObject(“dept_id”) + “</font)”);
out.println("<td width='12%‘align=‘center’ border=‘1’’>" + rs.getObject(“dept_name”) + “</font)”);
out.println("<td width='10%‘align=‘center’ border=‘1’’> " + rs.getObject(“dept_mgr”) + “</font)”);

rs.next();

}

%>


FUNCIONARIOS
FUNCIONARIOS <% int contadorr = 0; int quantidadee = 0; Conexao conexaoo = new Conexao(); String codd; String query2 = "select * from funcionarios WHERE dept_id is not null "; if( request.getParameter("idDepto") != null ) query2 += " and dept_id = " + request.getParameter("idDepto"); query2 += " order by id"; ResultSet rss = conexaoo.execSelect( query2 ); rss.next(); while (contadorr <= quantidadee && (!rss.isAfterLast())) { out.println("</font)"); out.println("</font)"); out.println("</font)"); out.println("</font)"); out.println("</font)"); out.println("</font)"); out.println("</font)"); out.println("</font)"); out.println("
SELECIONAR MATRICULA NOME SOBRENOME EMAIL TELEFONE SALARIO IDENTIIFCAÇÃO DO GERENTE DEPTO Nº
" + rss.getObject("id") + "" + rss.getObject("nome") + " " + rss.getObject("sobrenome") + "" + rss.getObject("email") + "" + rss.getObject("telefone") + "" + rss.getObject("salario") + "" + rss.getObject("gerente") + "" + rss.getObject("dept_id") + "

1 Resposta

Pedrosa

Cara procure por soluções que possam te ajudar com produtividade e simplicidade:

VRaptor:
http://www.vraptor.org/pt/ajax.html

Prototype
http://www.prototypejs.org/

Criado 15 de maio de 2007
Ultima resposta 15 de mai. de 2007
Respostas 1
Participantes 2