rowEdit在dataTable Primefaces上过滤行 [英] rowEdit filtered rows on dataTable Primefaces

查看:287
本文介绍了rowEdit在dataTable Primefaces上过滤行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用原始的dataTable和rowEdit事件来更改和保存我的dataTable中的值。当我编辑未被过滤的行值时,它的效果很好,但是当我过滤值并尝试编辑时,编辑错误的ID。

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.

例如:
我有一个包含5个记录的列表,范围从1到5的ids。

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

我过滤列表以显示id = 5的记录,所以它只显示一行。
当我尝试编辑这个记录时,我的bean正确地获取值,但是在应用过滤器之前,该对象的ID不是5,它的1在datatable的第一行。
如果我编辑过滤表的第二行,它将获取原始datatable的第二行的id,而不带过滤器。

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.

屏幕: p>

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):

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" ));
        }
    }


推荐答案

当我使用@requestScoped bean发生在我身上。我切换到@ViewScoped,它正常工作。

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

这篇关于rowEdit在dataTable Primefaces上过滤行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆