I'm trying to write a custom field that is representing the time spend on the task. The field derives from NumberField (number is representing minutes) but I want to display it on the list as HH:MM for that purpose I've tried to override the fallowing function:
protected override void RenderFieldForDisplay(System.Web.UI.HtmlTextWriter output)
{
Label timeSpan = new Label();
timeSpan.Text = ((int)this.Value / 60).ToString() + ":" + ((int)this.Value % 60).ToString();
timeSpan.RenderControl(output);
//base.RenderFieldForDisplay(timeSpan.RenderControl());
}
I'm not an ASP.NET developer so I'm trying to avoid defining DisplayTemplate.
Can you show me the way how to render it programmatically or just push me in the right direction?
Solution
Solved with help from Kusek answer. In fldtypes_HourField.xml:
<RenderPattern Name="DisplayPattern">
<HTML><![CDATA[<div align='right'>]]></HTML>
<Switch>
<Expr>
<Column />
</Expr>
<Case Value="" />
<Default>
<HTML><![CDATA[<script src="/_layouts/hourField.js"></script>]]></HTML>
<HTML><![CDATA[<div><SCRIPT>formatHourField("]]></HTML>
<Column />
<HTML><![CDATA[");</SCRIPT></div>]]></HTML>
</Default>
</Switch>
</RenderPattern>
And the hourField.js
function formatHourField(t) {
var m = t % 60;
var h = (t - m)/ 60;
document.write(h + ":" + m);
}
Having in mind what I've tried before - this solution looks beautifully simple :)
The value that is rendered in the All Items view is not based on the Template or control. It is rendered from the
Tag of the Field Schema that you have defined. Display Template Defines the look of how it is being displayed in the Disp Form. Hope this helps.
It will be bit trick to get your format in the CAML Representation . You can get some infomration about the subject here