文本区域上的换行符

Pra*_*ady 7 salesforce visualforce apex-code

我有一个名为Current_Address__c的自定义字段,其数据类型为textarea.

我需要以下面的格式填充此字段.即街道后面的换行符和拉链后的另一个换行符.

街道城市州拉链国家

城市州拉链国家等的价值取自联系对象.我不想将它用作公式字段.所以我需要在我的控制器中填充它并将其显示在我的VF页面上.

我试图通过使用下面的代码添加换行符char

this.customobj.Current_Address__c = currentStreet + '\\n ' + currentCity + ' ' + currentState  + ' ' + currentZIP  + '\\n ' + currentCountry ;
Run Code Online (Sandbox Code Playgroud)

我也使用\n而不是\n.

它仍然显示一行中的字段而不是3行

编辑

我使用以下代码完成了这项工作.我会接受mathews的答案,因为它适用于outputfield.

                currentAddress = currentStreet;
            currentAddress += '\r\n';
            currentAddress += currentCity + + ' ' + currentState  + ' ' + currentZIP ;
            currentAddress += '\r\n';
            currentAddress += currentCountry;
Run Code Online (Sandbox Code Playgroud)

仅当您使用+ =时才有效.不知道为什么会这样

Mat*_*t K 8

我想我发现了这个问题,你有两个转义字符斜杠(\\n),但只需要一个,因为斜杠输入\n不需要在这个上下文中转义.

此外,Salesforce还将新行保存为\r\n.试试这个:

this.customobj.Current_Address__c 
    = currentStreet + ' \r\n' 
    + currentCity + ' ' + currentState  + ' ' + currentZIP  + ' \r\n' 
    + currentCountry;
Run Code Online (Sandbox Code Playgroud)

使用<apex:outputfield>带有sObject字段的方法时,此方法有效.

<apex:outputtext value="{!myCustomSObject__c.Address__c}"/>
Run Code Online (Sandbox Code Playgroud)

如果您使用的是其他Visualforce组件,则无法使用.使用<apex:outputtext>Component时,Visualforce在HTML中呈现新行,但HTML忽略新行.如果您使用<br/>标记,Visualforce会将其呈现为&lt;br/&gt;.

我可以想出的最好的解决方案是渲染一个包含新行的变量(而不是sObject字段)是使用禁用的<apex:inputtextarea>.

<apex:inputtextarea value="{!myAddress}" disabled="true" readonly="true">
</apex:inputtextarea>
Run Code Online (Sandbox Code Playgroud)

  • Mathew ..我在发布之前尝试了类似的代码.我用/ r/n和单/.它没有出来正确.这一切都是正确的.正如raymOnd所使用的那样我也在使用outputtext.我终于编写了使用outputtext的代码.我正在编辑我的quesstion以发布我已完成的代码 (2认同)