Forum Discussion
List Column Formatting - Set WHOLE Attributes Object If Column Has Content
Hi, I would essentially like to know how to achieve one of two outcomes in my view formatting dependent on the column in question having a value.
The following are the two eventualities I would like to reach.
IF Column HAS Content
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "a",
"txtContent": "@currentField",
"attributes": {
"target": "_blank",
"href": "@currentField"
}
}ELSE IF Column Is EMPTY
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"style": {
"display": "flex",
"justify-content": "center",
"align-items": "center"
},
"children": [
{
"elmType": "span",
"txtContent": "No URL"
}
]
}Obviously I would expect the JSON to not resemble the above examples, they are just what I would like to achieve, here. I am suspecting this will not be possible ... at least not with the 'excel style' expressions.
4 Replies
- Eliot_ColeIron Contributor
OMG, thank you both for your responses ... sorry, I literally never saw the notifications for these. ๐ฉ
( also ... this forum site software is SOOOOOO much better than the Power Platform community one. )
I even have a feeling that this is related to the thing that I've just started working on again ... because that post date is IDENTICAL to the last modified time on the flow behind it all! ๐
Don't worry, I never expect or feel entitled to help / responses in these places ... I do not take you for granted, either of you. ๐
Anyway ... if I can work out why I asked this question, I might well come back to it ... as I'd like to do something similar with calculated columns in the list form. ( that's form proper, not the list views that masquerade as 'new' forms in lists )
- grant_jenkinsIron Contributor
Eliot_Coleโ I would add two elements to your JSON but only show one of them depending on if the field has a value. This way you would show the "a" element if there was a value, and a "span" if there wasn't a value.
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "children": [ { "elmType": "span", "style": { "display": "=if(@currentField=='', '=', 'none')" }, "txtContent": "No Url" }, { "elmType": "a", "style": { "display": "=if(@currentField=='', 'none', '')" }, "txtContent": "@currentField", "attributes": { "target": "_blank", "href": "@currentField" } } ] }- Eliot_ColeIron Contributor
I gotta ask, grant_jenkinsโ, how does the span thing work?
Because, as far as I can grep, that is creating this CSS:
span { display: =; }Am I going crazy? ๐ ๐คช
- Rob_ElliottSilver Contributor
Eliot_Coleโ you can do this with the following JSON:
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json", "elmType": "a", "txtContent": "=if(@currentField!='', @currentField, 'No URL')", "attributes": { "target": "=if(@currentField!='','_blank', '_self')", "href": "=if(@currentField!='', @currentField, '')" } }..which gives the following result:
Rob
Los Gallardos
Microsoft Power Automate Community Super User.
Principal Consultant, SharePoint and Power Platform WSP Global (and classic 1967 Morris Traveller driver)