This is tested on WSS 3.0.
I have a custom list definition. There is a field called "Status" in this list definition. I want to change the display color of this field based on its value. Here is the steps:
In the schema.xml file of this list, find the "Status" field definition. It should be inside /List/MetaData/Fields. Based on this field, create another Computed type field. Here is the code snippet:
<Field Type="Text" DisplayName="Status" ReadOnly="TRUE" Required="TRUE" MaxLength="255" ID="{d9daabac-0014-4c1e-8c06-093ff0c7ecfc}" SourceID="{142fdfdd-7e46-4ca3-b9d3-56ae0ead646e}" StaticName="Status" Name="Status" ColName="nvarchar3" RowOrdinal="0" />
<Field ID="{0864C5F6-688C-4ee5-A4FE-3FB17CD5EE1E}" ReadOnly="TRUE" Type="Computed" Name="ColorStatus" DisplayName="Status" Dir="" DisplayNameSrcField="Status" EnableLookup="TRUE" StaticName="ColorStatus">
<FieldRefs>
<FieldRef Name="Status" />
</FieldRefs>
<DisplayPattern>
<IfEqual>
<Expr1>
<Field Name="Status" />
</Expr1>
<Expr2>
<![CDATA[OK]]>
</Expr2>
<Then>
<HTML><![CDATA[<span >]]></HTML>
<Column HTMLEncode="TRUE" Name="Status" />
<HTML><![CDATA[</span>]]></HTML>
</Then>
<Else>
<HTML><![CDATA[<span >]]></HTML>
<Column HTMLEncode="TRUE" Name="Status" />
<HTML><![CDATA[</span>]]></HTML>
</Else>
</IfEqual>
</DisplayPattern>
</Field>
The first field is the original Status field, the second field is the Computed field. If the value of the Status field is "OK", it render it as green color, otherwise render it as red.
Now you get the fields definition done, the next step you need to do is the reference this field in your view. For example, in the All Items view, reference the newly defined ColorStatus as following:
<View Type="HTML" DisplayName="All Items" Url="AllItems.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/images/generic.png" WebPartZoneID="Main">
<ViewFields>
<FieldRef Name="LinkTitle" />
<FieldRef Name="SupplierCode" />
<FieldRef Name="StartDate" />
<FieldRef Name="EndDate" />
<FieldRef Name="PreferenceCriterion" />
<FieldRef Name="Producer" />
<FieldRef Name="CountryOfOrigin" />
<FieldRef Name="NetCost" />
<FieldRef Name="ColorStatus" />
</ViewFields>
......
</View>
Now, reset IIS and refresh the All Items view, the color will change.
I have a custom list definition. There is a field called "Status" in this list definition. I want to change the display color of this field based on its value. Here is the steps:
In the schema.xml file of this list, find the "Status" field definition. It should be inside /List/MetaData/Fields. Based on this field, create another Computed type field. Here is the code snippet:
<Field Type="Text" DisplayName="Status" ReadOnly="TRUE" Required="TRUE" MaxLength="255" ID="{d9daabac-0014-4c1e-8c06-093ff0c7ecfc}" SourceID="{142fdfdd-7e46-4ca3-b9d3-56ae0ead646e}" StaticName="Status" Name="Status" ColName="nvarchar3" RowOrdinal="0" />
<Field ID="{0864C5F6-688C-4ee5-A4FE-3FB17CD5EE1E}" ReadOnly="TRUE" Type="Computed" Name="ColorStatus" DisplayName="Status" Dir="" DisplayNameSrcField="Status" EnableLookup="TRUE" StaticName="ColorStatus">
<FieldRefs>
<FieldRef Name="Status" />
</FieldRefs>
<DisplayPattern>
<IfEqual>
<Expr1>
<Field Name="Status" />
</Expr1>
<Expr2>
<![CDATA[OK]]>
</Expr2>
<Then>
<HTML><![CDATA[<span >]]></HTML>
<Column HTMLEncode="TRUE" Name="Status" />
<HTML><![CDATA[</span>]]></HTML>
</Then>
<Else>
<HTML><![CDATA[<span >]]></HTML>
<Column HTMLEncode="TRUE" Name="Status" />
<HTML><![CDATA[</span>]]></HTML>
</Else>
</IfEqual>
</DisplayPattern>
</Field>
The first field is the original Status field, the second field is the Computed field. If the value of the Status field is "OK", it render it as green color, otherwise render it as red.
Now you get the fields definition done, the next step you need to do is the reference this field in your view. For example, in the All Items view, reference the newly defined ColorStatus as following:
<View Type="HTML" DisplayName="All Items" Url="AllItems.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/images/generic.png" WebPartZoneID="Main">
<ViewFields>
<FieldRef Name="LinkTitle" />
<FieldRef Name="SupplierCode" />
<FieldRef Name="StartDate" />
<FieldRef Name="EndDate" />
<FieldRef Name="PreferenceCriterion" />
<FieldRef Name="Producer" />
<FieldRef Name="CountryOfOrigin" />
<FieldRef Name="NetCost" />
<FieldRef Name="ColorStatus" />
</ViewFields>
......
</View>
Now, reset IIS and refresh the All Items view, the color will change.
Comments