3

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

This is what i am trying to replicate

3 Answers 3

0

Refer below code. This will work for you:

Here Status is a field whose color we want to change. And dropdowntest is the dropdown field on basis of which choice will be made.

(Note: Change the field name according to your field. And this code will work for you)

(function () {

      function registerRenderer() {
        var StakeholderStatusFiledContext = {};
        StakeholderStatusFiledContext.Templates = {};

        StakeholderStatusFiledContext.Templates = {
            Fields : {
                'Status': { //------ field whose color you want to change
                    View : function (ctx) {
                      var StakeholderStatus = ctx.CurrentItem.Status;
                      switch(ctx.CurrentItem.dropdowntest){     //------------ value of dropdown on whose basis choice will be made
                        case 'choice1':
                            return "<span style='color :#f00'>" + StakeholderStatus + "</span>";
                            break;
                        case 'choice2':
                            return "<span style='color :#ff6a00'>" + StakeholderStatus + "</span>";
                            break;
                        case 'choice3':
                            return "<span style='color :#cab023'>" + StakeholderStatus + "</span>";
                            break;
                      }
                    }
                },
            }
        };
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(StakeholderStatusFiledContext);
    }
    ExecuteOrDelayUntilScriptLoaded(registerRenderer, 'clienttemplates.js');


})();

OutPut:

enter image description here

11
  • I appreciate you taking the time to reply. I added the changes you proposed. My status column is called StakeholderStatus so i made the appropriate changes. However, when i run the site it still doesnt seem to affect the page. Any other ideas as to why this may be happening? Sep 6, 2017 at 13:43
  • Please double check that your JS file is loading properly. And try to hard refresh the page by pressing ctrl+F5 Sep 6, 2017 at 13:45
  • I have added the file in master pages and extracted the folloowing link: testbconnect.com.au/sites/ProjectSite/_catalogs/masterpage/…. When added to the web part it renders the link appropriately. I have then gone ahead and added the (~) to the link as i understand that the webpart wont be able to translate the JSLink appropriate without it. Link now looks like the following: ~/sites/ProjectSite/_catalogs/masterpage/Display Templates/Indicators/Color.js Yet the colours wont change. It's as if it had no effect on the web part. Sep 7, 2017 at 0:32
  • try using ~sitecollection/_catalog....
    – Aveenav
    Oct 20, 2017 at 18:41
  • @RohitWaghela Can you apply the formatting to "Status" column without "dropdowntest' column added to the view?
    – George
    Jan 5, 2018 at 9:48
0

I have been able to solve this. I was adding the JSLink to the miscellaneous section of the Web part properties as by the following: https://wonderlaura.com/2014/09/11/sharepoint-conditional-formatting/

This wouldnt work for me. So i added a content Editor and dragged it to the bottom of the page. In the Content Link section, I added the following link: /sites/ProjectSite/_catalogs/masterpage/Display Templates/Indicators/Color.js. I had to remove the (~). This made my conditional formatting work.

Based on a lot of webpages i have looked at, no one has mentioned adding a content editor. Feel free to shed some light on this area if you have knowledge in it

0

The below script worked for me in hiding the column:

(function () {

function RemoveFields(ctx) {
    var fieldName = "dropdowntest"; // here dropdowntest is field or column name to be hide
    var header = document.querySelectorAll("[displayname=" + fieldName + "]")[0].parentNode;
    var index = [].slice.call(header.parentNode.children).indexOf(header) + 1;
    header.style.display = "none";
    for (var i = 0, cells = document.querySelectorAll("td:nth-child(" + index + ")"); i < cells.length; i++) {
        cells[i].style.display = "none";
    }
}

  function registerRenderer() {
    var StakeholderStatusFiledContext = {};
    StakeholderStatusFiledContext.Templates = {};

    StakeholderStatusFiledContext.Templates = {
        Fields : {
            'Status': { //------ field whose color you want to change
                View : function (ctx) {
                  var StakeholderStatus = ctx.CurrentItem.Status;
                  switch(ctx.CurrentItem.dropdowntest){     //------------ value of dropdown on whose basis choice will be made
                    case 'choice1':
                        return "<span style='color :#f00'>" + StakeholderStatus + "</span>";
                        break;
                    case 'choice2':
                        return "<span style='color :#ff6a00'>" + StakeholderStatus + "</span>";
                        break;
                    case 'choice3':
                        return "<span style='color :#cab023'>" + StakeholderStatus + "</span>";
                        break;
                  }
                }
            },
        }
    };
StakeholderStatusFiledContext.OnPostRender = RemoveFields;
   SPClientTemplates.TemplateManager.RegisterTemplateOverrides(StakeholderStatusFiledContext);
}
ExecuteOrDelayUntilScriptLoaded(registerRenderer, 'clienttemplates.js');

})();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.