Hi,

I would like to know your opinion about how to work with DataTable
using MyFaces in the typical example where a list of objects is
presented and a link for each object is generated so as to go to some
kind of maintenance form.

IMHO this can be achieved using several approaches:

1) Bind the DataTable component to a property in a JSF bean; then
create an action in the bean and associate it to the CommandLink so
whenever the link is clicked, the action will be invoked, and you will
be able to get the current object with dataTable.getRowData():

<x:dataTable var="object" binding="#{ObjectBean.objectDataTable}"
preserveDataModel="true">
    <h:column>
        <x:commandLink action="#{ObjectBean.chooseObjectAction}"
immediate="true">
            <x:outputText value="#{object.someProperty}" />
        </x:commandLink>
    </h:column>
</x:dataTable>

2) Just give the value of the DataTable a collection of objects, but
use an UpdateActionListener to know which object was clicked (as now
you don't have the getRowData() available). This results in a setter
in our JSF bean being invoked when the link is clicked:

<x:dataTable var="object" value="#{ObjectBean.listOfObjects}"
preserveDataModel="true">
    <h:column>
        <x:commandLink action="nextPage" immediate="true">
            <x:outputText value="#{object.someProperty}" />
            <x:updateActionListener value="#{object.id}"
property="#{ObjectBean.id}" />
        </x:commandLink>
    </h:column>
</x:dataTable>

In my tests, the update action method in the JSF bean doesn't get
invoked if I set preserveDataModel="false". Don't know why...

3) Similar to 2, but using request parameters instead of the
UpdateActionListener, that can be later obtained from within the
action in the JSF bean:

<x:dataTable var="object" value="#{ObjectBean.listOfObjects}"
preserveDataModel="true">
    <h:column>
        <x:commandLink action="#{ObjectBean.chooseObjectAction}"
immediate="true">
            <x:outputText value="#{object.someProperty}" />
            <f:param value="#{object.id}" />
        </x:commandLink>
    </h:column>
</x:dataTable>

And then from the JSF bean, get the parameters using the FacesContext
through the getParametersMap() method.

What do you currently use? What do you think is the best approach?
What are the alternatives?

An also, what happens when the DataModel must survive between
different requests, for example, when using tabs? Is the only solution
to make the JSF bean session scoped?

Looking forward to hearing your comments ;-)

Reply via email to