Problemas com o filterBy de um dataTable - Primefaces

5 respostas
H

Olá Pessoal, estou com o seguinte problema, tenho um método que retorna uma lista de objetos de uma base da dados MySQL, populei um dataTable com estes dados, porém quando digito algo a ser buscado dentro da dataTable é executado várias vezes este meu método que retorna os dados em um List<>.

Com isso trava meu banco pois está chamando o método várias vezes.

Se alguém puder me ajudar fico muito grato.

Abraços.

5 Respostas

ThEDiegO

Posta seus códigos… bean, xhtml …

Hebert_Coelho
Veja se o get da sua lista não está buscando toda hora o objeto no banco, se estiver faça algo do tipo
public List<Carro> getCarros(){
    if(carros == null){
        carros = new ArrayList<Carro>();
    }

    return carros;
}
A

Dá uma olhada no xpert-framework ele possui o componente “filterOnEnter” que vincula o filterBy a ser executado apenas quando o usuário pressionar “enter”

Link do showcase:
xpertsistemas.com.br/xpert-showcase-war/views/components/filterOnEnter.jsf

H

Olá Abaixo o xhtml

<p:dataTable 
                    id="tabela" 
                    value="#{alunoController.model}" 
                    var="a"
                    paginator="true"
                    rows="15"
                    emptyMessage="Nenhum aluno encontrado"
                    rowStyleClass="#{a.tipoBolsa eq 'DESISTENTE' ? 'vermelho' : null}">

                    <p:column>  
                        <f:facet name="header">  
                            Matrícula
                        </f:facet>  
                        <h:outputText value="#{a.rm}" />  
                    </p:column> 

                    <p:column filterBy="#{a.nome}">  
                        <f:facet name="header">  
                            Nome  
                        </f:facet>  
                        <h:outputText value="#{a.nome}" />  
                    </p:column> 

                    <p:column>  
                        <f:facet name="header">  
                            Tipo Bolsa
                        </f:facet>  
                        <h:outputText value="#{a.tipoBolsa}" />  
                    </p:column>
Método que retorna a lista
public static List<AlunoBean> getLista() throws SQLException {
        System.out.println("entrou aqui");
        List<AlunoBean> lista = new ArrayList<AlunoBean>();
        Connection con = Conexao.getConnection();
        String sql = "select * from aluno order by nome";
        PreparedStatement stmt = con.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            AlunoBean aluno = new AlunoBean();
            aluno.setIdAluno(rs.getInt("idAluno"));
            aluno.setNome(rs.getString("nome"));
            aluno.setRm(rs.getString("rm"));
            aluno.setCpf(rs.getString("cpf"));
            aluno.setIndicante(rs.getString("indicante"));
            aluno.setBanco(rs.getString("banco"));
            aluno.setPerIndicBolsa(rs.getString("perIndicBolsa"));
            aluno.setPorBolsa(rs.getString("porBolsa"));
            aluno.setTipoBolsa(rs.getString("tipoBolsa"));
            aluno.setObservacoes(rs.getString("observacoes"));
            aluno.setTurma(rs.getString("turma"));
            lista.add(aluno);
        }
        stmt.close();
        rs.close();
        con.close();

        return lista;
    }

ManagedBean

private DataModel modeloTabela;
    private List<AlunoBean> lista;

    public DataModel getModel() {
        List<AlunoBean> lista = null;
        try {
            lista = AlunoDAO.getLista();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        modeloTabela = new ListDataModel(lista);
        return modeloTabela;
    }
Hebert_Coelho
hebaum:
Olá Abaixo o xhtml
<p:dataTable 
                    id="tabela" 
                    value="#{alunoController.model}" 
                    var="a"
                    paginator="true"
                    rows="15"
                    emptyMessage="Nenhum aluno encontrado"
                    rowStyleClass="#{a.tipoBolsa eq 'DESISTENTE' ? 'vermelho' : null}">

                    <p:column>  
                        <f:facet name="header">  
                            Matrícula
                        </f:facet>  
                        <h:outputText value="#{a.rm}" />  
                    </p:column> 

                    <p:column filterBy="#{a.nome}">  
                        <f:facet name="header">  
                            Nome  
                        </f:facet>  
                        <h:outputText value="#{a.nome}" />  
                    </p:column> 

                    <p:column>  
                        <f:facet name="header">  
                            Tipo Bolsa
                        </f:facet>  
                        <h:outputText value="#{a.tipoBolsa}" />  
                    </p:column>
Método que retorna a lista
public static List<AlunoBean> getLista() throws SQLException {
        System.out.println("entrou aqui");
        List<AlunoBean> lista = new ArrayList<AlunoBean>();
        Connection con = Conexao.getConnection();
        String sql = "select * from aluno order by nome";
        PreparedStatement stmt = con.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            AlunoBean aluno = new AlunoBean();
            aluno.setIdAluno(rs.getInt("idAluno"));
            aluno.setNome(rs.getString("nome"));
            aluno.setRm(rs.getString("rm"));
            aluno.setCpf(rs.getString("cpf"));
            aluno.setIndicante(rs.getString("indicante"));
            aluno.setBanco(rs.getString("banco"));
            aluno.setPerIndicBolsa(rs.getString("perIndicBolsa"));
            aluno.setPorBolsa(rs.getString("porBolsa"));
            aluno.setTipoBolsa(rs.getString("tipoBolsa"));
            aluno.setObservacoes(rs.getString("observacoes"));
            aluno.setTurma(rs.getString("turma"));
            lista.add(aluno);
        }
        stmt.close();
        rs.close();
        con.close();

        return lista;
    }

ManagedBean

private DataModel modeloTabela;
    private List<AlunoBean> lista;

    public DataModel getModel() {
        List<AlunoBean> lista = null;
        try {
            lista = AlunoDAO.getLista();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        modeloTabela = new ListDataModel(lista);
        return modeloTabela;
    }
Você leu o que eu escrevi?
Criado 12 de novembro de 2012
Ultima resposta 12 de nov. de 2012
Respostas 5
Participantes 4