我有一个名为 Current_Address__c 的自定义字段,它的数据类型为 textarea。
我需要以下面的格式填充此字段。即街道后的换行符和 zip 后的另一个换行符。
街道
邮政编码
国家
城市国家邮政编码国家等的值取自联系对象。我不想将其用作公式字段。所以我需要在我的 Controller 中填充它并在我的 VF 页面上显示它。
我正在尝试使用下面的代码添加换行符
this.customobj.Current_Address__c = currentStreet + '\\n ' + currentCity + ' ' + currentState + ' ' + currentZIP + '\\n ' + currentCountry ;
我也使用了\n 而不是\n。
它仍然在一行而不是 3 行中显示该字段
编辑
我使用以下代码完成了这项工作。我会接受 mathews 的答案,因为它可以与输出字段一起使用。
currentAddress = currentStreet;
currentAddress += '\r\n';
currentAddress += currentCity + + ' ' + currentState + ' ' + currentZIP ;
currentAddress += '\r\n';
currentAddress += currentCountry;
这仅在您使用 += 时有效。
不知道为什么会这样
最佳答案
我想我发现了问题,您有两个转义字符斜杠( \\n
),但只需要一个,因为 \n
中的斜杠在这种情况下不需要转义。
此外,Salesforce 将新行另存为 \r\n
.尝试这个:
this.customobj.Current_Address__c
= currentStreet + ' \r\n'
+ currentCity + ' ' + currentState + ' ' + currentZIP + ' \r\n'
+ currentCountry;
此方法在使用 时有效
<apex:outputfield>
带有 sObject 字段。<apex:outputtext value="{!myCustomSObject__c.Address__c}"/>
如果您使用不同的 Visualforce 组件,它将不起作用。使用
<apex:outputtext>
时,Visualforce 在 HTML 中呈现新行组件,但 HTML 忽略新行。如果您使用 <br/>
标记,Visualforce 将其呈现为 <br/>
.我可以想出的最佳解决方案是使用禁用的
<apex:inputtextarea>
来呈现其中包含新行的变量(而不是 sObject 字段) .<apex:inputtextarea value="{!myAddress}" disabled="true" readonly="true">
</apex:inputtextarea>
关于salesforce - 文本区域上的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10083472/