VisualForce:将长文本字段中的回车符转换为 html 换行符
在 Salesforce 中,如果我将文本字段绑定到 VisualForce 页面,将文本字段中的回车符转换为 HTML
标记的好方法是什么?
例如,从这样的内容开始:
<apex:page standardController="Case">
<apex:pageBlock title="Test">
<p>{!case.Description}</p>
</apex:pageBlock>
<apex:detail relatedList="false" />
</apex:page>
...如果描述很长并且有很多回车符,我如何对其进行 HTML 化?
(我想这是一个相当简单的问题,我确信我可以用 google 搜索它,但为了让 Salesforce 社区在这里继续下去,我认为我们需要一些简单的问题。)
编辑:(添加赏金是为了尝试引起一些兴奋)
In Salesforce, if I'm binding a text field into a VisualForce page, whats a good way to convert the carriage returns in the text-field into HTML <br/>
tags?
e.g. starting from something like this:
<apex:page standardController="Case">
<apex:pageBlock title="Test">
<p>{!case.Description}</p>
</apex:pageBlock>
<apex:detail relatedList="false" />
</apex:page>
... if the Description is long with lots of carriage returns, how do I HTML-ify it?
(I guess this is a fairly easy question, and I'm sure I could google it, but to get the Salesforce community going on here I figure we need a few easy questions.)
edit: (Bounty added to try and generate some excitement)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个:
使用输出字段将自动保持格式。
Try this:
Using output fields will maintain formatting automagically.
我最终通过一些冗长的代码实现了这一点。
在自定义控制器中,添加手动搜索后返回字段的方法,并替换字段中的换行符并将其替换为
标签:然后在页面中,使用 apex:outputText with escape="false":
请注意,escape="false" 对于防止 VisualForce 转义 html 标记是必要的。这也意味着您可能会受到假设嵌入数据中的脚本攻击的影响。这就是为什么控制器中的
lineBreaks()
fn 也会替换任何<
和>
字符。(可能有更好的方法使字符串安全,欢迎建议)
I eventually achieved this with some long winded code.
In the custom controller, add methods to return the field after manually searching and replace the line breaks in the field and replacing them with
<br/>
tags:Then in the page, use apex:outputText with escape="false":
Note that escape="false" is necessary to prevent VisualForce from escaping the html tags. This also means you leave yourself open to scripting-attacks that could by hypothetically embedded in the data. Thats why the
lineBreaks()
fn in the controller also replaces any<
and>
characters.(There may be a better way to make the string safe, suggestions welcome)
上面的 TehNrd 回答了我的问题。
我正在开发案例的选项卡式视图,类似于帐户的常见示例。在显示案例评论时,您不能只是将它们放入相关列表中,而是需要手动设置它们的格式。使用标准的 apex pageBlockTable 会产生一个紧密包装的表,用户无法读取该表,因此我们必须进行更多的手动编码。这种方法还允许我使用 CSS 来格式化表格内容。但问题是用换行符格式化案例评论并格式化电子邮件。 TehNrd 的回答非常有效!
对于其他人,这里是显示带有格式化 CaseComment 的选项卡以及编辑评论的操作的代码。
TehNrd above answered the question for me.
I am developing a tabbed view of Cases similar to the common example for Accounts. When it comes to showing the case comments you can not just put them into a related list and instead you need to format them by hand. Using the standard apex pageBlockTable results in a tightly packed table that can not be read by users so we have to do more hand coding. This approach also allows me to use CSS to format the table contents. But the problem was formatting the Case Comments with line breaks and email messages formatted. TehNrd's answer worked perfectly!
For others here is the code to display a tab with formatted CaseComment along with an action to edit the comment.
您尝试过使用outputText吗?
如果这不起作用,请在这里投票支持我的想法: https://sites.secure .force.com/ideaexchange/ideaView?id=08730000000H4XDAA0
因为我在尝试将 JSON 返回到页面时遇到了同样的问题。
其他人也想要这个想法 https://sites.secure.force。 com/ideaexchange/apex/ideaview?id=08730000000BrhEAAS
Have you tried using outputText?
IF that does not work vote for my idea here: https://sites.secure.force.com/ideaexchange/ideaView?id=08730000000H4XDAA0
As I have the same issue when trying to return JSON to a page.
Other people also want this idea https://sites.secure.force.com/ideaexchange/apex/ideaview?id=08730000000BrhEAAS
对我来说,TehNrd 成功了——我试图在 VisualForce 通知电子邮件模板中显示案例“描述”,所有 CR/LF 都消失了,并且行/段落一起运行。使其成为 OutputField 值完全解决了这个问题。
For me, TehNrd nailed it -- I was trying to display a Case "Description" in a VisualForce notification e-mail template, and all the CR/LFs disappeared and the lines / paragraphs were getting run together. Making it an OutputField value totally fixed it.
你可以尝试这样的事情:
You could try something like: