0

I have a custom list which displays numerical variance as a percentage. I would like to color code the numerical value based on it's value. i.e. 10% displays in red.

I am using SharePoint 2016 on-premise with the classic experience enabled.

I tried JSON, however it did not work. Any help would be appreciated.

Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61
Daryl
  • 127
  • 1
  • 9

2 Answers2

0

JSON formatting will not work in SharePoint 2016 classic experience.

For your requirement, you have to use Client Side Rendering in SharePoint.

Try something like below:

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {
SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
  OnPostRender: function(ctx) {
     var rows = ctx.ListData.Row;
     for (var i = 0; i < rows.length; i++) {
         var numVariance = rows[i]["Variance"]; // use internal name of your number column here
         var rowId = GenerateIIDForListItem(ctx, rows[i]);
         var row = document.getElementById(rowId);
     // Add your conditions &amp; color formatting here like
     if(numVariance &lt;= 10) {
         row.style.backgroundColor = &quot;#FF0000&quot;; //Red color
     } else if (numVariance &lt;= 50) {
         row.style.backgroundColor = &quot;#FFFF00&quot;; //Yellow color
     } else {
         row.style.backgroundColor = &quot;#00FF00&quot;; //Green color
     }
 }

} }); });

Where Variance is the internal name of your SharePoint column.

For more information, check this: SP 2013: Highlight Row if Field Value is empty

You can get the internal name of your column by following this article: How to find the Internal name of columns in SharePoint Online?

Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61
0

SharePoint 2016 classic experience does not support JSON formatting.

Based on your description, I understand that you want to set SharePoint list column background color according to column contents.

Here’re steps:

1.Edit page -> Add a content editor web part.

2.Press F12, find column ID. enter image description here

3.Edit page -> Edit source -> Add following codes.

Note: You should replace "ms-vb-lastCell.ms-cellstyle.ms-vb2.ms-vb-lastCell" with ID found in previous step. enter image description here

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script><script>
function listFormat() {        
        $Text = $("td .ms-vb-lastCell.ms-cellstyle.ms-vb2.ms-vb-lastCell:contains('10%')");
        $Text.css("background-color", "Red");
        $Text = $("td .ms-vb-lastCell.ms-cellstyle.ms-vb2.ms-vb-lastCell:contains('20%')");
        $Text.css("background-color", "Yellow");
}
//  This invokes the function defined above once the page Document Object Model (DOM) is ready for JavaScript code to execute:
    $(document).ready(function(){
        listFormat()
}); 
//  This preserves conditional formatting after list filters are applied or cleared:
    window.onhashchange = listFormat;

// Equivalent syntax: // window.addEventListener("hashchange", listFormat); </script> ​​​​​​​​​

4.Result.

enter image description here

Emily Du - MSFT
  • 2,611
  • 1
  • 5
  • 7
  • Thank You! Is it possible to define a range for the number? i.e. Display Red if between 0-10%? – Daryl Apr 27 '21 at 16:14