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
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
Example 2 – Employee Hierarchy
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