SOLVED

Conditional formatting based on Null value of field

Brass Contributor

I'm attempting to do conditional column formatting with JSON to assign a color to those fields showing any value, and no color for those that are a Null value. I found this online, which according to the discussion should work, I'm so new to this function, I don't even know where this string should be placed in the JSON code of the body. 

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "debugMode": true,
  "elmType": "div",
  "attributes": {
    "class": "=if(@currentField,'sp-field-severity--good','')"
  },
  "children": [
    {
      "elmType": "span",
      "style": {
        "display": "inline-block",
        "padding": "0 4px",
        "color": "=if([@currentField] == '', 'red', 'black')"
      }
    },
    {
      "elmType": "span",
      "txtContent": "@currentField"
    }
  ]
} 

 This is a portion of my current JSON body, where should the above schema string go here if I want the field "Title" to be a different color if there is a Value in the field? And no color if the field is Null value?

{
"sections": [
{
"displayname": "Administration & Communication",
"fields": [
"Status",
"Priority",
"Quantity",
"Segment",
"Responder Email",
"Assigned to",
"Request Type",
"KiteWorks Access",
"Notes to Requestor",
"Deeplink"
]
},
{
"displayname": "General Course Info",
"fields": [
"Title",
"Instructions",
"File Type",
"Content File Name",
"Training Hours",
"Mobile Enabled",
"Keywords",
"Training Description",
"Languages",
"Translation File",
"Course Evaluation",
"Learning Type",
"Business Unit",
"Completion Criteria",
"Reporting Tag",
"Subjects/Competencies"
]
}

 

Thank you for any help!

2 Replies
best response confirmed by kellyhickman14096 (Brass Contributor)
Solution

@kellyhickman14096 

 

Are you trying to change the color in list view using column formatting or on list form using form formatting?

 

First schema code given is for column formatting and seconds schema code is for form "body" formatting.

 

Unfortunately, form "body" formatting does not support conditional schema like column/view/form header/form footer formatting:

Unlike the header and the footer, body configuration only allows defining one or more sections and adding one or more columns into each of those sections.

 

SourceConfigure custom body with one or more sections 

 

Related readMake section border/separator invisible in list form body custom layout with JSON 


Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

Thank you, yes I figured that out yesterday after messing around, and for others who may come looking for this same instance, this is the what I ended up with and it works.

Use advanced formatting under the column view and add this JSON:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"elmType": "div",
"txtContent": "@currentField",
"style": {
"background-color": {
"operator": "?",
"operands": [
{
"operator": "==",
"operands": [
"@currentField",
"Title"
]
},
"",
"#FFF399"
]
}
}
}
1 best response

Accepted Solutions
best response confirmed by kellyhickman14096 (Brass Contributor)
Solution

@kellyhickman14096 

 

Are you trying to change the color in list view using column formatting or on list form using form formatting?

 

First schema code given is for column formatting and seconds schema code is for form "body" formatting.

 

Unfortunately, form "body" formatting does not support conditional schema like column/view/form header/form footer formatting:

Unlike the header and the footer, body configuration only allows defining one or more sections and adding one or more columns into each of those sections.

 

SourceConfigure custom body with one or more sections 

 

Related readMake section border/separator invisible in list form body custom layout with JSON 


Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

View solution in original post