You need to sign in to do that
Don't have an account?
If statement in Visualforce
Hi,
I have a simple question but I'm struggling with it as I'm still a beginner in Visualforce.
I created a button called "Print Contract" inside a page that belongs to the custom object Visa__c, and a visualforce page that renders to PDF a contract using html and css.
Here's a piece of code from the Visualforce page as example:
However, if the field {!Visa__c.CityName} = Shanghai, when the users click on the Print Contract button I want them to see a different text in the contract, for example, something like this (where Passport is replaced by ID Card).
How can I do this by using a IF statement in the Visualforce page?
Thanks.
I have a simple question but I'm struggling with it as I'm still a beginner in Visualforce.
I created a button called "Print Contract" inside a page that belongs to the custom object Visa__c, and a visualforce page that renders to PDF a contract using html and css.
Here's a piece of code from the Visualforce page as example:
<div class="allContainer"> <div class="headerContainer"> <div class="headerTitle"><label class="labelTitle">Job Contract<br>聘用合同</br></label></div> <div class="teacherContainer"> <table> <tr> <th style="width:50%;">1. Personal Info</th> </tr> <tr> <td><label class="labelDetail">Passport Number: </label> <apex:outputText styleClass="teacherDetail" value=" {!Visa__c.PassportNumber}"/></td> </tr>
However, if the field {!Visa__c.CityName} = Shanghai, when the users click on the Print Contract button I want them to see a different text in the contract, for example, something like this (where Passport is replaced by ID Card).
<div class="allContainer"> <div class="headerContainer"> <div class="headerTitle"><label class="labelTitle">Job Contract<br>聘用合同</br></label></div> <div class="teacherContainer"> <table> <tr> <th style="width:50%;">1. Personal Info</th> </tr> <tr> <td><label class="labelDetail">ID Card: </label> <apex:outputText styleClass="teacherDetail" value=" {!Visa__c.IDCard}"/></td> </tr>
How can I do this by using a IF statement in the Visualforce page?
Thanks.
Try like this
<apex:outputlabel value="{!IF(m.Name='sai' ,true,false) }">
{
true condition here
}
</apex:outputlabel>
Thanks,
Sumitha P
Actually you can use RENDERED attribute for restricting the display based on other values. You can use this as follows:
<apex:outputText styleClass="teacherDetail" value=" {!Visa__c.IDCard}" rendered="{!if(Visa__c.Name=='xyz',true,false}"/>
Hope this will be useful.
Thanks
Bhargavi.
If I have multiple values to check for Visa__c.Name, how can I add them?
Thanks.
How could I hide or show some piece of text where I don't have an outputText value?
For example, how could I hide these titles if Visa__c.Name = Shanghai:
In the <apex:pageblocktable> we have<apex:column> even in which you can use using rendered.
Thanks.
<apex:page standardcontroller="Visa_c" extensions="sample">
<apex:repeat value="{!query}" var="tst">
<apex:outputText styleClass="teacherDetail" value=" {!tst.IDCard}"/></td>
<apex:outputlabel rendered="{!hide}">
<br><tr> <th style="width:50%;">5. Contract Dates</th>
<th style="width:50%; padding-left: 20px">5. 合同日期</th> </tr></br>
</apex:outputlabel>
</apex:repeat>
</apex:page>
apex class:
public class sample
{
public boolean hide{get;set;}
public List<Visa__c> query{get;set;}
{
pulic sample()
{
hide= false; // true means it will display
query=[select IDCard,(call all fields API name here) from Visa__c where Name = Shanghai];
}
}
Hope it helps,
Thanks,
Sumitha P
Does your code hide only the fields or also the text like "5. Contract Dates" between the <th> tag?
It hides all the text/inputfields given inside the
<apex:outputlabel rendered="{!hide}">
//hide all the fields given inside.
</apex:outputlabel>
Thanks
Sumitha P
thanks again.
A few more questions:
for the class, it's a separate file, right?
I tried to create it but I get an error. I'm not familiar with writing Apex classes so perhaps I'm doing something wrong.
Sry its a mistake.
Bracles { is added extra, try the below code.
Apex class:
public class sample
{
public boolean hide{get;set;}
public List<Visa__c> query{get;set;}
public sample()
{
hide= false; // true means it will display
query=[select IDCard,(call all fields API name here) from Visa__c where Name = Shanghai];
}
}
Thanks,
Sumitha P
now I get this error:
Error: Unknown constructor 'sample.sample(ApexPages.StandardController controller)'
For now, I only want to hide the text "Social Insurance" so I made something like this:
and your class:
Try tis one,
Visual force Page:
<apex:page standardController="Visa__c" extensions="sample">
<apex:outputlabel rendered="{!hide}">
<br>
<tr>
<th style="width:50%;">6. Social Insurance</th>
<th style="width:50%; padding-left: 20px;">6. 社会保险</th>
</tr>
</br>
</apex:outputlabel>
<br>
<tr>
</apex:page>
public class sample
{
public boolean hide{get;set;}
public List<Visa__c> query{get;set;}
public sample(ApexPages.StandardController controller)
// should add the content here because in visual force page when standardcontroller is given should add this
{
hide= false; // true means it will display
}
}
Hope it Helps you,
Thanks,
Sumitha P
But if I want to hide it only when CityName = 'Chengdu', where can I add the condition?
I tried to add it in the class but it didn't work:
Thanks so much for all your help so far.