I have a list of elements in my visualforce page that can sometime be empty. The visualforcepage is displayed in a visualforce component on the home page. I wonder if there is any possibility to hide the component if the list is empty.
<apex:repeat var="a" value="{!accounts}">
<apex:outputLabel for="link" value="{!a.Name} " />
</apex:repeat>
You could use the rendered
attribute. Something like
<apex:repeat var "a" value="{!accounts}" rendered="{!NOT(AccountsEmpty)}">
<apex:outputLabel for="link" value="{!a.name}" />
</apex:repeat>
and in your controller add the method:
public boolean getAccountsEmpty(){
return this.accounts == null || this.account.size() == 0;
}
EDIT: Adding how to also not render parent components/page
Here is some in depth documentation on this but put simply you need to add an attribute to your component with the repeat in it so you can pass in the page parent page/components apex controller for example if your parent page had a apex controller with the name MyApexController
...
<apex:attribute name="controller" description="parent page controller" type="MyApexController" assignTo="{!pageController}"/>
Then you can access its members with public getters and setters from within the component Heres how that would look Parent Page Controller
public class MyApexController {
public boolean hasAccounts {get; set;}
public MyApexController(){
//Your controller constructor
}
}
Child Components Apex controller
public class MyComponentsController {
public List<Account> accounts {get; set;}
public MyApexController pageController {get; set;} //this will get set by apex:attibute
public MyComponentController(){
if(getAccountsEmpty()){
this.pageController.hasAccounts = false; //no accounts let parent page know
} else {
this.pageController.hasAccounts = true;
}
}
public boolean getAccountsEmpty(){
return this.accounts == null || this.account.size() == 0;
}
Then on your parent page or component just add a similar attribute to your child component
Parent page tag
<apex:page rendered="{!hasAccounts}">
or a parent component tag
<apex:component rendered="{!hasAccounts}">