{"id":2485,"date":"2017-12-01T09:50:31","date_gmt":"2017-12-01T08:50:31","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=2485"},"modified":"2017-11-27T09:51:03","modified_gmt":"2017-11-27T08:51:03","slug":"spfx-column-formatting-using-json-blur-field","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/spfx-column-formatting-using-json-blur-field\/","title":{"rendered":"SPFx Column formatting using JSON &#8211; blur field"},"content":{"rendered":"<p>Recently a new feature called column formatting is released.<br \/>\nColumn formatting changes how data in a list item is displayed. It uses a JSON object that describes the element(s) that are displayed.<\/p>\n<p>The basics of column formatting can be found in the Microsoft docs &#8216;<a href=\"https:\/\/docs.microsoft.com\/en-us\/sharepoint\/dev\/declarative-customization\/column-formatting\" target=\"_blank\" rel=\"noopener\">Use column formatting to customize SharePoint<\/a>&#8216;.<\/p>\n<p>A while ago I created <a href=\"https:\/\/www.itidea.nl\/index.php\/spfx-field-customizer-extension-blur-field\/\" target=\"_blank\" rel=\"noopener\">a post<\/a> about a really simple SPFx field customizer extension which could blur the contents of a field and make the contents of the field readable again when hovering over the field.<br \/>\nSince this is a purely CSS solution it should be a candidate for column formatting.<\/p>\n<h4>Needed<\/h4>\n<p>The JSON of a field has to reference a custom class which implements the style to blur the contents of the field and a style definition for the :hover selector to make the contents of the field readable again when hovering over the field.<\/p>\n<p>That&#8217;s all!<\/p>\n<p>The documentation lists the <a href=\"https:\/\/docs.microsoft.com\/en-us\/sharepoint\/dev\/declarative-customization\/column-formatting#style-guidelines\" target=\"_blank\" rel=\"noopener\">predefined classes<\/a> one can use, but we need to reference our own.<\/p>\n<p>To reference a custom style sheet I use an SPFx Application Customizer which references the style sheet at the page level.<br \/>\nThe SCSS file can be placed in the SPFx Application Customizer project, but then all style sheet classes will be post-fixed with a unique random string. This is the smart way SPFx avoids conflicting CSS definitions, but it&#8217;s quite impossible to reference the appropriate class in the JSON object, because the post-fix will change every time the SCSS file is compiled again into CSS.<\/p>\n<p>So I&#8217;m going to deploy the style sheet to Azure in this case so keep the class names as I like. The downside of this is that I&#8217;m fully responsible to avoid conflicting CSS definitions. Therefor SCSS is used to create nested CSS rules and I try to create a unique parent.<\/p>\n<h5>Style definition<\/h5>\n<p>The contents of the SCSS file looks like this:<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n.ITIdeaGlobal {\r\n .blurredCell {\r\n  background-color: #e5e5e5;\r\n  display: inline-block;\r\n  filter: blur(2.5px);\r\n  margin-left: 3px;\r\n }\r\n .blurredCell:hover {\r\n  filter: none;\r\n  }\r\n}\r\n<\/pre>\n<h5>Reference the style sheet<\/h5>\n<p>The SPFx Applicaton Customizer class is implemented as:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nexport default class GlobalCssApplicationCustomizer\r\n extends BaseApplicationCustomizer&lt;IGlobalCssApplicationCustomizerProperties&gt; {\r\n\r\nprivate _cssUrl: string = &quot;https:\/\/itideadev.azurewebsites.net\/css\/ITIdeaGlobalSASS.css&quot;;\r\n\r\n@override\r\npublic onInit(): Promise&lt;void&gt; {\r\n  console.log(&quot;Adding ITIdeaGlobalCss&quot;);\r\n\r\n  let linkTag: HTMLLinkElement = document.createElement(&quot;link&quot;);\r\n  linkTag.href = this._cssUrl;\r\n  linkTag.rel = &quot;stylesheet&quot;;\r\n  document.getElementsByTagName(&quot;head&quot;)&#x5B;0].appendChild(linkTag);\r\n\r\n  console.log(&quot;Added ITIdeaGlobalCss&quot;);\r\n\r\n  return Promise.resolve();\r\n }\r\n}\r\n<\/pre>\n<p>The code adds a link tag to the &lt;head&gt; element of the page and references the css file at the location specified.<\/p>\n<h5>Change the display of the field<\/h5>\n<p>Now the css classes can be referenced in the JSON object to display a field in SharePoint.<br \/>\nTo demonstrate this a number field is created in a SharePoint list.<br \/>\nThen open the column formatting pane by selecting the drop down menu of the heading of the field and select &#8216;Format this column&#8217; in &#8216;Column settings&#8217; as show in figure 1.<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/11\/1-Column-formatting-blur-Open-settings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2493\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/11\/1-Column-formatting-blur-Open-settings.png\" alt=\"\" width=\"466\" height=\"455\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/11\/1-Column-formatting-blur-Open-settings.png 466w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/11\/1-Column-formatting-blur-Open-settings-300x293.png 300w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><\/a><\/p>\n<p>Figure 1 &#8211; Format this column<\/p>\n<p>JSON can be added in the box shown in Figure 2.<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/11\/2-Column-formatting-blur-JSON.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2492\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/11\/2-Column-formatting-blur-JSON.png\" alt=\"\" width=\"600\" height=\"442\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/11\/2-Column-formatting-blur-JSON.png 600w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/11\/2-Column-formatting-blur-JSON-300x221.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Figure 2 &#8211; JSON object<\/p>\n<ul>\n<li>The schema is added to have validation and autocomplete in Visual Studio code for the JSON. In the browser it isn&#8217;t necessary.<\/li>\n<li>debugMode outputs error messages and logs warnings to the console.<\/li>\n<li>elmType specifies the element to create, a div is this case with an class attribute with value ITIdeaGlobal.<\/li>\n<li>As a child of this div another div is created with &#8216;blurredCell&#8217; as class name. The values of the classes reference the custom style sheet definition classes.<\/li>\n<li>The text content, represented by txtContent, of the div element is the value of the currentField.<\/li>\n<\/ul>\n<p>This results in the HTML displayed in Figure 3.<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/11\/3-Column-formatting-blur-Result.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2491\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/11\/3-Column-formatting-blur-Result.png\" alt=\"\" width=\"350\" height=\"301\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/11\/3-Column-formatting-blur-Result.png 350w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/11\/3-Column-formatting-blur-Result-300x258.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/a><\/p>\n<p>Figure 3 &#8211; Result of column formatting<\/p>\n<p>Where the blurredCell class and the :hover selector are applied as defined in the style sheet.<\/p>\n<h4>Summary<\/h4>\n<p>The column formatting feature is quite powerful just as the documentation and samples already outlining.<br \/>\nAlso using custom styles is powerful. This way any style attribute can be used, not only the ones allowed and listed in the <a href=\"https:\/\/docs.microsoft.com\/en-us\/sharepoint\/dev\/declarative-customization\/column-formatting#detailed-syntax-reference\" target=\"_blank\" rel=\"noopener\">documentation<\/a> to use directly in the JSON object.<br \/>\nTo be able to use it some plumbing has to be done to get the styles available at the page, but then you can do anything you want!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently a new feature called column formatting is released. Column formatting changes how data in a list item is displayed. It uses a JSON object that describes the element(s) that are displayed. The basics of column formatting can be found &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/spfx-column-formatting-using-json-blur-field\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39,41],"tags":[37,48],"class_list":["post-2485","post","type-post","status-publish","format-standard","hentry","category-office-365","category-sharepoint","tag-office365","tag-spfx"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=2485"}],"version-history":[{"count":12,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2485\/revisions"}],"predecessor-version":[{"id":2501,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2485\/revisions\/2501"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=2485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=2485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=2485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}