javascriptsharepointjslink

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?


Solution

  • Turns out the answer was very simple, and I was a being a bit ignorant.

    I just created a main function, that called two other functions.

    Initially I tried returning both results with one return; however, they both linked to the one field; hence, I needed to call two separate functions.

    I needed to functions in order to allow for mutliple returns to different fields, i.e:

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

    and

    (statusFieldCtx.Templates.Fields = {"bzwi": {"View": StatusFieldViewTemplate}};

    (function () {
    
        funcOne();
        funcTwo();
    
    })();
    
    function funcOnc(){
    
    . . . Code linked to first field
    
    }
    
    function funcTwo(){
    
    . . . Code linked to second field
    
    }