JSF: Como filtrar uma h:dataTable?

4 respostas
J

Salve Galera,
Como faço para filtrar uma h:dataTable ?
Consegui trazer todos os dados e paginar minha dataTable, mas gostaria de ter dois campos(input) na tela: valorParaPesquisa e criterioDePesquisa e um botão de pesquisar que chamasse um método do meu backingBean e me filtrasse a lista original.

Isto é possível ?

4 Respostas

M

ummm é só você criar um método que manipule esta lista aí no seu backing bean.

:okok:

J

Marcos,
apesar da sua dica não estou conseguindo.

Vamos aos códigos:

[ FacesConfig ]
<managed-bean>
        <managed-bean-name>funcaoList</managed-bean-name>
        <managed-bean-class>br.com.simcaweb.web.FuncaoList</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
	    <managed-property>
	    	<property-name>rowsPerPage</property-name>
	      	<value>5</value>
	    </managed-property>
        <managed-property>
            <property-name>funcaoManager</property-name>
            <value>#{funcaoManager}</value>
        </managed-property>
    </managed-bean>
[ BackingBean ]
package br.com.simcaweb.web;

import java.util.List;

import javax.faces.component.UIData;
import javax.faces.event.ActionEvent;

import br.com.simcaweb.service.FuncaoManager;

public class FuncaoList {
    
	private FuncaoManager mgr = null;
	private int rowsPerPage = 5;
	private UIData historyDataTable;
	private String valorPesquisado = "";
	private String criterioPesquisa = "";

	public UIData getHistoryDataTable(){
		return historyDataTable;
	}
	public void setHistoryDataTable(UIData historyDataTable){
		this.historyDataTable = historyDataTable;
	}
	public int getRowsPerPage(){
		return rowsPerPage;
	}
	public void setRowsPerPage(int rowsPerPage){
		this.rowsPerPage = rowsPerPage;
	}
	public boolean getShowNext(){
		return (historyDataTable.getFirst() + rowsPerPage) < getFuncoes().size();
	}
	public boolean getShowPrevious(){
	    return (historyDataTable.getFirst() - rowsPerPage) >= 0;
	}
	public void next(ActionEvent actionEvent){
	    int newFirst = historyDataTable.getFirst() + rowsPerPage;
	    if (newFirst < getFuncoes().size()){
	    	historyDataTable.setFirst(newFirst);
	    }
	}
	public void previous(ActionEvent actionEvent){
		int newFirst = historyDataTable.getFirst() - rowsPerPage;
	    if (newFirst >= 0){
	    	historyDataTable.setFirst(newFirst);
	    }
	}
	public void first(ActionEvent actionEvent){
		historyDataTable.setFirst(0);		
	}
	public void last(ActionEvent actionEvent){
		
		int newFirst = historyDataTable.getRowCount() - rowsPerPage;
	    if (newFirst >= 0){
	    	historyDataTable.setFirst(newFirst);
	    }
	}
	public String getValorPesquisado() {
		return valorPesquisado;
	}
	public void setValorPesquisado(String valorPesquisado) {
		this.valorPesquisado = valorPesquisado;
	}
	public String getCriterioPesquisa() {
		return criterioPesquisa;
	}
	public void setCriterioPesquisa(String criterioPesquisa) {
		this.criterioPesquisa = criterioPesquisa;
	}
	public void setFuncaoManager(FuncaoManager funcaoManager){
        this.mgr = funcaoManager;
    }    
    public List getFuncoes() {
        return mgr.getFuncoes();
    }
    public List getFuncoes(String a, String b){
    	return null;    	
    }
    public List getFuncoesByCriteria(){
    	
    	System.out.println(getValorPesquisado() + "-" + getCriterioPesquisa());
    	getFuncoes(valorPesquisado, criterioPesquisa);
    	return null;
    }
}
[ JSP ]
<%@ include file="/taglibs.jsp"%>

<f:view>
<f:loadBundle var="messages" basename="messages"/>
<title><fmt:message key="index.title"/>-<fmt:message key="funcaoList.title"/></title>
	<h2><fmt:message key="funcaoList.title"/></h2>
	<h:form id="funcaoList">
	    <fieldset>
			<legend><fmt:message key="field.pesquisa"/>:</legend>
			<h:outputLabel for="valorPesquisado" value="#{messages['field.valor']}"/>:
			<h:inputText value="#{funcaoList.valorPesquisado}" id="valorPesquisado" required="false" size="40"/>
			<select id="criterioPesquisa" value="#{funcaoList.criterioPesquisa}">
				<option>exatamente</option>
				<option>começando por</option>
				<option>terminando por</option>
			</select>
			<h:commandButton accesskey="P" value="#{messages['button.pesquisar']}" action="#{funcaoList.getFuncoesByCriteria}" id="find" immediate="true" styleClass="button"/>
		</fieldset>	    
		<hr/>
		<div align="center">
			<h:commandButton accesskey="I" value="#{messages['button.incluir']}" action="add" id="add" immediate="true" styleClass="button"/>
			<h:commandButton accesskey="r" value="#{messages['button.imprimir']}" action="#{funcaoForm.print}" id="print" immediate="true" styleClass="button"/>
			<h:commandButton accesskey="A" value="#{messages['button.atualizar']}" action="#{funcaoForm.requery}" id="requery" immediate="true" styleClass="button"/>
		</div>
		<hr/>
		<h:dataTable var="funcao" value="#{funcaoList.funcoes}" binding="#{funcaoList.historyDataTable}" rows="#{funcaoList.rowsPerPage}" id="funcaoList" styleClass="list" rowClasses="odd,even">
	        <h:column>
		        <f:facet name="header"><h:outputText value="#{messages['field.id']}"/></f:facet>
		        <h:commandLink action="#{funcaoForm.edit}" value="#{funcao.id}">
		            <f:param name="id" value="#{funcao.id}"/>
		        </h:commandLink>
			</h:column>
		    <h:column>
		        <f:facet name="header"><h:outputText value="#{messages['field.descricao']}"/></f:facet>
		        <h:outputText value="#{funcao.descricao}"/>
		    </h:column>
		    <f:facet name="footer">
				<h:panelGroup>
				    <h:commandLink actionListener="#{funcaoList.first}" style="padding-right: 5px;">
						<h:outputText value="#{messages['button.primeiro']}"/>
					</h:commandLink>
					<h:commandLink actionListener="#{funcaoList.previous}" rendered="#{funcaoList.showPrevious}" style="padding-right: 5px;">
						<h:outputText value="#{messages['button.anterior']}"/>
					</h:commandLink>
					<h:commandLink actionListener="#{funcaoList.next}" rendered="#{funcaoList.showNext}" style="padding-right: 5px;">
						<h:outputText value="#{messages['button.proximo']}"/>
					</h:commandLink>
					<h:commandLink actionListener="#{funcaoList.last}">
						<h:outputText value="#{messages['button.ultimo']}"/>
					</h:commandLink>
				</h:panelGroup>
			</f:facet>
		</h:dataTable>
	</h:form>
</f:view>
Inclusive eu não to conseguindo ler os campos para pesquisa que eu seto na tela.
J

E ai galera,
Alguma luz ?

M

Opa,

Acho que o teu problema está em não ter criado uma variável para armazenar suas funções… aí sempre que você executa o método e faz as operações, mas quando vai carregar, chama o metódo getFuncoes e ele simplemente, retorna na ordem default.

:okok:

Criado 28 de junho de 2006
Ultima resposta 29 de jun. de 2006
Respostas 4
Participantes 2