Play Games

Search This Blog

Tuesday, April 19, 2016

How to remove html tags from rich text area field while displaying data in visualforce page.

Problem : When we are displaying data in vf page,if we display data of rich text area using <apex:outputtext> tag,the output data contains some html tags as shown in the image below.

Solution : Use escape ="false" attribute with in <apex:outputtext> .
Example: In this example I have created Rich Text field in lead object as Rich Text Area field as shown in image below.

Also Created Lead Record as shown in image below.

Apex Code:
public class RichTextAreaIssue {
    public Lead lobj {get;set;}
    public string textStr {get;set;}
    public RichTextAreaIssue () {
        lobj  = [select id,name,rich_text__c from lead where id=:'00Q2800000HTety' ];
        textStr =lobj.rich_text__c.stripHtmlTags();
    }
}
Visualforce page code Without using escape attribute :
<apex:page Controller="RichTextAreaIssue">
    <apex:form >
        <apex:pageBlock title="Display Rich Text Area Data in Visualforce Page" >
                <apex:pageBlockSection columns="4">
                     <apex:outputText >{!lobj.Rich_Text__c}</apex:outputText>
                </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Output : 

Visualforce page code  using escape attribute :
<apex:page Controller="RichTextAreaIssue">
    <apex:form >
        <apex:pageBlock title="Display Rich Text Area Data in Visualforce Page" >
                <apex:pageBlockSection columns="4">
                     <apex:outputText escape="false" value="{!lobj.Rich_Text__c}"></apex:outputText>
                </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Output : 

Important Note: use value attribute of <apex:outputtext> to display data.
if we use like this <apex:outputText escape="false" >{!lobj.Rich_Text__c}</apex:outputText>  it won't work.
if we use like this <apex:outputText escape="false" value="{!lobj.Rich_Text__c}"></apex:outputText>,then only it will work.

Enjoy Coding.............


1 comment:

  1. This escape="false" is not working on my end :(

    ReplyDelete