Inserir linha e recarregar rich:dataTable

5 respostas
yorgan

Olá Pessoal,
Estou utilizando a rich:dataTable e preciso inserir linhas nela dinamicamente. Alguém já fez algo parecido?
Fiz um teste com o código que segue abaixo, mas a tabela não recarrega.

JSP

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
    <f:view>
        <head>
            <link href="css/estilos.css" rel="stylesheet" type="text/css" />
            <title><h:outputText value="Climp" /></title>
        </head>
        
        <body style="border: 0px; padding: 0px;">
            <center>
                <h:form>
                    <a4j:region>
                        <h:commandButton action="#{testeDataTable.inserirLinha}" value="inserir linha" />
                        <rich:dataTable value="#{testeDataTable.lista}" var="pes" style="width:200px;" id="table" rows="30"
                          columnClasses="dataTableColuna100px,
                                         dataTableColuna100px">
                            <f:facet name="header">
                                <rich:columnGroup>
                                    <rich:column breakBefore="true">
                                        <h:outputText value="Coluna1"/>
                                    </rich:column>
                                    <rich:column>
                                        <h:outputText value="Coluna2"/>
                                    </rich:column>
                                </rich:columnGroup>
                            </f:facet>
                            <rich:column>
                                <h:outputText value="#{pes.coluna1}"/>
                            </rich:column>
                            <rich:column>
                                <h:outputText value="#{pes.coluna2}"/>
                            </rich:column>
                        </rich:dataTable>
                    </a4j:region>
                </h:form>
            </center>
        </body>
    </f:view>
</html>

Bean

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package teste;

import java.util.ArrayList;
import java.util.List;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author daniel
 */
public class TesteDataTable {
    private DataModel lista;
    private ObjetoTabela objeto;
    
    public DataModel getLista(){
	lista = new ListDataModel(dadosTabela());
	return lista;
    }     
    
    public String inserirLinha() {
        lista = new ListDataModel(dadosTabela(1));
        return "linhaInserida";
    }
    
    private List dadosTabela(int numeroAdicionar) {
        numeroAdicionar += 10;
        List lista = new ArrayList();
        ObjetoTabela obj;
        for(int i = 0; i < numeroAdicionar; i++) {
            obj = new ObjetoTabela();
            obj.setColuna1("C1 Dado " + i);
            obj.setColuna2("C1 Dado " + i);
            lista.add(obj);
        }
        return lista;
    }
    
    
    private List dadosTabela() {
        return dadosTabela(0);
    }
}

[]´s

Daniel

5 Respostas

Javabuntu

sim já fiz, basta incluir um novo item na lista que está ligada na DataTable e fazer um reRender no id da tabela.

yorgan

Tentei trocar o commandButton por:

<a4j:commandButton action="#{testeDataTable.inserirLinha}" value="inserir linha" reRender="table" />

Mas não deu certo.
O reRender eu tenho que aplicar no commandButton ou na tabela?

[]´s

Javabuntu

yorgan:
Tentei trocar o commandButton por:

<a4j:commandButton action="#{testeDataTable.inserirLinha}" value="inserir linha" reRender="table" />

Mas não deu certo.
O reRender eu tenho que aplicar no commandButton ou na tabela?

[]´s

é assim mesmo é no botao que coloca, aparentemente está certo, agora tem que ver se realmente está inserindo na lista. Faz um debug e verifica se o valor realmente é adicionado, porque era pra aparece na tabela desse jeito ai.

yorgan

Opa,
Quando debugo, ele entra no método e gera a lista novamente sem erros. Já tentei adicionar mais de um registro e também não muda nada.

Muito estranho. Eu tenho que setar alguma coisa no faces-config ou no web.xml?

[]'s

yorgan

Deu certo pessoal, o código é esse mesmo que está ai, feitas as modificações sitadas pelo Javabuntu.
Após inserir o reRender ele rodou certo.

Valeu Javabuntu.

[]´s

Daniel

Criado 1 de setembro de 2008
Ultima resposta 1 de set. de 2008
Respostas 5
Participantes 2