1

I have 2 columns namely:

  • "Status", Choice column with Red,Yellow,Green as values
  • "Comment", Multiple Lines of text column

Now, I want to set the background of "Comment" column using the color selected in "Status". For eg: If "Red"is selected, "Comment" column should have red background.

I want to have this functionality applied to all the views in the list and importantly "Status" column should not be available in the view.

I think this could be achieved using JS Link functionalities,but I'm not good with scripting. Any help will be highly appreciated.

  • For SPO Use this instead of JS Link sharepointing.co.uk/2017/11/10/… – SharePointer Dec 15 '17 at 21:43
  • 1
    @SharePointer, unfortunately Multiple lines of text columns are not yet supported by Column Formatting – theChrisKent Dec 22 '17 at 1:41
  • @SharePointer Column Formatting using JSON is supported only in Modern UI right? – George Jan 10 '18 at 15:57
  • @George yes only in the modern experience. – SharePointer Jan 12 '18 at 17:11
1

I had this problem before and after a lot of searching I learned that you can't modify multiline text fields style. You can use a single line text field instead but it will limit your comments to 255 characters.

I made 3 columns: 1.- Status: Choice Type with 3 options, Red, Yellow, Green 2.- Item Comments: Single Text line (this is where the actual comments are) 3.- Comments: Calculated Field of type: Number with the following formula:

="<div style='text-align: left; width: 200px; background-color: "&Status&"'>"&[Item Comments]&"</div>"

Note that "Item Comments" is the field that shows in the capture form, but it is excluded from the default view. The default view should only show the "Comments" column

Hope this is what you need!

enter image description here

0

The JSLink script below for your reference:

(function () {
    (window.jQuery || document.write('<script src="//ajax.aspnetcdn.com/ajax/jquery/jquery-1.10.0.min.js"><\/script>'));
    var overrideCtx = {};
    overrideCtx.Templates = {};
    overrideCtx.Templates.Fields = {
        "Comment": { "View": ColorField }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();

function ColorField(ctx) {
    var listTitle = ctx.ListTitle;
    var itemId = ctx.CurrentItem.ID;
    var comment = ctx.CurrentItem.Comment;
    var status = GetStatusValue(listTitle,itemId);
    return "<div style='background-color:" + status + "'>" + comment + "</div>";
}
function GetStatusValue(listTitle,itemId) {
    var status;
    var url = _spPageContextInfo.webAbsoluteUrl;   
    var requestUri = url + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + itemId + ")?$select=Status";
    $.ajax({
        url: requestUri,
        type: "GET",
        headers: { "ACCEPT": "application/json;odata=verbose" },
        async: false,
        success: function (data) {
            status = data.d["Status"];            
        },
        error: function (err) {
            //alert(err);
        }
    });
    return status;
}
  • For some reason this script is not working for me. – George Jan 5 '18 at 9:29
  • Using IE F12 developer tools to debug your code and check the error messages. – LZ_MSFT Jan 5 '18 at 9:36
  • This works now.Again this script applies formatting to "Comments" column only when "Status" column is added to the view. – George Jan 5 '18 at 9:52
  • Yes, if the "Status" column not add to view, the JSLINK will not work. You can mark the reply as answer if it help you. – LZ_MSFT Jan 5 '18 at 9:59
  • I already have a working script as shared by you. My requirement is to apply formatting when "Status" column is not added to the view – George Jan 5 '18 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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