Tuesday, 16 April 2019

Javascript and SharePoint 2013: Set background color to list item

I have a script that I use in my SharePoint that sets the field to be green or white, depending on a calculated value. It works with no issues (I only included a snippet of the code, I can include the entire function if that requested).

I calculate the statusValue in the code, then, if it is < or > than a threshold value, the background of the field changes accordingly.

(function () {

    var statusFieldCtx = {};
    statusFieldCtx.Templates = {};
    statusFieldCtx.Templates.Fields = {
        "biui": {"View": StatusFieldViewTemplate}
    };


SPClientTemplates.TemplateManager.RegisterTemplateOverrides(statusFieldCtx);

function StatusFieldViewTemplate(ctx) {

    EQ = VALUE // Removed code for simplification
    statusValue = MATH // Removed code for simplification

    if (statusValue < EQ) {

        return "<div style='background-color:green;color:white'>" + statusValue.toFixed(5) + "</div>";

        }

    else {

        return "<div style='background-color:white;color:black'>" + statusValue.toFixed(5) + "</div>";

        }

    }
})();

I would like to perform more calculations after this; however, the return essentially ends the script.

Is there another way to set the background similar to the above code, without using the return functionality?



from Javascript and SharePoint 2013: Set background color to list item

No comments:

Post a Comment