Bom dia a todos!
Estou trabalhando com um projeto gerado pelo JBoss Seam.
Tenho que fazer um combobox ao selecionar um item, ele mostra abaixo do combobox uma tabela com as informações do item. Segue a figura para ilustrar:
[Resolvido]<rich:comboBox> com <rich:dataTable>
7 Respostas
Por favor michel posta o código para dar uma olhada para saber o que vc faz ao trocar um item do combo!
Abraços!
Opa, segue a Classe Projeto:
package br.com.xxx.projeto.entity;
// Generated 03/10/2008 08:34:40 by Hibernate Tools 3.2.2.GA
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
/**
* Projeto generated by hbm2java
*/
@Entity
@Table(name = "projeto", schema = "public")
public class Projeto implements java.io.Serializable {
private static final long serialVersionUID = 5883392836664107996L;
private int idProjeto;
private String nome;
private Date dataInicio;
private Date dataFim;
private Set<Atividade> atividades = new HashSet<Atividade>(0);
public Projeto() {
}
public Projeto(int idProjeto) {
this.idProjeto = idProjeto;
}
public Projeto(int idProjeto, String nome, Date dataInicio, Date dataFim,
Set<Atividade> atividades) {
this.idProjeto = idProjeto;
this.nome = nome;
this.dataInicio = dataInicio;
this.dataFim = dataFim;
this.atividades = atividades;
}
@Id
@Column(name = "id_projeto", unique = true, nullable = false)
@NotNull
public int getIdProjeto() {
return this.idProjeto;
}
public void setIdProjeto(int idProjeto) {
this.idProjeto = idProjeto;
}
@Column(name = "nome", length = 45)
@Length(max = 45)
public String getNome() {
return this.nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Temporal(TemporalType.DATE)
@Column(name = "data_inicio", length = 13)
public Date getDataInicio() {
return this.dataInicio;
}
public void setDataInicio(Date dataInicio) {
this.dataInicio = dataInicio;
}
@Temporal(TemporalType.DATE)
@Column(name = "data_fim", length = 13)
public Date getDataFim() {
return this.dataFim;
}
public void setDataFim(Date dataFim) {
this.dataFim = dataFim;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "projeto")
public Set<Atividade> getAtividades() {
return this.atividades;
}
public void setAtividades(Set<Atividade> atividades) {
this.atividades = atividades;
}
@Override
public String toString() {
// Integer result1 = idProjeto;
String result2 = nome;
// String result = result1 + " " + result2;
return result2;
}
}
A Classe ProjetoList:
package br.com.xxx.projeto;
import br.com.liveware.dotproject.entity.*;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityQuery;
import java.util.List;
import java.util.Arrays;
@Name("projetoList")
public class ProjetoList extends EntityQuery {
private static final String[] RESTRICTIONS = {"lower(projeto.nome) like concat(lower(#{projetoList.projeto.nome}),'%')",};
private Projeto projeto = new Projeto();
@Override
public String getEjbql() {
return "select projeto from Projeto projeto";
}
@Override
public Integer getMaxResults() {
return 25;
}
public Projeto getProjeto() {
return projeto;
}
@Override
public List<String> getRestrictions() {
return Arrays.asList(RESTRICTIONS);
}
}
E a página Estudo2.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:rich="http://richfaces.org/rich"
template="layout/template.xhtml"
xmlns:a4j="http://richfaces.org/a4j">
<ui:define name="body">
<h:messages globalOnly="true" styleClass="message" id="globalMessages"/>
<h:form id="projetoSearch" styleClass="edit">
<rich:simpleTogglePanel label="Pesquisa Projeto" switchType="ajax">
<s:decorate template="layout/display.xhtml">
<ui:define name="label">Nome</ui:define>
<rich:comboBox defaultLabel="Escolha um projeto" selectFirstOnUpdate="false"
width="150" value="#{projetoList.projetoSelecionado.nome}" >
<s:selectItems var="projeto" value="#{projetoList.resultList}" />
</rich:comboBox>
<!--
<h:inputText id="nome" value="#{projetoList.projeto.nome}"/>
<rich:comboBox selectFirstOnUpdate="false"
defaultLabel="Escolha um Projeto">
<s:selectItems value="#{projetoList.resultList}" var="nome" />
</rich:comboBox>
-->
<div class="actionButtons">
<h:commandButton id="search" value="Search" action="/Estudo2.xhtml"/>
</div>
<!--
<h:inputText id="nome" value="#{projetoList.projeto.nome}"/>
-->
</s:decorate>
</rich:simpleTogglePanel>
</h:form>
<br />
<rich:panel>
<f:facet name="header">Resultados Encontrados</f:facet>
<div class="results" id="projetoList">
<h:outputText value="The projeto search returned no results."
rendered="#{empty projetoList.resultList}"/>
<rich:dataTable id="projetoList"
var="projeto"
value="#{projetoList.resultList}"
rendered="#{not empty projetoList.resultList}">
<h:column>
<f:facet name="header">
<s:link styleClass="columnHeader"
value="idProjeto #{projetoList.orderColumn=='idProjeto' ? (projetoList.orderDirection=='desc' ? messages.down : messages.up) : ''}">
<f:param name="sort" value="idProjeto" />
<f:param name="dir" value="#{projetoList.orderDirection=='asc' ? 'desc' : 'asc'}"/>
</s:link>
</f:facet>
#{projeto.idProjeto}
</h:column>
<h:column>
<f:facet name="header">
<s:link styleClass="columnHeader"
value="nome #{projetoList.orderColumn=='nome' ? (projetoList.orderDirection=='desc' ? messages.down : messages.up) : ''}">
<f:param name="sort" value="nome" />
<f:param name="dir" value="#{projetoList.orderDirection=='asc' ? 'desc' : 'asc'}"/>
</s:link>
</f:facet>
#{projeto.nome}
</h:column>
<h:column>
<f:facet name="header">
<s:link styleClass="columnHeader"
value="dataInicio #{projetoList.orderColumn=='dataInicio' ? (projetoList.orderDirection=='desc' ? messages.down : messages.up) : ''}">
<f:param name="sort" value="dataInicio" />
<f:param name="dir" value="#{projetoList.orderDirection=='asc' ? 'desc' : 'asc'}"/>
</s:link>
</f:facet>
#{projeto.dataInicio}
</h:column>
<h:column>
<f:facet name="header">
<s:link styleClass="columnHeader"
value="dataFim #{projetoList.orderColumn=='dataFim' ? (projetoList.orderDirection=='desc' ? messages.down : messages.up) : ''}">
<f:param name="sort" value="dataFim" />
<f:param name="dir" value="#{projetoList.orderDirection=='asc' ? 'desc' : 'asc'}"/>
</s:link>
</f:facet>
#{projeto.dataFim}
</h:column>
<h:column>
<f:facet name="header">action</f:facet>
<s:link view="/#{empty from ? 'Projeto' : from}.xhtml"
value="#{empty from ? 'View' : 'Select'}"
id="projeto">
<f:param name="projetoIdProjeto"
value="#{projeto.idProjeto}"/>
</s:link>
</h:column>
</rich:dataTable>
</div>
</rich:panel>
<div class="tableControl">
<s:link view="/Estudo2.xhtml"
rendered="#{projetoList.previousExists}"
value="#{messages.left}#{messages.left} First Page"
id="firstPage">
<f:param name="firstResult" value="0"/>
</s:link>
<s:link view="/Estudo2.xhtml"
rendered="#{projetoList.previousExists}"
value="#{messages.left} Previous Page"
id="previousPage">
<f:param name="firstResult"
value="#{projetoList.previousFirstResult}"/>
</s:link>
<s:link view="/Estudo2.xhtml"
rendered="#{projetoList.nextExists}"
value="Next Page #{messages.right}"
id="nextPage">
<f:param name="firstResult"
value="#{projetoList.nextFirstResult}"/>
</s:link>
<s:link view="/Estudo2.xhtml"
rendered="#{projetoList.nextExists}"
value="Last Page #{messages.right}#{messages.right}"
id="lastPage">
<f:param name="firstResult"
value="#{projetoList.lastFirstResult}"/>
</s:link>
</div>
<s:div styleClass="actionButtons" rendered="#{empty from}">
<s:button view="/ProjetoEdit.xhtml"
id="create"
value="Create projeto">
<f:param name="projetoIdProjeto"/>
</s:button>
</s:div>
</ui:define>
</ui:composition>
OBS: isto é o que eu tenho… mas podem alterar à vontade!!! hehehe
Michel,
Pelo que entendi você quer que um dataTable seja populado após o usuário selecionar um valor no comboBox é isso?
Bom, se for isso no seu comboBox você pode definir um método no “valueChangeListener”, que irá “acionar” seu bean
Exemplo no comboBox:
<rich:comboBox valueChangeListener="#{seuBean.popularLista}" onchange="document.NOMEDOFORM.submit();">
<s:selectItems var="item" value="#{seuBean.lista}" />
</rich:comboBox>
*tentei fazer sem esse “onchange” mas não consegui
Exemplo do método no bean:
public void popularLista(ValueChangeEvent event) {
Long id = Long.valueOf((String) event.getNewValue());
//agora é só você criar um método que recebe o id do valor selecionado e seta sua lista a partir desse id
FacesContext.getCurrentInstance().renderResponse();
}
Espero que isso possa ti ajudar, se tiver outra dúvida posta aí.
Abraços,
Opa, vlw pela resposta, porém nao entendi direito o método no bean (Na classe ProjetoList).
a página ficou: (está correta?!)
<h:form id="[b]projetoSearch[/b]" styleClass="edit">
<rich:simpleTogglePanel label="Pesquisa Projeto" switchType="ajax">
<s:decorate template="layout/display.xhtml">
<ui:define name="label">Nome</ui:define>
<rich:comboBox valueChangeListener="#{projetoList.popularLista}"
onchange="document.[b]projetoSearch[/b].submit();">
<s:selectItems var="projeto" value="#{projetoList.lista}" />
</rich:comboBox>
Michel,
Fiz um exemplo rápido, não está nada elegante mas funciona, da pra você tirar como base eu espero.
Bean:
//imports
public class ProjetoBean {
private List<String> listaDeFilhos;
//get e set
//montando combo dos pais
public List<SelectItem> getPais() {
List<SelectItem> lista = new ArrayList<SelectItem>();
for (int i = 0; i < 5; i++) {
SelectItem item = new SelectItem(i+1, "pai " + (i + 1));
lista.add(item);
}
return lista;
}
//populando a lista de filhos a partir do pai escolhido
public void popularLista(ValueChangeEvent event) {
Long id = Long.valueOf(event.getNewValue().toString());
listaDeFilhos = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
listaDeFilhos.add("Filho " + i + " do pai " + id);
}
//substitua esse for por um método que consulte o banco de dados e tals
FacesContext.getCurrentInstance().renderResponse();
}
}
Página;
...
<f:view>
<h:form id="meuForm">
<h:outputText value="Escolha um pai: "/>
<h:selectOneMenu id="pai" onchange="document.meuForm.submit();"
valueChangeListener="#{projetoBean.popularLista}">
<f:selectItems value="#{projetoBean.pais}"/>
</h:selectOneMenu>
<br />
<h:dataTable border="1" value="#{projetoBean.listaDeFilhos}" var="filho" rendered="#{not empty projetoBean.listaDeFilhos}">
<h:column>
<h:outputText value="#{filho}" />
</h:column>
</h:dataTable>
</h:form>
</f:view>
...
Fiz com selectOnMenu, mas com o comboBox do richfaces deve funcionar normal.
Tenta aí e me fala se deu certo.
Abraços,
deu certo agradeco a todos!!!
segue o combo:
<rich:comboBox defaultLabel="Escolha um projeto" selectFirstOnUpdate="false"
width="150" value="#{projetoList.projeto.nome}" >
<s:selectItems var="projeto" value="#{projetoList.resultList}" />
</rich:comboBox>
e a classe no bean:
@SuppressWarnings("unchecked")
public ArrayList<Atividade> getProjetoDataTable() {
ArrayList<Projeto> arrayProjeto = new ArrayList<Projeto>();
ArrayList<Atividade> arrayAtividades = new ArrayList<Atividade>();
arrayProjeto = (ArrayList<Projeto>) getResultList();
for (Projeto projeto : arrayProjeto) {
for (Atividade atividade : projeto.getAtividades()) {
arrayAtividades.add(atividade);
}
}
arrayProjeto.remove(projeto);
return arrayAtividades;
}
