2015-04-06 84 views
3

我曾经使用c:if,c:当jsp中的JSTL标签。但是我不知道是否有类似的东西可用于视觉页面。举个例子,我正在为jsp提供示例代码。 -if-else visualforce中的条件块

<h1>A Demo conditional section code</h1> 
    <c:choose> 
     <c:when test="${param.colorField == 'red'}"> 
     <table border="0" width="150" height="50" bgcolor="#ff0000"> 
      <tr><td>It is red</td></tr> 
     </table> 
     </c:when> 
     <c:when test="${param.colorField == 'blue'}"> 
     <table border="0" width="150" height="50" bgcolor="#0000ff"> 
     <tr><td>It is blue</td></tr> 
     </table> 
    </c:when> 
    <c:when test="${param.colorField == 'green'}"> 
     <table border="0" width="150" height="50" bgcolor="#00ff00"> 
      <tr><td>Green table</td></tr> 
     </table> 
    </c:when> 
    <c:otherwise> 
     <table border="0" width="150" height="50" bgcolor="#000000"> 
     <tr><td>No colour changed</td></tr> 
     </table> 
    </c:otherwise> 
</c:choose> 
<br/> 
and other codes.... 

我在vf页面中缺少这种页面块准备。

回答

3

我发现,我们可以使用outputpanel(<apex:outputpanel>)任何块,并使用rendered属性来处理加载它的条件。

<h1>A Demo conditional section code</h1> 
    <apex:outputpanel rendered="{!param.colorField == 'red'}"> 
     <table border="0" width="150" height="50" bgcolor="#ff0000"> 
      <tr><td>It is red</td></tr> 
     </table> 
    </apex:outputpanel> 
    <apex:outputpanel rendered="{!param.colorField == 'blue'}"> 
     <table border="0" width="150" height="50" bgcolor="#0000ff"> 
     <tr><td>It is blue</td></tr> 
     </table> 
    </apex:outputpanel> 
    : 
    : 
and other codes.... 
1

在visualforce中,您可以使用一些逻辑运算符和函数。 Explanation here

你需要“逻辑功能”列表中,相同的代码,您提供的,在VF必须是这样的:

{!IF(salary<=0, "Salary is very low to survive.", "No comment sir")} 
+0

好的。在这种情况下,我提供的样本太简单了,不能说明我想提到的复杂性。说一个条件,我想以表格的形式显示一组数据。否则,我会打印一个段落。那又怎么样?我想很多包含HTML标记和价值观等 – 2015-04-07 06:28:30

+1

对于开关的代码块一个代码块,你可以使用渲染属性附加伤害 <顶点之间切换:outputPanel渲染=“{工资<=0}"> ... 0}”> ... nickzenko 2015-04-07 06:48:05

0

你需要把逻辑控制器(其中大多数人认为它属于无论如何)。你VF会是什么样子:

<table border="0" width="150" height="50" bgcolor="{!bgColorVar}"> 

而在你的控制器,在吸气定义逻辑:

public string bgColorVar{ 
    get{ 
     //logic 
    } 
    set; 
} 
1

相同的概念放在这里其他的答案,但你可以使用呈现的属性上PageBlock渲染该块或不:

<apex:pageBlock rendered="{!object.Color == 'red'}"> 
    it is red 
</apex:pageBlock> 
<apex:pageBlock rendered="{!object.Color == 'blue'}"> 
    it is blue 
</apex:pageBlock> 
<apex:pageBlock rendered="{!object.Color == 'green'}"> 
    it is green 
</apex:pageBlock>