How to change the background color of a cell in a SharePoint Datasheet View based on a condition. For example -say I have fields Status = Pending, Due Date = 12/2/2021, Type = Internal
Color the background for Status for this row only based on the due data and also the type value. One row the status cell might be red, another might be green. Not just the text but the Background color.
2) How to change dynamically the font based on the same.
Environment
SharePoint 2016 on prem, NOT modern, powerapps, etc.
Javascript solutions fine -thanks
Type.registerNamespace('EE')
EE.Templates = EE.Templates || {}
EE.Functions = EE.Functions || {}
EE.Functions.AddJQuery = function (ctx) {
// check if jQuery is available, otherwise reload it
if (!window.jQuery) {
var jq = document.createElement('script');
jq.type = 'text/javascript';
jq.src = _spPageContextInfo.webAbsoluteUrl + '/SiteAssets/jquery-3.6.0.js';
document.getElementsByTagName('head')[0].appendChild(jq);
}
// Verify that custom CSS class is loaded from Site Assets
var cssId = 'eeDsv';
if (!document.getElementById(cssId))
{
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = cssId;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = _spPageContextInfo.webAbsoluteUrl + '/SiteAssets/eedsv.css';
link.media = 'all';
head.appendChild(link);
}
}
EE.Functions.InitiateScopeList = function () {
//Add jqery and links
EE.Functions.AddJQuery(ctx);
}
EE.Functions.ChangeFieldColor = function () {
jQuery(".redback").closest('td').addClass('tdredback');
jQuery(".unused").closest('td').removeClass('tdredback');
}
EE.OnPreRender = EE.Functions.InitiateScopeList;
EE.OnPostRender = EE.Functions.ChangeFieldColor;
EE.Functions.OverrideStatusField = function (ctx) {
var status = ctx.CurrentItem.Status;
var dueDate = new Date(ctx.CurrentItem.Due_x0020_Date);
var itemType = ctx.CurrentItem.TaskType;
var today = new Date();
var dummyClass = "unused"
if (status == 'Pending' && itemType == 'Internal' && dueDate < today)
{
dummyClass = "redback";
}
return "<span class='" + dummyClass + "'>" + status + "</span>";
}
EE.Templates.Fields = {
'Status': { 'View': EE.Functions.OverrideStatusField }
};
/*
This function has been used to register the namespace as per Minimal Download Strategy(MDS)
*/
EE.Functions.RegisterField = function () {
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(EE)
}
/*
This function has been used to register the JS file as per Minimal Download Strategy(MDS)
*/
EE.Functions.MdsRegisterField = function () {
var thisUrl = _spPageContextInfo.webServerRelativeUrl + '/SiteAssets/ee_CustomBackgroundDsv.js';
EE.Functions.RegisterField();
RegisterModuleInit(thisUrl, EE.Functions.RegisterField)
}
/*
This code block used to find Minimal Download Strategy(MDS) enabled on the site or not
>on the basis of that it's registring the JS file
*/
if (typeof _spPageContextInfo != "undefined" && _spPageContextInfo != null) {
//-- MDS enabled on our site
EE.Functions.MdsRegisterField()
} else {
//-- MDS no enabled on our site
EE.Functions.RegisterField()
}
afaik you have two different options:
1. Using JSLink
2. Using custom Javascript via Script Editor on the view page
With JSLink you can overwrite how the content of the cell is written. The below sample will set the span background color (but limited to the span, not the whole cell). Benefit here is, that you have direct access to the item values (the code is run for each item).
Open in new window
Just save the above script (after adjusting the column names to your list internal names (!!!)), upload it into your site assets library (e.g. MyCustomBackground.js) and thenWith custom JavaScript (e.g. via jQuery) you can easily set the background colors of the cells BUT it will be somehow harder to get the item values for each row.
HTH
Rainer