{"id":53,"date":"2018-08-23T06:22:00","date_gmt":"2018-08-23T06:22:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2018\/08\/23\/what-is-conditional-formatting-in-css-and-how-to-do-it-with-angularjs-ng-class\/"},"modified":"2019-02-15T16:03:09","modified_gmt":"2019-02-15T15:03:09","slug":"what-is-conditional-formatting-in-css-and-how-to-do-it-with-angularjs-ng-class","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/what-is-conditional-formatting-in-css-and-how-to-do-it-with-angularjs-ng-class\/","title":{"rendered":"What is Conditional Formatting in CSS and How to do it With AngularJS (ng-class)"},"content":{"rendered":"<p>I&#8217;m so happy to have you again!<\/p>\n<p><ins style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"4209786523\"><\/ins><br \/>\nIn this brief blog, I am going to help you understand conditional formatting and how to apply it using css.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"clear: both; text-align: center;\"><\/div>\n<p><b>What is Conditional Formatting?<\/b><br \/>\nConditional\u00a0 Formatting also called Conditional Styling is a situation where styles are apply to html element based on conditions that may changes at view time. For example, based on values from data in a database.<\/p>\n<p>&nbsp;<\/p>\n<p><b>When to use Conditional Formatting<\/b><br \/>\nA typical example of when you can use conditional formatting is when the color of the text needs to change depending on the status. For example:<\/p>\n<ul>\n<li>Status: Pending<\/li>\n<li>Status: In Progress<\/li>\n<li>Status: Completed<\/li>\n<\/ul>\n<p><b>How to Apply it<\/b> In angularjs, you can achieve conditional formating using either<\/p>\n<ul>\n<li>ng-class<\/li>\n<li>ng-style<\/li>\n<\/ul>\n<p><b>Using ng-class<\/b> Cleate two classes called <i>Pending <\/i>and <i>Completed<\/i><\/p>\n<p>css Style<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #bb0066; font-weight: bold;\">.pending<\/span> \r\n{\r\n   <span style=\"color: #008800; font-weight: bold;\">color<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">red<\/span>;\r\n}\r\n<span style=\"color: #bb0066; font-weight: bold;\">.completed<\/span>\r\n{\r\n<span style=\"color: #008800; font-weight: bold;\">color<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">green<\/span>;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Then in your table use the html markup below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;table<\/span> <span style=\"color: #0000cc;\">ng-repeat =<\/span> <span style=\"background-color: #fff0f0;\">\"row in activities\"<\/span> <span style=\"color: #0000cc;\">width=<\/span><span style=\"background-color: #fff0f0;\">\"364\"<\/span> <span style=\"color: #0000cc;\">height=<\/span><span style=\"background-color: #fff0f0;\">\"140\"<\/span> <span style=\"color: #0000cc;\">border=<\/span><span style=\"background-color: #fff0f0;\">\"1\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;tr&gt;<\/span>\r\n      <span style=\"color: #007700;\">&lt;td&gt;<\/span>Create C# Tutorial <span style=\"color: #007700;\">&lt;\/td&gt;<\/span>\r\n      <span style=\"color: #007700;\">&lt;td<\/span> <span style=\"color: #0000cc;\">ng-class=<\/span><span style=\"background-color: #fff0f0;\">\"{'pending':row.status === 'Pending'}, 'completed':row.status ===\"<\/span><span style=\"color: #0000cc;\">Completed<\/span><span style=\"color: #007700;\">&gt;<\/span>{{row.status}}<span style=\"color: #007700;\">&lt;\/td&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;\/tr&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;tr&gt;<\/span>\r\n      <span style=\"color: #007700;\">&lt;td&gt;<\/span>Do a Presentation on .Net<span style=\"color: #007700;\">&lt;\/td&gt;<\/span>\r\n      <span style=\"color: #007700;\">&lt;td<\/span> <span style=\"color: #0000cc;\">ng-class=<\/span><span style=\"background-color: #fff0f0;\">\"{'pending':row.status === 'Pending'}, 'completed':row.status ===\"<\/span><span style=\"color: #0000cc;\">Completed<\/span><span style=\"color: #007700;\">&gt;<\/span>{{row.status}}<span style=\"color: #007700;\">&lt;\/td&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;\/tr&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/table&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The output is as shown below:<\/p>\n<div style=\"clear: both; text-align: center;\">\u00a0<a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/2.bp.blogspot.com\/-hRper1o1kIo\/W35RAC5jhhI\/AAAAAAAACFc\/xhP0ep1Edzoqo2YRpV_BXQLQ7ktmyQ2aACLcBGAs\/s1600\/Conditional%2BFomatting%2Bin%2BCSS%2Bwith%2BAngualrJS2.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/2.bp.blogspot.com\/-hRper1o1kIo\/W35RAC5jhhI\/AAAAAAAACFc\/xhP0ep1Edzoqo2YRpV_BXQLQ7ktmyQ2aACLcBGAs\/s400\/Conditional%2BFomatting%2Bin%2BCSS%2Bwith%2BAngualrJS2.jpg\" width=\"400\" height=\"162\" border=\"0\" data-original-height=\"372\" data-original-width=\"913\" \/><\/a><\/div>\n<p>Let&#8217;s now see how to do it using ng-style<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m so happy to have you again! In this brief blog, I am going to help you understand conditional formatting and how to apply it &hellip; <\/p>\n","protected":false},"author":2,"featured_media":507,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[113],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/53"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions\/509"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/507"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}