E 'possibile formattare un campo numerico in un layout di pagina?
-
09-09-2019 - |
Domanda
sto sviluppando un sito di pubblicazione di SharePoint e la creazione dei suoi tipi di contenuto e layout di pagina. Ho bisogno di visualizzare il valore di un campo Anno di tipo Number. Il markup attualmente è:
<SharePointWebControls:NumberField FieldName="Year" runat="server" id="Year" />
Il problema con il comportamento predefinito è che mostra ogni numero con una virgola, ad esempio "2009" invece di "2009". C'è un modo per impostare una sorta di sintassi String.Format sul campo per renderlo viene visualizzata correttamente?
Ho cercato di creare un nuovo modello di rendering che assomiglia a questo:
<SharePoint:RenderingTemplate ID="YearNumberField" runat="server">
<Template>
<SharePoint:FormField ID="TextField" runat="server"/>
</Template>
</SharePoint:RenderingTemplate>
... ma non sembra essere qualsiasi proprietà 'Format' sull'oggetto FormField.
Grazie per qualsiasi aiuto.
Aggiornamento:
Ho provato avvolgendo la SharePoint: tag FormField all'interno di SharePoint: FormattedString. Purtroppo il campo non è stato formattato, stessi risultati come questa domanda .
Soluzione
The issue is that the rendering template must use FormField. This always renders the value in the format: 1,989 . To resolve this the rendered text needs to be trapped and altered to get the desired output. Here are two approaches to resolving this:
1. Write a custom control inherited from NumberField
The RenderFieldForDisplay and RenderFieldForInput methods can be overridden to provide the desired output. Additional properties can be added to the control to describe additional behaviour.
Pros: No changes to rendering templates required.
2. Write a custom control for use in the rendering template
A control that (for example) uses regular expressions to alter text can wrap around the FormField control.
<SharePoint:RenderingTemplate ID="YearField" runat="server">
<Template>
<RX:RegexManipulatorControl runat="server"
Mode="Replace"
Expression=","
Replacement="">
<SharePoint:FormField runat="server"/>
</RX:RegexManipulatorControl>
</Template>
</SharePoint:RenderingTemplate>
Pros: Generic solution can be used for any type of field.
Altri suggerimenti
from Just Another SharePoint Blog
Open the list view in SharePoint Designer.
Right click on the data view web part. (the list)
Select Convert to XSLT Data View
Click on the number field you would like to format
A > will appear showing Data Field, Format As
Click on the link below Format As - Number formatting options
Under Options deselect Use 1000 separator
Click OK
Save your changes and hit F12 to preview