Tuesday, February 19, 2019

Conditional Column Formatting in APEX

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:
  1. 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.
  2. 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
1
2
3
4
5
6
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
1
<span style="color:#THE_COLOR#;font-weight:bold;">#SAL#</span>
Result
salary_color

Example 2 – Employee Hierarchy

Query
1
2
3
4
5
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
1
<span style="padding-left:#THE_LEVEL#px;">#ENAME#</span>
Result
employee_hierarchy

No comments:

Post a Comment

To generate a PDF using JavaScript in Oracle APEX from a collection

  To generate a PDF using JavaScript in Oracle APEX from a collection, you can follow these steps: 1. Create a button or link on your APEX p...