2

In a page block table based on a condition, I want to render some values as hyperlink and other as plain text. I am able to achieve the above requirement as below:

  <apex:column headerValue="Account Name">
                <apex:outputText rendered="{!IF(a.AccessLevel__c != 'Full',true,false)}" value="{!a.Name}"></apex:outputText>
                <apex:outputLink rendered="{!IF(a.AccessLevel__c == 'Full',true,false)}" value="/{!a.id}">{!a.Name}</apex:outputLink>
  </apex:column> 

I want to know is there a better way of doing it from VF page without changing the logic in class

2
  • 2
    I think it is good. I also used the same piece of code earlier when it required. If you want to do with another approach then apply the CSS conditionally. In this use only <apex:outputLink ../> and apply css of hyperlink when it will treat as link otherwise css of plain text. – Anurag Bhardwaj Jul 18 '16 at 11:35
  • @AnuragBhardwaj Disadvantage with CSS is that it still gets rendered to the page and a savvy user might be able to dig it out. I believe using the rendered attribute does not leave that possibility open. – Adrian Larson Jul 18 '16 at 13:51
3

Your markup is fine.

Your merge logic, however, violates a basic axiom of Boolean logic:

You can always replace IF(condition, true, false) with simply condition.

In your case, it should be:

<apex:outputText rendered="{!a.AccessLevel__c != 'Full'}" ...>
<apex:outputLink rendered="{!a.AccessLevel__c == 'Full'}" ...>

Another minor cleanup is that you can build the link itself more programmatically.

Instead of:

<apex:outputLink ... value="/{!a.id}">

it is arguably cleaner to use:

<apex:outputLink ... value="{!URLFOR($Action.Account.View, a.id)}">
2
  • value="{!URLFOR($Action.Account.View, a.id)}" is not working (Nothing displaying) – C0DEPirate Jul 19 '16 at 7:59
  • Hmm if it doesn't yield anything, just stick with what you've got then. It's worth trying when building links though. – Adrian Larson Jul 19 '16 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.