JSON Formatting in Modern SharePoint lists/libraries.



One of the most important feature in modern SharePoint lists and library is its ability to be customized by JSON formatting. There are two different methods to do so.

1. additionalRowClass

2. rowFormatter

Below is the one liner defs for both these methods.

additionalRowClass is used when you only wnat to concentrate on one column in the list or library.

rowFormatter is used to customize the complete Row.

And if you decide to act smart and use both of them together, any styling applied to additionalRowClass will be lost.

Now we will use rowFormatter, and for it we will create elements using DIVs

we will create a Main DIV and nest 3 DIVs inside it.

Below is the complete JSON for it. We have also applied styling to the DIVs.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
  "hideSelection": true,
  "hideColumnHeader": true,
  "rowFormatter": {
    "elmType": "div",
    "_comment_": "MAIN DIV",
    "attributes": {
      "class": "ms-bgColor-neutralLighter"
    },
    "style": {
      "display": "flex",
      "flex-wrap": "wrap",
      "align-items": "stretch",
      "flex-direction": "row",
      "padding": "20px",
      "margin-bottom": "16px",
      "max-width": "850px",
      "border-radius": "8px"
    },
    "children": [
  {
        "elmType": "div",
        "__comment": "DIV 1",
        "style": {
          "flex-grow": "1",
          "display": "flex",
          "flex-direction": "column",
          "flex-wrap": "nowrap",
          "align-items": "stretch",
          "max-width": "260px"
        },
        "children": [
          {
            "elmType": "img",
            "style": {
              "width": "260px",
              "height": "160px"
            },
            "attributes": {
              "src": "[$PIC]"
            }
          }
        ]
      },
                    
      {
        "elmType": "div",
        "__comment": "DIV 2",
        "style": {
          "flex-grow": "1",
          "display": "flex",
          "flex-direction": "column",
          "flex-wrap": "nowrap",
          "align-items": "center",
          "max-width": "360px",
          "min-width": "205px"
        },
        "children": [
          {
            "elmType": "span",
            "txtContent": "=[$Title]",
            "style": {
              "display": "block"
            },
            "attributes": {
              "class": "ms-fontSize-l ms-fontWeight-semibold ms-fontColor-neutralPrimary"
            }
          },
          {
            "elmType": "span",
            "txtContent": "Location",
            "style": {
              "display": "block",
              "padding-top": "20px"
            },
            "attributes": {
              "class": "ms-fontSize-l ms-fontWeight-semibold ms-fontColor-neutralPrimary"
            }
          },
          {
            "elmType": "span",
            "txtContent": "[$Address]"
          },
          {
            "elmType": "a",
            "attributes": {
              "iconName": "MapPin",
              "class": "ms-fontColor-themePrimary",
              "target": "_blank",
              "href": "='http://maps.google.com/?q=' + ( [$Title] + ' ')  + ( [$Address])"
            },
            "style": {
              "font-size": "15px",
              "padding-top": "5px",
              "cursor": "pointer"
            }
          },
          {
            "elmType": "span",
            "txtContent": "='Capacity ' + [$Maximum_x0020_capacity]",
            "style": {
              "display": "block",
              "padding-top": "20px"
            }
          }
        ]
      },
      {
        "elmType": "div",
        "__comment": "DIV 3",
        "style": {
          "flex-grow": "1",
          "display": "flex",
          "flex-direction": "column",
          "align-items": "center",
          "margin-top": "10px",
          "max-width": "310px",
          "min-width": "155px"
        },
        "children": [
          {
            "elmType": "div",
            "__comment": "RATING & STAR DIV",
            "style": {
              "display": "block",
              "font-size": "15px",
              "font-weight": "bold"
            },
            "children": [
              {
                "elmType": "span",
                "txtContent": "[$Rating]",
                "style": {
                  "padding-right": "5px"
                }
              },
              {
                "elmType": "span",
                "attributes": {
                  "iconName": "FavoriteStarFill",
                  "class": "ms-fontColor-themePrimary"
                }
              }
            ]
          },
          {
            "elmType": "span",
            "txtContent": "Price",
            "style": {
              "display": "block",
              "padding-top": "20px"
            },
            "attributes": {
              "class": "ms-fontSize-l ms-fontWeight-semibold ms-fontColor-neutralPrimary"
            }
          },
          {
            "elmType": "span",
            "txtContent": "='£' +[$Price]",
            "style": {
              "display": "block"
            }
          },
          {
            "elmType": "a",
            "txtContent": "View Venue Details",
            "style": {
              "border": "none",
              "padding-top": "32px",
              "background-color": "transparent",
              "color": "#0078d7",
              "padding-left": "0px",
              "text-align": "left",
              "cursor": "pointer"
            },
            "attributes": {
              "target": "_blank",
              "href": "[$Venuelink]"
            }
          }
        ]
      }
    ]
  }
}

Note: Please remember we are here using Picture column to display iamge. If you use image column, the above code won't work.


Comments

Popular posts from this blog

Creating Nested Galleries in Power Apps. (Parent-Child Relationship)

How to reset a datepicker control in PowerApps?

Reset () function failing to work on Edit form for Combo boxes in Power App.