2010-03-26 126 views
5

在Salesforce中,如果我將文本字段綁定到VisualForce頁面,那麼將文本字段中的回車轉換爲HTML格式的好方法是什麼<br/>標籤?VisualForce:在長文本字段中將回車符轉換爲html換行符

例如從這樣的開始:

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

...如果描述很長,有很多回車的,我怎麼HTML-IFY呢?

(我想這是一個很簡單的問題,我敢肯定,我可以谷歌,但要獲得Salesforce的社區會在這裏我想我們需要一些簡單的問題。)

編輯:(賞金加入到嘗試和產生一些刺激)

回答

4

試試這個:

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

使用輸出字段將保持自動地格式化。

+0

這對我不起作用。當我使用outputField時,回車符不會被轉換。 – codeulike 2010-04-05 10:28:00

+0

但它似乎適用於其他人! – codeulike 2011-06-13 09:07:35

3

我最終與一些長篇大論的代碼來實現這一點。

在自定義控制,添加方法與<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()也會替換任何<>字符。

(有可能是一個更好的辦法,以使字符串安全,建議表示歡迎)

0

你可以嘗試這樣的:

{!substitute(Case.Description, '\n', '<br/>')} 
0

對於我來說,TehNrd釘它 - 我想顯示VisualForce通知電子郵件模板中的案例「描述」,並且所有CR/LF消失,並且行/段落一起運行。使其成爲OutputField值完全解決了它。

1

上面的TehNrd回答了我的問題。

我正在開發類似於帳戶常見示例的選項卡式視圖。說到案例評論時,你不能把它們放到相關列表中,而是需要手工格式化它們。使用標準apex 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> 
+0

更新:僅噹噹前用戶擁有案例評論時,編輯操作才起作用。我正在研究解決方案並將進行更新。 – Bryan 2011-06-16 01:19:01