{"id":523,"date":"2014-02-26T02:29:23","date_gmt":"2014-02-26T08:29:23","guid":{"rendered":"http:\/\/www.idubbs.com\/blog\/?p=523"},"modified":"2014-02-26T02:29:23","modified_gmt":"2014-02-26T08:29:23","slug":"js-link-csr-view-conditional-formatting","status":"publish","type":"post","link":"https:\/\/www.idubbs.com\/blog\/2014\/js-link-csr-view-conditional-formatting\/","title":{"rendered":"JS Link \u2013 CSR View Conditional Formatting"},"content":{"rendered":"<p>This post builds on the <a href=\"http:\/\/www.idubbs.com\/blog\/2014\/js-link-csr-simple-view-field-formatting\/\" target=\"_blank\">JS Link \u2013 CSR Simple View Field Formatting<\/a> post.<\/p>\n<p>If you can format a single field as was done in the previous example, it\u2019s not a big step from there to using conditional formatting.<\/p>\n<p>In SharePoint Designer (pre-2013) one of relatively easy yet powerful features\u00a0 was conditional formatting \u2013 the ability to dynamically change how content was displayed based on rules. In the <a href=\"http:\/\/www.idubbs.com\/blog\/2014\/js-link-csr-simple-view-field-formatting\/\" target=\"_blank\">last post<\/a> we went over how to do simple formatting, in this post we\u2019ll review how to incorporate some simple rules or conditions.<\/p>\n<p>In the previous example we bolded everything in the Issue Status field.<\/p>\n<p><a href=\"http:\/\/209.95.55.158\/~idubbsco\/blog\/wp-content\/uploads\/2014\/02\/image4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"image\" alt=\"image\" src=\"http:\/\/209.95.55.158\/~idubbsco\/blog\/wp-content\/uploads\/2014\/02\/image_thumb4.png\" width=\"405\" height=\"169\" border=\"0\" \/><\/a><\/p>\n<p>In this example we\u2019ll \u2018conditionally\u2019 select which items are bolded based on data values for each item.<\/p>\n<p><a href=\"http:\/\/209.95.55.158\/~idubbsco\/blog\/wp-content\/uploads\/2014\/02\/image5.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"image\" alt=\"image\" src=\"http:\/\/209.95.55.158\/~idubbsco\/blog\/wp-content\/uploads\/2014\/02\/image_thumb5.png\" width=\"459\" height=\"188\" border=\"0\" \/><\/a><\/p>\n<p>We\u2019ll start with a couple of individual fields and go from there. For this example and similar to the last post, I created a page in the Site Pages library and added the Office Issues web part to the page. I\u2019ll be setting the JS Link property of the web part here in a few steps.<\/p>\n<p>In this post we\u2019re adding three concepts:<\/p>\n<ol>\n<li>JavaScript &#8211; Calling a function.<br \/>\nIf you are a JavaScript developer this concept isn\u2019t really even worth mentioning. If you are not a developer, we\u2019re moving the code around a little bit to keep it organized, easier to read, etc\u2026<a href=\"http:\/\/209.95.55.158\/~idubbsco\/blog\/wp-content\/uploads\/2014\/02\/image6.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"image\" alt=\"image\" src=\"http:\/\/209.95.55.158\/~idubbsco\/blog\/wp-content\/uploads\/2014\/02\/image_thumb6.png\" width=\"436\" height=\"132\" border=\"0\" \/><\/a>\u2018Status\u2019:{\u2018View\u2019: <span style=\"background-color: #ffff00;\">ConditionalStatus<\/span>},<br \/>\nIn this line rather than setting the value of the field outright like we did with simple field formatting, we\u2019re calling a function \u2018ConditionalStatus\u2019 where addition work is being done.<\/li>\n<li>Checking for the \u2018condition\u2019 or rule.<br \/>\n<a href=\"http:\/\/209.95.55.158\/~idubbsco\/blog\/wp-content\/uploads\/2014\/02\/image7.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"image\" alt=\"image\" src=\"http:\/\/209.95.55.158\/~idubbsco\/blog\/wp-content\/uploads\/2014\/02\/image_thumb7.png\" width=\"433\" height=\"212\" border=\"0\" \/><\/a>Without getting into all the JavaScript details, what we\u2019re doing in the function is checking the value of the \u2018Issue Status\u2019 field and either making the contents bold, or leaving them in the default format.if (ctx.CurrentItem.Status == \u201cActive\u201d ) {<\/li>\n<li>Building the HTML for what is displayed or rendered in the web part on the page.In this example we\u2019re just using a the HTML tags necessary to bold the content being displayed. You could use virtually any HTML or CSS you wanted when it comes to formatting the data:ret = \u201c&lt;b&gt;\u201d + ctx.CurrentItem.Status + \u201c&lt;\/b&gt;\u201d;\n<p>You should also note the way we set up the data. The HTML and field data need to be built into a string &#8211; which is different than the way we did it in the previous post.<\/li>\n<\/ol>\n<p>The full sample code can be found <a href=\"http:\/\/wp.me\/agWQ1-8i\" target=\"_blank\">HERE<\/a>.<\/p>\n<p>The completed file is then uploaded to your chosen location: Master Page Gallery in this case. The web part JS Link property is configured to point to your JavaScript file:<\/p>\n<p>~site\/_catalogs\/masterpage\/[yourFileName].js<\/p>\n<p>Notes:<\/p>\n<ul>\n<ul>\n<li>You are not limited to pairing the conditional field with the field that is custom rendered.<br \/>\nIf Issue Status is \u2018Active\u2019, bold the Status field\u2026<br \/>\nYou can just as easily define a rule for the Title field to check the value of the Issue Status field.<br \/>\nIf the Issue Status field is \u2018Active\u2019, bold the Title field\u2026<br \/>\nYou can also combine rules into more complicated criteria<br \/>\nIf Issue Status is \u2018Active\u2019 <em>and<\/em> Priority is \u2018High\u2019, then bold the Title field\u2026<\/li>\n<\/ul>\n<li>Rather than customizing how each field is displayed separately, you can use a different override \u2013 the Item override \u2013 to customize each field or how the entire item is displayed. This will be covered in a separate post.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This post builds on the JS Link \u2013 CSR Simple View Field Formatting post. If you can format a single field as was done in the previous example, it\u2019s not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[33,11,13,21,27],"tags":[],"class_list":["post-523","post","type-post","status-publish","format-standard","hentry","category-client-side-rendering-csr","category-sharepoint","category-sharepoint-2013","category-sharepoint-online","category-spc14"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pgWQ1-8r","jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.idubbs.com\/blog\/wp-json\/wp\/v2\/posts\/523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idubbs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idubbs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idubbs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idubbs.com\/blog\/wp-json\/wp\/v2\/comments?post=523"}],"version-history":[{"count":0,"href":"https:\/\/www.idubbs.com\/blog\/wp-json\/wp\/v2\/posts\/523\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.idubbs.com\/blog\/wp-json\/wp\/v2\/media?parent=523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idubbs.com\/blog\/wp-json\/wp\/v2\/categories?post=523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idubbs.com\/blog\/wp-json\/wp\/v2\/tags?post=523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}