是否有可能使我的 visualforce 页面仅在特定条件下在 visualforce 组件中可见

Is there any possibility to make my visualforce page visible in visualforce component only in specific conditions

我的 visualforce 页面中有一个元素列表,有时可能是空的。 visualforcepage 显示在主页上的 visualforce 组件中。我想知道如果列表为空,是否有可能隐藏该组件。

 <apex:repeat var="a" value="{!accounts}">
                <apex:outputLabel for="link" value="{!a.Name} " />
 </apex:repeat>

您可以使用 rendered 属性。像

<apex:repeat var "a" value="{!accounts}" rendered="{!NOT(AccountsEmpty)}">
    <apex:outputLabel for="link" value="{!a.name}" />
</apex:repeat>

并在您的控制器中添加方法:

public boolean getAccountsEmpty(){
  return this.accounts == null || this.account.size() == 0;
}

编辑:添加如何不渲染父级 components/page

Here is some in depth documentation on this 但简而言之,您需要在组件中添加一个带有重复的属性,这样您就可以传入页面父 page/components 顶点控制器,例如,如果您的父页面有一个名称为 MyApexController...

的顶点控制器
<apex:attribute name="controller" description="parent page controller" type="MyApexController" assignTo="{!pageController}"/>

然后您可以从组件中使用 public getter 和 setter 访问它的成员 这是它的样子 父页面控制器

public class MyApexController {
  public boolean hasAccounts {get; set;}

  public MyApexController(){
    //Your controller constructor
  }
}

子组件 Apex 控制器

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;
}

然后在您的父页面或组件上为您的子组件添加一个类似的属性 父页面标签 <apex:page rendered="{!hasAccounts}">

或父组件标签

<apex:component rendered="{!hasAccounts}">