{"id":68,"date":"2011-01-03T00:31:20","date_gmt":"2011-01-03T05:31:20","guid":{"rendered":"http:\/\/xlinesoft.com\/blog\/?p=68"},"modified":"2016-06-03T20:31:19","modified_gmt":"2016-06-04T01:31:19","slug":"tutorial_conditional_formatting","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2011\/01\/03\/tutorial_conditional_formatting\/","title":{"rendered":"Tutorial: Conditional formatting"},"content":{"rendered":"<p>This tutorial shows how to use conditional formatting in PHPRunner\/ASPRunnerPro projects.<\/p>\n<p>Our first example illustrates the profitability of the product line where we have a list of products with their respective monthly profit figures. All positive numbers are displayed in black and all negative in red. Changing the font color of this field based on its value helps the viewer immediately spot the losing products and take necessary actions.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-81\" title=\"conditional_formatting1\" src=\"http:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting1.png\" alt=\"\" width=\"556\" height=\"400\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting1.png 556w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting1-300x215.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting1-150x107.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting1-400x287.png 400w\" sizes=\"auto, (max-width: 556px) 100vw, 556px\" \/><\/p>\n<p><!--more--><br \/>\nTo set the formatting proceed to Visual Editor and double-click on the field you want to format. In our case it is the field &#8220;Profitability&#8221;. Select &#8216;Custom&#8217; View As option. Add your code in the custom code editor. If the value of my current field is greater than zero I will set the font color to black, otherwise to red. Always remember to check the syntax of your code.<\/p>\n<p><strong>PHP<\/strong><\/p>\n<pre name=\"code\" class=\"php:nocontrols\">if ($value &lt; 0) {\r\n     $color=\"black\";\r\n} else {\r\n   $color=\"red\";\r\n}\r\n$value=\"&lt;span style='color: \" . $color . \"'>\" .$value . \"&lt;\/span>\";<\/pre>\n<p><strong>ASP<\/strong><\/p>\n<pre name=\"code\" class=\"vb:nocontrols\">if strValue < 0 then\r\n   color=\"black\"\r\nelse\r\n   color=\"red\"\r\nend if\r\nstrValue=\"&lt;span style='color: \" &#038; color &#038; \"'>\" & strValue & \"&lt;\/span>\"<\/pre>\n<p><strong>C#<\/strong><\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">\r\nstring color;\r\nif (value > 0) {\r\ncolor=\"black\"; \r\n}else { \r\ncolor=\"red\"; \r\n}\r\nvalue=\"&lt;span style='color: \" + color + \"'>\" + value.ToString() + \"&lt;\/span>\";\r\n<\/pre>\n<p>You can also represent the positive and negative results with images. For example I have added the red and green circles to highlight the performance of each product. To make this work you will simply need to add two more lines to your custom code. Don&#8217;t forget to place the actual images in the project folder.<\/p>\n<p><strong>PHP<\/strong><\/p>\n<pre name=\"code\" class=\"php:nocontrols\">if ($value < 0) {\r\n  $value =$value. ' &lt;img src=\"green.png\" alt=\"\" \/>';\r\n   $color=\"black\";\r\n} else {\r\n   $value ='&lt;strong>'.$value. '&lt;\/strong> &lt;img src=\"red.png\" alt='' \/>';\r\n   $color=\"red\";\r\n}\r\n$value=\"&lt;span style='color: \" . $color . \"'>\" . $value . \"&lt;\/span>\";<\/pre>\n<p><strong>ASP<\/strong><\/p>\n<pre name=\"code\" class=\"vb:nocontrols\">if strValue < 0 then\r\n  strValue = strValue &#038; \" &lt;img src='green.png' alt='' \/>\"\r\n  color=\"black\"\r\nelse\r\n  strValue = \"&lt;strong>\" & strValue & \"&lt;\/strong> &lt;img src='red.png' alt='' \/>\"\r\n  color=\"red\"\r\nend if\r\nstrValue=\"&lt;span style='color: \" & color & \"'>\" & strValue & \"&lt;\/span>\"<\/pre>\n<p><strong>C#<\/strong><\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">\r\nstring color;\r\nif (value < 0) {\r\n  value = String.Format(\"{0} &lt;img src='green.png' alt='' \/>\", value); \r\n  color=\"black\"; \r\n} else  {\r\n  value = String.Format(\"&lt;strong>{0}<\/strong> &lt;img src='red.png' alt='' \/>\", value);  \r\n  color=\"red\";  \r\n}\r\nvalue = String.Format( \"&lt;span style='color: {0}'>{1}<\/span>\" , color, value); \r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-82\" title=\"conditional_formatting2\" src=\"http:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting2.png\" alt=\"\" width=\"554\" height=\"400\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting2.png 554w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting2-300x216.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting2-150x108.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting2-400x288.png 400w\" sizes=\"auto, (max-width: 554px) 100vw, 554px\" \/><\/p>\n<p>Now, lets say we want to highlight not only the font color itself but also change the background of the cell to yellow.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-83\" title=\"conditional_formatting3\" src=\"http:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting3.png\" alt=\"\" width=\"551\" height=\"399\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting3.png 551w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting3-300x217.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting3-150x108.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2011\/01\/conditional_formatting3-400x289.png 400w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/p>\n<p>Because we are no longer working with just the field values, but with the table structure, we will have to use the events to set the background color.<\/p>\n<p>Proceed to the Events screen in the software and select &#8216;After record processed&#8217; event for the list page of the Products table. The code will be similar to the one we used earlier. If the value of the &#8216;Profitability&#8217; field is less than zero and will set the background of the cell to yellow. You can refer to the software manual for more examples and code syntax.<\/p>\n<p><strong>PHP<\/strong><\/p>\n<pre name=\"code\" class=\"php:nocontrols\">if ($data[\"Profitability\"]<0)\r\n   $record[\"Profitability_css\"].='background:yellow';<\/pre>\n<p><strong>ASP<\/strong><\/p>\n<pre name=\"code\" class=\"vb:nocontrols\">if data(\"Profitability\")<0 then\r\n   record(\"Profitability_css\") = record(\"Profitability_css\") &#038; \"background:yellow\"\r\nend if<\/pre>\n<p><strong>C#<\/strong><\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">\r\nif (data[\"Profitability\"]<0)  \r\n   record[\"Profitability_css\"]+=\"background:yellow\";  \r\n<\/pre>\n<p>Let\u2019s say you wanted to take it a step further and highlight the entire row of the losing products.<\/p>\n<p>In the same event you can use the same code for the condition check and only change the code for setting the background of the row. You code should look like this. Once again you can find the exact syntax for this example in the software manual.<\/p>\n<p><strong>PHP<\/strong><\/p>\n<pre name=\"code\" class=\"php:nocontrols\">if ($data[\"Profitability\"] < 0)\r\n   $row[\"rowstyle\"]='style=\"background:yellow\"';<\/pre>\n<p><strong>ASP<\/strong><\/p>\n<pre name=\"code\" class=\"vb:nocontrols\">if data(\"Profitability\") < 0 then\r\n   row(\"rowstyle\")=\"style='background:yellow'\"\r\nend if<\/pre>\n<p><strong>C#<\/strong><\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">\r\nif (data[\"Profitability\"]<0)  \r\n   row[\"rowstyle\"]+=\"background:yellow\";  \r\n<\/pre>\n<p>And if you want to change the background color of all rows regardless of the condition, you can simply comment out the condition in this event:<\/p>\n<p><strong>PHP<\/strong><\/p>\n<pre name=\"code\" class=\"php:nocontrols\">$row[\"rowstyle\"]='style=\"background:yellow\"';<\/pre>\n<p><strong>ASP<\/strong><\/p>\n<pre name=\"code\" class=\"vb:nocontrols\"> row(\"rowstyle\")=\"style='background:yellow'\"<\/pre>\n<p><strong>C#<\/strong><\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">\r\nrow[\"rowstyle\"]+=\"background:yellow\";  \r\n<\/pre>\n<p><a href=\"http:\/\/xlinesoft.com\/tutorials\/ConditionalFormating.html\" target=\"_blank\">Watch the video tutorial<\/a><\/p>\n<p>Other tutorials: <a href=\"http:\/\/xlinesoft.com\/phprunner\/php-database.htm\" target=\"_blank\">PHPRunner<\/a>, <a href=\"http:\/\/xlinesoft.com\/asprunnerpro\/tutorial.htm\" target=\"_blank\">ASPRunnerPro<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use conditional formatting in PHPRunner\/ASPRunnerPro projects. Our first example illustrates the profitability of the product line where we have a list of products with their respective monthly profit figures. All positive numbers are displayed in black and all negative in red. Changing the font color of this field based on its value helps the viewer immediately spot the losing products and take necessary actions.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1,8],"tags":[9,17,6,5],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-asp-net","category-php-category","category-tutorials","tag-view-as-custom","tag-appearance","tag-asprunnerpro","tag-phprunner"],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":72,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions\/194"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}