Get row reference in table with JSF (Primefaces)

1

I have a scenario that presents something strange and I did not understand the reason for this behavior. Using JSF with Primefaces, I have a Datatable with some (many) values that come from the Database and are displayed in the columns. My scenario has the following dependencies:

JSF - Primefaces (Datatable) > ManagedBeans > DAOS (Data Access Methods) > Models (JPA)

My datatable looks like this:

<h:form id="form">
    <p:growl id="msgs" showDetail="true" />
    <p:dataTable id="calls" var="call" value="#{callMB.allCalls}" rows="25" paginator="true" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PageLinks} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="25,50,100" paginatorPosition="bottom" draggableColumns="true" scrollable="true" emptyMessage="Nenhum registro foi encontrado.">
                    <c:facet name="header">
                        Consulta de Chamadas
                    </c:facet>
                    <p:column headerText="Info" width="200" style="text-align: center">
                        <p:commandLink value="Ver Mais" type="button"
                            oncomplete="PF('callDialog').show();" update=":form:dialog">
                            <c:setPropertyActionListener value="#{call}"
                                target="#{callMB.selectedCall}"></c:setPropertyActionListener>
                        </p:commandLink>
                    </p:column>
                    <p:column headerText="Call ID" filterBy="#{call.callId}"
                        filterMatchMode="exact" width="500">
                        <h:outputText value="#{call.callId}"></h:outputText>
                    </p:column>
                </p:dataTable>
                <p:dialog id="dialog" header="Modal Dialog" widgetVar="callDialog"
                    modal="true" height="100">
                    <h:outputText value="#{callMB.selectedCall.callId}"></h:outputText>
                </p:dialog>
            </h:form>

My ManagedBean looks like this:

@ManagedBean
@ApplicationScoped
public class CallMB {

    private List<Call> allCalls;
    private Call selectedCall;

    public Call getSelectedCall() {
        return selectedCall;
    }

    public void setSelectedCall(Call selectedCall) {
        this.selectedCall = selectedCall;
    }

    public List<Call> getAllCalls() {
        allCalls = new CallDAO().retrieveAllCalls();
        RouteDAO routeDAO = new RouteDAO();
        for (Call call : allCalls) {
            call.setRouteName(routeDAO.retrieveRouteByCode(call.getRouteCode()).getName());
        }
        return allCalls;
    }
}

But when I click on my "See More" link it is looking for a totally different id from the one on the line.

    
asked by anonymous 23.09.2016 / 17:00

1 answer

1

The problem was occurring because when I clicked on my commandlink, when I opened the showdialog it was destroyed and reloaded my ManagedBean, so my array was reloaded with the latest information. I solved by adding an init () method annotated as @PostConstruct to load the array and fixed the scope of my ManagedBeans from @ApplicationScoped to @SessionScoped.

    
25.09.2016 / 17:56