1

Let's say I want to iterate over a List of objects based on a class in my controller (myObject), and have a commandButton to display a pageblock within the apex:repeat.

<apex:repeat value="{!myObjects}" var='myObject'>
<apex:commandButton action="{!myObject.renderMe}" value="Show Pageblock" />
<apex:pageblock title="myTitle" rendered = "{!myObject.rendered}">
Here I am!
</apex:pageblock>
</repeat>

Controller excerpt:

List<myObject> myObjects;

public class myObject {
    public Integer height {get;set;}
    public Integer width {get;set;}
    public Boolean rendered {get; set;}

    public void renderMe(){
        this.rendered = true;
    }

public List<myObject> getmyObjects(){
    List<myObject> tmpList = new List<myObject>();
    myObject obj1 = new myObject();
    obj1.height = 5;
    obj1.width = 5;
    obj1.rendered = false;
    tmpList.add(obj1);
    return tmpList;
}

}

When I click the commandButton, the pageblock is not rendering because {!myObject.rendered} is not being set to true. Why is this the case?

More generally, is storing the rendered property like this the right approach?

Thanks

2
  • 1
    I believe the apex:commandbutton will rerender the complete page and thus reinitialize all your object to rendered=false. Try adding a rerender (of the complete table) to the commandbutton. Commented Mar 17, 2014 at 16:02
  • Thanks for the suggestion, guy. For future reference, this was not the problem - Double A's answer did the trick without needing to change rerender my attribute.
    – Rob
    Commented Mar 17, 2014 at 17:33

1 Answer 1

2

Storing a render value is fine in practice, but it really depends on what you are trying to achieve. If this is only for a UI display, I would probably use javascript to hide your section. If you are doing some sort of processing with objects which are hidden and which are displayed your approach works just fine. I had no issue when I replicated your code.

Class

public with sharing class testController {
    public List<myObject> myobjects {get; set;}

    public testController(){
        myobjects = new List<myObject>();
        for(Integer i = 0; i <= 5; i++){
            myobjects.add(new myObject(i,i,false));
        }
    }

    public class myObject {
        public Integer height {get;set;}
        public Integer width {get;set;}
        public Boolean rendered {get; set;}

        public myObject(Integer height, Integer width, Boolean rendered){
            this.height = height;
            this.width = width;
            this.rendered = rendered;
        }

        public void renderMe(){
            this.rendered = true;
        }
    }

}

Visualforce Page

<apex:page controller="testController" >

<apex:form>
    <apex:repeat value="{!myObjects}" var="myObject">
        <apex:commandButton action="{!myObject.renderMe}" value="Show Pageblock" />
        <apex:pageblock title="myTitle" rendered="{!myObject.rendered}">
           {!myObject.height}
        </apex:pageblock>
        <br/>
    </apex:repeat>
</apex:form>

</apex:page>
1
  • Thank you Double A, this answer was very helpful. A JS-only solution was not appropriate in this particular case, but it's great to know that the general approach was valid.
    – Rob
    Commented Mar 17, 2014 at 17:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .