Hi Enrique,
I use an instance of javax.faces.model.DataModel as a value for the
datatable. The value is held by a bean which holds the instance of the
DataModel. The DataModel instance is used to iterate through the rows of
the datatable. When an action is called on a row I only have to get the
current row from the DataModel to find the row on which the action was
called.
The JSF-Page contains:
<h:dataTable value="bean.valuesModel" var="object">
<h:column>
<h:commandLink action="#{bean.chooseObjectAction}">
<x:outputText value="#{object.someProperty}" />
</h:commandLink>
</h:column>
</h:dataTable>
The Bean contains:
public class SomeBeanClass
{
DataModel mValueModel;
public DataModel getValuesModel()
{
if(mValueModel == null)
{
mValueModel = new ListDataModel(initializeValues());
}
return mValueModel;
}
public String chooseObjectAction()
{
Object currentObject = getValuesModel().getRowData();
}
}
Hope this helps a little bit.
Mathias
> -----Original Message-----
> From: Enrique Medina [mailto:[EMAIL PROTECTED]
> Sent: Thursday, May 05, 2005 6:58 PM
> To: MyFaces Discussion
> Subject: DataTable discussion
>
>
> 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 ;-)
>