Starting from this example:
https://code.msdn.microsoft.com/office/Client-side-rendering-code-0a786cdd
I would like to set the entire row background color depending on a column value.
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityStarting from this example:
https://code.msdn.microsoft.com/office/Client-side-rendering-code-0a786cdd
I would like to set the entire row background color depending on a column value.
Below example demonstrates how to customize List View in order to highlight list item rows.
Assume there is a Tasks
list and depending on status, rows have to be highlighted using the corresponding color.
Template code:
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {
SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
OnPostRender: function(ctx) {
var statusColors = {
'Not Started' : '#FFF1AD',
'In Progress' : '#FFD800',
'Completed' : '#01DF3A'
};
var rows = ctx.ListData.Row;
for (var i=0;i<rows.length;i++)
{
var status = rows[i]["Status"];
var rowId = GenerateIIDForListItem(ctx, rows[i]);
var row = document.getElementById(rowId);
row.style.backgroundColor = statusColors[status];
}
}
});
});
There are at least two options how to apply the changes:
Script Editor
/Content Editor
web partsHere is how to apply the changes using the second option:
Script Editor
webpart right below the list view web part.script
tag code into the Script Editor, for example: <script type="text/javascript">{Template JS code goes here}</script>
You can do this by handling OnPostRender
event. Something like
priorityFiledContext.OnPostRender = [
ApplyRowOverride
];
function ApplyRowOverride(ctx) {
for(var index = 0; index < ctx.ListData.Row.Length; index++) {
var item = ctx.ListData.Row[index];
if(item.Field == "WhateverValue") {
var row = document.getElementById(GenerateIIDForListItem(ctx, item));
if(row != null) {
row.style.backgroundcolor = "red";
}
}
}
}