Salesforce 文本区域上的换行符
我有一个名为Current_Address___c的自定义字段,它的数据类型为textarea 我需要按以下格式填充此字段。街后换行,邮编后换行 街头 城市州邮政编码 国家 城市、州、邮编、国家等的值取自contact对象。我不想将其用作公式字段。所以我需要在我的控制器中填充它,并在我的VF页面上显示它 我正在尝试使用下面的代码添加换行符Salesforce 文本区域上的换行符,salesforce,apex-code,visualforce,Salesforce,Apex Code,Visualforce,我有一个名为Current_Address___c的自定义字段,它的数据类型为textarea 我需要按以下格式填充此字段。街后换行,邮编后换行 街头 城市州邮政编码 国家 城市、州、邮编、国家等的值取自contact对象。我不想将其用作公式字段。所以我需要在我的控制器中填充它,并在我的VF页面上显示它 我正在尝试使用下面的代码添加换行符 this.customobj.Current_Address__c = currentStreet + '\\n ' + currentCity + ' '
this.customobj.Current_Address__c = currentStreet + '\\n ' + currentCity + ' ' + currentState + ' ' + currentZIP + '\\n ' + currentCountry ;
我还使用了\n而不是\n
它仍然在一行而不是三行中显示字段
编辑
我使用下面的代码实现了这一点。我会接受马修斯的答案,因为它将与outputfield一起工作
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;
将
与sObject字段一起使用时,此方法有效
<apex:outputtext value="{!myCustomSObject__c.Address__c}"/>
最近我遇到了同样的问题,我想重新设计新的生产线 我发现的解决方案是这样的,虽然有点棘手,但它是有效的:
<apex:outputText value="{!SUBSTITUTE(JSENCODE(textVariableThanContainsNewLines), '\\n', '<br/>')}" escape="false"/>
试试这个:
this.customobj.Current_Address__c
= currentStreet + ' \r\n'
+ currentCity + ' ' + currentState + ' ' + currentZIP + ' \r\n'
+ currentCountry;
控制器
public List<String> getLetterLines() {
if (letterBody == null) {
return new List<String>();
}
return letterBody.split('\n');
}
public List getLetterLines(){
如果(正文==null){
返回新列表();
}
返回信体。拆分('\n');
}
VF页面:
<apex:repeat value="{!letterLines}" var="letterLine">
<apex:outputText value="{!letterLine}" /><br />
</apex:repeat>
玩得开心 value=“备注:{!SUBSTITUTE(JSENCODE(textVariableThanContainesNewlines),'\r\n','
')}”我也有同样的问题。我试过\r\n、\n、\\n甚至
但都没有成功!您使用什么类型的标记来显示数据<代码>适合我。我不能使用outputfield,因为我做动态绑定。我得到的这个错误值不是动态绑定!我编辑了我的答案,请看上面<代码>除非与sObject字段一起使用,否则将不起作用。。我在这里发布之前也尝试过类似的代码。我使用了/r/n和single/。结果不正确。一切都是正确的。正如raymOnd所使用的,我也在使用outputtext。我终于在outputtext上编写了代码。我正在编辑我的问题,以发布我在回答之前需要考虑的代码:1)你的答案应该在帖子中添加新的信息,而不仅仅是重复别人的信息,2)你应该解释为什么/如何解决问题,3)你应该很好地格式化你的答案(将代码放入代码块)。