Salesforce VisualForce:将回车转换为长文本字段中的html换行符

Salesforce VisualForce:将回车转换为长文本字段中的html换行符,salesforce,carriage-return,visualforce,Salesforce,Carriage Return,Visualforce,在Salesforce中,如果我要将文本字段绑定到VisualForce页面,那么将文本字段中的回车转换为HTML标记的好方法是什么 e、 g.从这样的事情开始: <apex:page standardController="Case"> <apex:pageBlock title="Test"> <p>{!case.Description}</p> </apex:pageBlock>

在Salesforce中,如果我要将文本字段绑定到VisualForce页面,那么将文本字段中的回车转换为HTML

标记的好方法是什么

e、 g.从这样的事情开始:

<apex:page standardController="Case">
  <apex:pageBlock title="Test">
      <p>{!case.Description}</p>
  </apex:pageBlock>                   
  <apex:detail relatedList="false" />
</apex:page>   

{!case.Description}

。。。如果描述很长,有很多回车符,我该如何将其HTML化

(我想这是一个相当简单的问题,我相信我可以用谷歌搜索它,但为了让Salesforce社区继续下去,我想我们需要一些简单的问题。)

编辑:(增加了赏金以尝试产生一些刺激)

试试这个:

<apex:outputField value="{!case.Description}"/>


使用输出字段将自动保持格式。

您尝试过使用outputText吗

如果这不起作用,请在此处投票支持我的想法: 因为我在尝试将JSON返回到页面时遇到了相同的问题


其他人也想要这个想法

我最终通过一些冗长的代码实现了这一点

在自定义控制器中,添加手动搜索并替换字段中的换行符并将其替换为

标记后返回字段的方法:

public string getCaseDescriptionFormatted()
{
    Case c = this.loadCaseFromDatabaseOrWhatever();
    return lineBreaks(c.Description);   
}

private string lineBreaks(string inText)
{
   if (inText == null)
       return '';
   else
       return inText.replaceAll('<','(').replaceAll('>',')').replaceAll('\n','<br/>');
}
公共字符串getCaseDescriptionFormatted()
{
案例c=this.loadcasefromdatabaseorwhich();
返回换行符(c.说明);
}
专用字符串换行符(字符串inText)
{
如果(inText==null)
返回“”;
其他的
返回inText.replaceAll(''').replaceAll('\n','
'); }
然后在页面中,使用apex:outputText和escape=“false”:


请注意,escape=“false”是防止VisualForce转义html标记所必需的。这也意味着您可能会受到脚本攻击,这些攻击可能通过假设嵌入到数据中来实现。这就是为什么控制器中的
换行符()
fn也会替换任何
字符


(可能有更好的方法来确保字符串安全,欢迎建议)

您可以尝试以下方法:

{!substitute(Case.Description, '\n', '<br/>')}
{!替换(Case.Description,'\n','
')}
对我来说,特恩德做到了——我试图在VisualForce通知电子邮件模板中显示一个案例“描述”,所有的CR/LF都消失了,行/段都在一起运行。将其设置为OutputField值完全修复了它。

上面的TehNrd为我回答了这个问题

我正在开发一个选项卡式的案例视图,类似于常见的帐户示例。当涉及到显示案例评论时,您不能仅仅将它们放入相关列表中,而是需要手动设置它们的格式。使用标准的apex pageBlockTable会导致用户无法读取紧凑的表,因此我们必须进行更多的手工编码。这种方法还允许我使用CSS格式化表内容。但问题是用换行符和电子邮件格式化案例评论。特恩德的回答非常有效

对于其他人,这里是显示带有格式化CaseComent的选项卡以及编辑注释的操作的代码

<apex:tab label="Comments" name="Comments" id="tabComments">
    <apex:form >
        <apex:pageBlock id="commentsPageBlock">
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Toggle Sort" action="{!RequeryComments}" id="theButton" rerender="commentsPageBlock"></apex:commandButton>
            </apex:pageBlockButtons>
            <table border="0"  class="commentsTable">       
            <tr>
                <th class="commentsActionColumn">Action</th>
                <th class="commentBodyClass">Comments</th>
            </tr>
            <!-- get the case comments from the controller -->
            <apex:repeat value="{!comments}" var="c">
                <tr>
                <td class="commentsActionColumn">
                <!-- open the case comment for edit -->
                <apex:outputLink title="" value="/{!c.id}/e?parent_id={!c.parentId}&retURL=/apex/{!$CurrentPage.Name}%3Fid={!case.id}" style="font-weight:bold">Edit</apex:outputLink> 
                </td>
                <td>
                <!-- display the case comment formatted using the apex outputField -->
                <div class="commentTdClass">
                <apex:outputField value="{!c.commentbody}"></apex:outputField>
                </div>
                </td>
                </tr>
            </apex:repeat>
            </table>
        </apex:pageBlock>
    </apex:form>
</apex:tab>

行动
评论
编辑

这对我不起作用。当我使用outputField时,回车不会转换。更新:编辑操作仅在当前用户拥有案例注释时有效。我正在处理解决方案,将进行更新。
CR LF
字符不使用此技术检测,也不使用
“\r\n
”。
<apex:tab label="Comments" name="Comments" id="tabComments">
    <apex:form >
        <apex:pageBlock id="commentsPageBlock">
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Toggle Sort" action="{!RequeryComments}" id="theButton" rerender="commentsPageBlock"></apex:commandButton>
            </apex:pageBlockButtons>
            <table border="0"  class="commentsTable">       
            <tr>
                <th class="commentsActionColumn">Action</th>
                <th class="commentBodyClass">Comments</th>
            </tr>
            <!-- get the case comments from the controller -->
            <apex:repeat value="{!comments}" var="c">
                <tr>
                <td class="commentsActionColumn">
                <!-- open the case comment for edit -->
                <apex:outputLink title="" value="/{!c.id}/e?parent_id={!c.parentId}&retURL=/apex/{!$CurrentPage.Name}%3Fid={!case.id}" style="font-weight:bold">Edit</apex:outputLink> 
                </td>
                <td>
                <!-- display the case comment formatted using the apex outputField -->
                <div class="commentTdClass">
                <apex:outputField value="{!c.commentbody}"></apex:outputField>
                </div>
                </td>
                </tr>
            </apex:repeat>
            </table>
        </apex:pageBlock>
    </apex:form>
</apex:tab>