Conditional Column Formatting in APEX
Posted by Tyler Muth on December 1, 2007
I wanted to share a little trick I’ve used in APEX for a while now to conditionally format report columns based on their values. I’m sure there are plenty of alternatives to this trick, including the 4 built-in conditional alternatives for a row when using a named column template.
At a high level, this technique uses the following components:
- A hidden column in the query that returns the formatting attributes for a column. I’m going to return the color or padding-left in my examples later in this post.
- Edit the Report Attributes > Column Attributes > Column Formatting > HTML Expression of a visible column and use the hidden column to change it’s formatting. This is the same section you apply a date or number format.
Example 1 - Color Code Salary
Query
select empno,ename,sal, case when sal < 1000 then 'red' when sal between 1000 and 2000 then 'purple' when sal > 2000 then 'green' end the_color from emp
Report Attributes > Column Attributes for “SAL” > Column Formatting > HTML Expression
<span style="color:#THE_COLOR#;font-weight:bold;">#SAL#</span>
Example 2 - Employee Hierarchy
Query
select (level*20)-20 the_level,
empno,ename,mgr
from emp
connect by prior empno = mgr
start with mgr is null
Report Attributes > Column Attributes for “ENAME” > Column Formatting > HTML Expression
<span style="padding-left:#THE_LEVEL#px;">#ENAME#</span>
December 1, 2007 at 4:12 pm
Hi Tyler,
Very good post. Usefull to.
Sometimes, the solution is right in front of your eyes, and all you need is that little “click”.
Congratulations.
December 2, 2007 at 6:21 am
Tyler,
Nice trick, simple but very effective! Will definitely be useful.
Anthony.
December 3, 2007 at 2:45 am
Hello Tyler,
thanks for this post. Very interesting.
best regards
carsten
December 8, 2007 at 5:45 am
I like the hierarchy example!
Patrick
February 14, 2008 at 11:34 am
Great examples, Tyler. I like to see when somebody takes the simpler way to the solution.
In fact, I’m using exactly your “Color Code” solution to manage the look of my app, which has improved dramatically since its use.
But I yet have one unsolved problem, that I’ve posted (and even replied) in the Apex forum without a solution. Maybe you could answer a simple question:
I don’t know / don’t found how to do / nobody could explain how to, appliyng the same column color format to the column summary at the end of the report.
All suggestions tested weren’t conditionally applicable.
Thanks.
April 27, 2008 at 10:59 am
I have developed an Apex application for my senior management,but my database through accessible thru VPN from my desktop.Now senior management do not want to go to vpn to access this application.We have three tier environment and database is in tier 3 and there is web server in tier 1 and Application Server in tier 2.Is there any way to view my Apex application accessible on Web server in tier1,I do not know anything about network architecture.Please helpas otherwise my labour to develop this application will go invain.
April 27, 2008 at 11:28 am
Prakash,
I suggest you post this question on the APEX forum. I’d like to keep the comments related to the post itself.
Tyler