Pick row reference in table with JSF(Primefaces)

Asked

Viewed 653 times

1

I have a scenario that presents something weird and I don’t understand why this behavior. Using JSF with Primefaces, I have a Datatable with some (several) values that comes from the Database and are displayed in the columns. My scenario shows the following dependencies:

JSF - Primefaces (Datatable) > Managedbeans > DAOS (Methods of access to data) > 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 is 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 than the one on the line.

  • Try adding the attribute rowKey="#{call.callId}" in your data table.

1 answer

1


The problem was occurring because when I clicked on my commandlink, when opening the showdialog was destroyed and reloaded my Managedbean, so my array was reloaded with the latest information. I decided to add an init() method annotated as @Postconstruct to load the array and fixed the scope of my Managedbeans from @Applicationscoped to @Sessionscoped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.