0

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.

mona
  • 6,079
  • 12
  • 41
  • 46
  • possible duplicate of [Passing parameters between managed beans with request scope](http://stackoverflow.com/questions/9701590/passing-parameters-between-managed-beans-with-request-scope) – kolossus Mar 28 '13 at 19:49
  • @kolossus I tried to change my code based on the post you mentioned. But it did not work. I pasted my updated code. Any help appreciated. – mona Mar 28 '13 at 20:28
  • "Did not work".equals("?"). :) Did it the param return null? Did it throw an exception? These are burning questions @mona. Hope you didn't forget to add the getter and setter for `indId` (I can't see it anywhere here) and your `@RequestScoped` is from the `javax.faces.bean` package. As a last resort alternative, you can pass the variable directly within your markup `action = "#{someBean.loadReport(t.id)}"` and change your method signature accordingly. This works only if you're on JSF with EL2.2 – kolossus Mar 28 '13 at 21:05
  • The f:param returned null to the managed bean. I had the getter/setters for indId, @RequestScoped is from javax.faces.bean package, but I cannot get the indId in my bean. I am not using JSF with EL2.2, so cannot go for the last alternative you suggested. The updated codes pasted above is all the changes I have now.. Thanks for your suggestions, please let me know if you think I should add anything more.....Thanks – mona Mar 29 '13 at 01:17
  • I completely overlooked the render condition. Your render condition is quite convoluted: it depends on a value that will always be 0 as at the time the value is being evaluated. To verify that this is the root cause of the problem now, remove it and try to pass the parameter. Ultimately, to fix, Perform the check in a method annotated with `@PostConstruct` and use the `eq` operator (instead of `==`) in the render condition – kolossus Mar 29 '13 at 02:15

1 Answers1

2

First of all, you should declare your bean as @ViewScoped. Then, you can keep your findInfo method as following:

@ManagedBean(name="myBean")
@ViewScoped
public class MyBean(){
    private List testList;    

    public boolean findInfo(Integer indId){ ... }

    // Getters and Setters
}

The following snippet should do the job:

<h:dataTable value="#{myBean.testList}" var="t" >
    <h:column>
        <h:commandButton value="Add" action="#{someBean.loadReport}"
                         rendered="#{not myBean.findInfo(t.id)}" />

        <h:commandButton value="View" action="#{someBean.loadReport}"
                         rendered="#{myBean.findInfo(t.id)}" />
    </h:column>
</h:dataTable>

UPDATE: Since you're not using EL 2.2, you may need process the boolean value for each row before rendering the table. Wrapping the objects in the testList inside a dummy object containing a boolean value should do the job :). Something like this:

@ManagedBean(name="myBean")
@ViewScoped
public class MyBean(){
    private List<DummyObject> testList;    

    @PostConstruct
    public boolean prepareTestList(){ 
        // Initiate test list
        List<AnObject> oriList = ....;

        // Process oriList to create a testList
        this.testList = new LinkedList();
        for (AnObject o : oriList) {
            DummyObject do = new DummyObject();
            do.setObject(o);
            do.setIdPresent(findInfo(o.getId()));
            this.testList.add(do);
        }
    }

    public boolean findInfo(Integer indId) { ... }

    public class DummyObject {
       private boolean  idPresent;
       private AnObject object;

       // Getters and Setters
    }

    // Getters and Setters
}

Then your rendered attribute should look like this:

<h:commandButton value="View" action="#{someBean.loadReport}"
                 rendered="#{t.idPresent}" />
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90