I am working on a SharePoint Online 2016 site collection and need to add conditional formatting to a few drop down fields. The drop down options are: 1: Red, 2: Amber, 3: Green. I want the field in the view to change according to the colour selected But i cannot seem to get it working.
NOTE: I have tried using html formatting and that doesn't work. Also, i am aware that you can change the data return type to number but i need this to remain as a single line of text due to SSIS issues.
I have constructed the following code:
(function () {
// Create object that have the context information about the field that we want to change it's output render
var StakeholderStatusFiledContext = {};
StakeholderStatusFiledContext.Templates = {};
StakeholderStatusFiledContext.Templates.Fields = {
// Apply the new rendering for StakeholderStatus field on List View
"StakeholderStatus": { "View": StakeholderStatusFiledTemplate }
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(StakeholderStatusFiledContext);})();
// This function provides the rendering logic for list view
function StakeholderStatusFiledTemplate(ctx) {
var StakeholderStatus = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
// Return html element with appropriate color based on StakeholderStatus value
switch (StakeholderStatus) {
case '1) Red':
return "<span style='color :#f00'>" + StakeholderStatus + "</span>";
break;
case '2) Amber':
return "<span style='color :#ff6a00'>" + StakeholderStatus + "</span>";
break;
case '3) Green':
return "<span style='color :#cab023'>" + StakeholderStatus + "</span>";
}
}
The script has been uploaded to master pages and the link has been inputted to the web part via miscellaneous tab. When i refresh the page, it doesn't seem to do anything which has got me thinking that either my script is incorrect or it has been added to the wrong section. Any ideas?
This is what i am trying to replicate