Find your content:

Search form

You are here

VisualForce: Conditionally render HTML elements like

 
Share

I have a Force.com site portal written completely with custom Visualforce, HTML and CSS.

I have a component which is used to display the navigation links at the top of the page. This navigation is built using and

  • tags which is pretty common.

    <ul id="hdrnav">
                    <li><a href="/apex/billing" id="billnav">Billing</a></li>
                    <li><a href="/apex/deposits" id="depositsnav">Deposits</a></li>  
                    <li><a href="/apex/checks" id="checksnav">Checks</a></li>   
    
                </ul>
    

    Now I want to be able to render these

  • links based on the user's security as each of these links display information from different custom objects.

    I know we can check a user's access to a record using something like:

    {!$ObjectType.objectname.accessible}
    

    So how can I achieve something like not displaying the

  • link for deposits if the user has no access to the deposit custom object.


    Attribution to: Richard N

    Possible Suggestion/Solution #1

    This is a bit off the cuff, but I think you can wrap it in a panel:

    <apex:outputPanel rendered="{!$ObjectType.objectname.accessible}" layout="none"><!-- content --> </apex:outputPanel>
    

    Would that work in your case?


    Attribution to: joshbirk

    Possible Suggestion/Solution #2

    One option would be to compose them server side, then display with <apex:dataList> or <apex:repeat>. Useful especially if the list will grow in future?

    To do that build some wrapper apex class (or even something as simple as Map<String, String>) that would hold the URL and display name. Anything you can do in VF with {!$ObjectType.Account.accessible} you can check with Account.sObjectType.getDescribe().isAccessible()


    Attribution to: eyescream

    Possible Suggestion/Solution #3

    Off the cuff, I believe you can overload the use of the <apex:variable> tag along with it's rendered flag to show/hide individual elements.

    <apex:variable var="foo" rendered="{!$ObjectType.objectname.accessible} >
        <li> bar ... </li>
    </apex:variable>
    

    Attribution to: Kevin P

    Possible Suggestion/Solution #4

    If the VF level or Controller level is not an option (the two first things I would try) - then you could also potentially control it via jQuery. Give the LI's you want to hide a specific class, and then tie it back to a function which checks if the user has the permission, and then shows the LI (instead of the other way around).


    Attribution to: joshbirk
    This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4113
  • My Block Status

    My Block Content