在Salesforce的,如果我绑定的文本字段成VisualForce页,什么在文本字段中的回车符转换成HTML <br/>标签?

的好方法

e.g。从开始的东西这样的:

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

...如果描述很长,有很多回车的,我怎么HTML-IFY呢?

(我想这是一个很简单的问题,我敢肯定,我可以谷歌,但要获得Salesforce的社区会在这里我想我们需要一些简单的问题。)

编辑:(富饶加入尝试和产生一些刺激)

有帮助吗?

解决方案

尝试这种情况:

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

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

其他提示

我最终与一些长嗦代码来实现这一点。

在定制控制器,添加方法后手动搜索以返回字段和外地更换换行和与<br/>标签替换它们:

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/>');
}

然后,在页中,使用顶点:与的outputText逃逸= “假”:

<apex:outputText value="{!CaseDescriptionFormatted}" escape="false" />

请注意逃逸=“假”是必要的,以防止VisualForce逸出html标签。这也意味着你给自己打开,可以通过假设嵌入在数据脚本攻击。这就是为什么在控制器中的lineBreaks() FN也替换任何<>字符。

(有可能是一个更好的办法,以使字符串安全,建议表示欢迎)

TehNrd以上回答了这个问题对我来说。

我开发类似于用于帐目常见的例子例的标签式图。当涉及到显示的情况下注释,你不能只是把它们放入一个相关列表,而是你需要手工格式化。使用中不能由用户,所以我们必须做更多的手工编码读取紧密压缩表中的标准顶点pageBlockTable结果。这种方法还允许我使用CSS来格式化表格内容。但问题是与格式格式化换行和电子邮件的案例点评。 TehNrd的回答非常完美!

有关他人这里是与动作到编辑注释连同格式化CaseComment来显示标签的代码。

<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>

您是否尝试过使用的outputText?

如果不起作用投票给我的想法在这里: https://开头sites.secure.force.com/ideaexchange/ideaView?id=08730000000H4XDAA0 正如我试图JSON返回页面时同样的问题。

其他人也希望这样的想法 https://sites.secure .force.com / ideaexchange /顶点/ ideaview?ID = 08730000000BrhEAAS

对于我来说,TehNrd钉它 - 我想显示在VisualForce通知电子邮件模板案“说明”,所有的CR离子/ LF消失,行/段落都拿到一起运行。使它成为一个OutputField值完全固定它。

您可以尝试是这样的:

{!substitute(Case.Description, '\n', '<br/>')}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top