How To customize a field's display |
User data is displayed in various situations: forms, form gridviews, navigation, Office files, etc.
Consequently we find three types of display:
Display text (Office files, SMS, or any non-html media)
Display HTML (gridviews, navigation)
Control (forms, inline edition)
This article focuses on the first two, controls customization is detailed here.
This topic contains the following sections:
We consider the following example, listing static apnea records with their respective duration.
Our objective is to change that duration display without affecting the stored values.
First, you need to open the HtmlHelper corresponding to your content-type ([Generated_Project]\Forms\Html Helpers\Record_Helper.cs).
Then override the GetDisplayText method like this:
protected override string GetDisplayText(PFField field, PFFieldControlMode mode, object fieldValue) { if (field.Name == Record.FieldName_Duration && mode != PFFieldControlMode.Hidden) return fieldValue.GetString().Replace(":", "min ") + "s"; else return base.GetDisplayText(field, mode, fieldValue); }
Once deployed, we can export the view to Excel and see the following:
Again, you need to open the HtmlHelper corresponding to your content-type ([Generated_Project]\Forms\Html Helpers\Record_Helper.cs).
Then override the GetDisplayHtml method like this:
protected override string GetDisplayHtml(PFField field, PFFieldControlMode mode, object fieldValue, string sourceUrl = null) { if (field.Name == Record.FieldName_Duration && mode != PFFieldControlMode.Hidden) return "<b>" + fieldValue.GetString().Replace(":", "min</b> ") + "s"; else return base.GetDisplayHtml(field, mode, fieldValue, sourceUrl); }
When loading the view again, we get this result:
You can also override the HTML to include the value of another field.
Consider a new (choice) field "Nationality" (targeting the Country content-type) has been added to the content-type Record, and we want to be see its value without changing the view.
We can call the GetDisplayHtml for that field:
protected override string GetDisplayHtml(PFField field, PFFieldControlMode mode, object fieldValue, string sourceUrl = null) { if (mode != PFFieldControlMode.Hidden) { if (field.Name == Record.FieldName_Duration) return "<b>" + fieldValue.GetString().Replace(":", "min</b> ") + "s"; else if (field.Name == Record.FieldName_Name) { //Get nationality field and value PFField nationalityField = CurrentObject.ParentContentType.PFField_Nationality; PFFieldChoiceValue nationality = CurrentObject.__Nationality; //Return HTML return String.Format("{0}<br />{1}", base.GetDisplayHtml(field, mode, fieldValue, sourceUrl), base.GetDisplayHtml(nationalityField, mode, nationality, sourceUrl)); } } return base.GetDisplayHtml(field, mode, fieldValue, sourceUrl); }
Here is the result:
Note |
---|
Packflow is only loading the values displayed in the view, plus a few system fields. To display a value from a field not included in the view, you must force its load checking the "Force load on views" in the modeling. Else, its value will not be available in the data collection of the current item. |