We have a requirement to strike through SharePoint 2013 document
item based on its status "Obsolete". If the obsolete is "yes", the column named "Part Number" should be strike through and highlighted as red as in the below screenshot.
The easiest way to
implement this is to use SharePoint 2013 new feature JS Link. There are several
good JS Link references to highlight
a SharePoint list row and color
list item with different color. You could refer my previous blog to
configure the JS Link. If you add the following javascript as JS Link named Obsolete.js to the display
web part, you will get the desired result.
(function () {
// Create object that have the context information about the field that we want to change it's output render
var priorityFiledContext = {};
priorityFiledContext.Templates = {};
priorityFiledContext.Templates.Fields = {
// Apply the new rendering for Obsolate field on List View
"Part_x0020_Number": { "View": obsolateFiledTemplate }
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(priorityFiledContext);
})();
// This function provides the rendering logic for list view
function obsolateFiledTemplate(ctx) {
var title = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
var obsolate = ctx.CurrentItem.Obsolate;
console.log(obsolate);
// Return html element with appropriate color based on priority value
if (obsolate == "Yes")
{
return "<span style='color :#f00'>" + "<del>" + title + "</del>" + "</span>";
}
else{
return "<span style='color :#000000'>" + title + "</span>";
}
}
The following script will highlight the item to red when obsolete value is "Yes".
(function () {
var overrideCtx = {};
overrideCtx.Templates = {};
overrideCtx.OnPostRender = {
HighlightRowOverride
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();
function HighlightRowOverride(ctx) {
for (var i=0; i< ctx.ListData.Row.length; ++i){
var lid = GenerateIIDFromListItem(ctx, listItem);
var row = document.getElementById(iid);
if (listItem.Obsolate == "Yes") {
row.style.backgroundColor = "rgba(255, 0, 0, 0.5)";
}
}
ctx.skipNextAnimation = true;
}
You will see this implementation is very flexible and we could use this for many other different implementations.
If you need to strike through the Document Name field, you would look at the field using view source. Here is the field definition.
{"Name": LinkFilename",
"FieldType": "Computed",
"RealFieldName": "FileLeafRef",
"DisplayName": "Name",
"ID": "5cc6dc79-3710-4374-b433-61cb4a686c12",
"ClassInfo": "Menu",
"Type": "Computed",
"Filterable": "FALSE",
"listItemMenu": "TRUE",
"CalloutMenu": "TRUE",
"AllowGridEditing": "TRUE"}
You can change the two line of the javascript.
priorityFiledContext.Templates.Fields = {
"LinkFilename": { "View": obsolateFiledTemplate }
};
var title = ctx.CurrentItem.FileLeafRef;
Please note that you could use powershell or server side API to automatically set the JSLink to the webpart if there are many lists or libraries you need to configure.
(function () {
var overrideCtx = {};
overrideCtx.Templates = {};
overrideCtx.OnPostRender = {
HighlightRowOverride
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();
function HighlightRowOverride(ctx) {
for (var i=0; i< ctx.ListData.Row.length; ++i){
var lid = GenerateIIDFromListItem(ctx, listItem);
var row = document.getElementById(iid);
if (listItem.Obsolate == "Yes") {
row.style.backgroundColor = "rgba(255, 0, 0, 0.5)";
}
}
ctx.skipNextAnimation = true;
}
You will see this implementation is very flexible and we could use this for many other different implementations.
If you need to strike through the Document Name field, you would look at the field using view source. Here is the field definition.
{"Name": LinkFilename",
"FieldType": "Computed",
"RealFieldName": "FileLeafRef",
"DisplayName": "Name",
"ID": "5cc6dc79-3710-4374-b433-61cb4a686c12",
"ClassInfo": "Menu",
"Type": "Computed",
"Filterable": "FALSE",
"listItemMenu": "TRUE",
"CalloutMenu": "TRUE",
"AllowGridEditing": "TRUE"}
You can change the two line of the javascript.
priorityFiledContext.Templates.Fields = {
"LinkFilename": { "View": obsolateFiledTemplate }
};
var title = ctx.CurrentItem.FileLeafRef;
Please note that you could use powershell or server side API to automatically set the JSLink to the webpart if there are many lists or libraries you need to configure.