I have a page with a datatable that populates from a list. The list has many ids, which I need to pass to a bean method and perform a database operation.I am using a commandButton's rendered attribute to make a call to the bean method (findInfo()). I need to pass the ids (myBean.testList.id) one at a time from the list to this bean method so that I get the boolean back. First of, I am not sure how to pass the id (to the findInfo method in my bean), secondly if I use tag, I end up sending just a single ID. Could anyone please suggest me a solution. I tried doing it with the codes as below.
My Bean
@ManagedBean(name="myBean")
public class MyBean(){
public boolean findInfo(Integer indId){
boolean var = dataProvider.isIdPresent(indId); // database operation
if (var)
return true;
else
return false;
}
}
xhtml page
<h:dataTable value="#{myBean.testList}" var="t" >
<h:column>
<f:facet name="header">
<h:outputText value="Actions" />
</f:facet>
<h:commandButton value = "Add"
action = "#{someBean.loadReport}"
rendered = "#{myBean.findInfo() == false}"/>
<h:commandButton value = "View"
action = "#{someBean.loadReport}"
rendered = "#{myBean.findInfo() == true}"/>
</h:column>
<h:column>
...
</h:column>
</h:dataTable>
UPDATE: I tried to follow the answer that BalusC's explained (that kolossus posted), I end up passing the id as null in f:param. Any suggestions. My updated code:
<h:dataTable value="#{myBean.testList}" var="t" >
<h:column>
<f:facet name="header">
<h:outputText value="Actions" />
</f:facet>
<h:commandButton value = "Add"
action = "#{someBean.loadReport}"
rendered = "#{myBean.findInfo() == false}">
<f:param name="id" value="#{t.id}" />
</h:commandButton>
<h:commandButton value = "View"
action = "#{someBean.loadReport}"
rendered = "#{myBean.findInfo() == true}">
<f:param name="id" value="#{t.id}" />
</h:commandButton>
</h:column>
<h:column>
...
</h:column>
My Updated Bean
@ManagedBean(name="myBean")
@RequestScoped
public class MyBean(){
@ManagedProperty("#{param.id}")
private Integer indId;
public boolean findInfo(){
boolean var = dataProvider.isIndIdPresent(indId);
if (var)
return true;
else
return false;
}
}
Also, I think I cannot use f:ViewParam as I am calling the bean from the data table within the same view.