function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Patrick Ledbetter HSPatrick Ledbetter HS 

apex:outputField will not render a div tag correctly

From VF Page:
<div style="float: left; width: 20%;">

<apex:outputField value="{!d.Company__c}" />

</div>

In the controller, if the Company__c is null, I want to set its value to '<div class="spacer" />' so that the div tag is rendered correctly on the page.  My output at the moment is literally <div class="spacer" />.

The goal is to have 5 static columns on the page but if one of the custom fields for the custom object is null, I just want a placeholder there in the HTML so that everything continues to line up correctly.

Best Answer chosen by Patrick Ledbetter HS
mukesh guptamukesh gupta
Hi PAtrick,

Please review this code :-

Apex Controller :- ShowHideHTMLControler
public class ShowHideHTMLControler {
 
    //Public Properties
    public Boolean showContent { get; set; }
 
    //Constructor
    public ShowHideHTMLControler() {
        //Set the showContent variable to true on page load
        showContent = true;
    }
 
    //Method called when the Toggle Content button is clicked
    public PageReference toggleContent() {
        //If the showContent variable is true, set it to false, else, set it to true
        if(showContent){
            showContent = false;
        }
        else{
            showContent = true;
        }
        return null;
    }
}
Vfpage:-
<apex:page controller="ShowHideHTMLControler">
    <apex:form>
        <h1>Show/Hide HTML form Visualforce Example</h1>
        
        <div id="contentToToggle" style="display:{!if(showContent,"block","none")};">
            This is the content that will be toggled.
        </div>
        
        <div>
            This content will not be toggled.
        </div>
        
        <apex:commandbutton value="Toggle Content" action="{!toggleContent}"></apex:commandbutton>
    </apex:form>
</apex:page>

Kindly mark my solution as the best answer if it helps you.

Regards
Mukesh



 

All Answers

mukesh guptamukesh gupta
Hi PAtrick,

Please review this code :-

Apex Controller :- ShowHideHTMLControler
public class ShowHideHTMLControler {
 
    //Public Properties
    public Boolean showContent { get; set; }
 
    //Constructor
    public ShowHideHTMLControler() {
        //Set the showContent variable to true on page load
        showContent = true;
    }
 
    //Method called when the Toggle Content button is clicked
    public PageReference toggleContent() {
        //If the showContent variable is true, set it to false, else, set it to true
        if(showContent){
            showContent = false;
        }
        else{
            showContent = true;
        }
        return null;
    }
}
Vfpage:-
<apex:page controller="ShowHideHTMLControler">
    <apex:form>
        <h1>Show/Hide HTML form Visualforce Example</h1>
        
        <div id="contentToToggle" style="display:{!if(showContent,"block","none")};">
            This is the content that will be toggled.
        </div>
        
        <div>
            This content will not be toggled.
        </div>
        
        <apex:commandbutton value="Toggle Content" action="{!toggleContent}"></apex:commandbutton>
    </apex:form>
</apex:page>

Kindly mark my solution as the best answer if it helps you.

Regards
Mukesh



 
This was selected as the best answer
Patrick Ledbetter HSPatrick Ledbetter HS
Mukesh, I ended up solving it with something very similar to your idea.  I used apex:outputPanels.

<!-- COLUMN 1 DATA -->
<apex:outputPanel rendered="{!d.Company__c != null}">
<div style="float: left; width: 20%;">
<apex:outputField value="{!d.Company__c}" />
</div>
</apex:outputPanel>
<apex:outputPanel rendered="{!d.Company__c == null}">
<div style="float: left; width: 20%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>
</apex:outputPanel>