3
votes

I'm using primefaces dataTable and rowEdit event to change and save the values in my dataTable. When I edit a row value that isn't filtered, it works well, but when i filter the values and try to edit, the wrong id is edited.

For example: I have a list with 5 records, ranging with ids from 1 to 5.

I filter the list to show the record where the id = 5, so it shows only one row. When I try to edit this record, my bean gets the values correctly, but the id of the object instead of being 5, its 1, wich was on the first row of the datatable before I applied the filter. If I edit the second row of a filtered table, it get the id of the second row of the original datatable without the filter.

Screen:

<p:dataTable id="dataTable" var="linha" rowKey="#{linha.id}"
                            value="#{gestor.listaGestor}" editable="true"
                            draggableColumns="true" 
                            rendered="#{not empty gestor.listaGestor}"
                            widgetVar="tableGestor" filteredValue="#{gestor.filteredGestor}"
                            emptyMessage="Não existem registros." resizableColumns="true" styleClass="datatable_cadastro" liveResize="true">

                            <f:facet name="header">
                                Tabela Gestores
                                <p:commandButton id="toggler" type="button" value="Colunas" />
                                <p:columnToggler datasource="dataTable" trigger="toggler" />
                            </f:facet>

                            <p:ajax event="rowEdit" listener="#{gestor.onRowEdit}"
                                update=":edicao:msgs,:edicao:dataTable" />
                            <p:ajax event="rowEditCancel" listener="#{gestor.onRowCancel}"
                                update=":edicao:msgs" />

                            <p:column headerText="ID" sortBy="#{linha.id}" filterBy="#{linha.id}"
                                style="width:50px;">
                                <p:outputLabel value="#{linha.id}" style="width:100%" />
                            </p:column>
                            <p:column headerText="NOME" filterBy="#{linha.nome}"
                                filterMatchMode="contains" sortBy="#{linha.nome}"
                                style="width:100%;">
                                <p:cellEditor>
                                    <f:facet name="output">
                                        <h:outputText value="#{linha.nome.trim()}" />
                                    </f:facet>
                                    <f:facet name="input">
                                        <p:inputTextarea value="#{linha.nome}" id="nome"
                                            update=":edicao" style="width:100%" />
                                    </f:facet>
                                </p:cellEditor>
                            </p:column>
                            <p:column headerText="E-MAIL"
                                style="width:100%;" sortBy="#{linha.email}"
                                filterBy="#{linha.email}" filterMatchMode="contains">
                                <p:cellEditor>
                                    <f:facet name="output">
                                        <h:outputText value="#{linha.email.trim()}" />
                                    </f:facet>
                                    <f:facet name="input">
                                        <p:inputTextarea value="#{linha.email}" id="email"
                                            update=":edicao" />
                                    </f:facet>
                                </p:cellEditor>
                            </p:column>
                            <p:column headerText="ATIVO" style="width:150px;" filterBy="#{linha.ativo}" filterMatchMode="equals">
                                    <f:facet name="filter">
                                        <p:selectOneMenu onchange="PF('tableGestor').filter()" >
                                            <f:converter converterId="javax.faces.Character"/>
                                            <f:selectItem itemLabel="TODOS" itemValue=""/>
                                            <f:selectItem itemLabel="ATIVO" itemValue="T"/>
                                            <f:selectItem itemLabel="INATIVO" itemValue="F"/>
                                        </p:selectOneMenu>
                                    </f:facet>
                                    <p:cellEditor>
                                        <f:facet name="output">
                                            <p:selectBooleanCheckbox value="#{gestor.charToBool(linha.ativo)}" disabled="true"/> 
                                        </f:facet>
                                        <f:facet name="input">
                                            <p:selectBooleanCheckbox value="#{gestor.ativo}" id="ativo" immediate="true" update=":edicao" style="width:100%;"/>
                                        </f:facet>
                                    </p:cellEditor>    
                                </p:column>
                            <p:column headerText="!" style="width:40px">
                                <p:rowEditor/>
                            </p:column>
                            <p:column headerText="X" style="width:40px">
                                <p:commandLink styleClass="ui-icon ui-icon-trash"
                                    action="#{gestor.deletar(linha)}" update=":edicao" />
                            </p:column>

                        </p:dataTable>

Bean(onRowEdit):

    public void onRowEdit(RowEditEvent event){

        Object value = event.getObject();

        GestorVO gestorTela = (GestorVO) value;

        SimpleDateFormat dataHora = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        FacesContext context = FacesContext.getCurrentInstance();

        if(gestorTela.getId() != null){

            if(ativo){
                gestorTela.setAtivo('T');
            } else {
                gestorTela.setAtivo('F');
            }

            GestorRN gestorRN = new GestorRN();
            GestorVO gestorAntes = gestorRN.getByPrimaryKey(gestorTela.getId());

            /** CONSISTENCIA DOS CAMPOS */

            /** IF CASO O CONTEUDO DOS CAMPOS SEJA NULO */
            if (gestorTela.getNome().trim().isEmpty() || gestorTela.getEmail().trim().isEmpty() ) {

                gestorTela.setNome(gestorAntes.getNome());
                gestorTela.setEmail(gestorAntes.getEmail());
                gestorTela.setAtivo(gestorAntes.getAtivo());

                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Alteração foi cancelada" ,"Nenhum campo pode estar vazio ou conter apenas espaço(s)." ));

            } else {
                /** IF CASO O CONTEUDO FOI SALVO MAS NÃO HOUVERAM ALTERAÇÕES NO REGISTRO */
                if (gestorAntes.getNome().trim().equals(gestorTela.getNome().trim()) 
                    && gestorAntes.getEmail().trim().equals(gestorTela.getEmail().trim())
                    && gestorAntes.getAtivo() == gestorTela.getAtivo() ) {

                    gestorTela.setNome(gestorAntes.getNome());
                    gestorTela.setEmail(gestorAntes.getEmail());
                    gestorTela.setAtivo(gestorAntes.getAtivo());

                    context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Alteração foi cancelada" ,"Não houve alteração no registro" ));
                }
                /** CASO O CONTEUDO TENHA SIDO REALMENTE ALTERADO, SALVA O CONTEUDO */
                else{
                    /** Realiza o trim para limpeza dos campos antes da gravação*/
                    gestorTela.setNome(gestorTela.getNome().trim());
                    gestorTela.setEmail(gestorTela.getEmail().trim());
                    gestorTela.setUsuarioAlteracao(loginRN.usuarioLogado().getNome());
                    gestorTela.setDataAlteracao(dataHora.format(new Date()));
                    /** Caso ocorra erros no método salvar, a tela retornará para os valores padrões antes da alteração */
                    boolean confirmacao = gestorRN.salvar(gestorTela);

                    if(!confirmacao) {
                        gestorTela.setNome(gestorAntes.getNome());
                        gestorTela.setEmail(gestorAntes.getEmail());
                        gestorTela.setAtivo(gestorAntes.getAtivo());
                    }
                }
            }
        } else {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "ID do objeto Gestor não identificado" ,"Não foi possível identificar o ID do Gestor selecionado. Informe o ocorrido ao administrador do sistema" ));
        }
    }
1
What Primefaces's version you're using?Deoxyseia
Hi Deoxyseia, Primefaces 5.0Leonardo Candido

1 Answers

0
votes

That happened to me when I used @requestScoped bean. I switched to @ViewScoped and it worked correctly.