Tyler Muth’s Blog

Technology with a focus on Oracle, Application Express and Linux

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:

  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

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>

Result
salary_color

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>

Result
employee_hierarchy

7 Responses to “Conditional Column Formatting in APEX”

  1. Paulo Vale Says:

    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.

  2. Anthony Rayner Says:

    Tyler,

    Nice trick, simple but very effective! Will definitely be useful.

    Anthony.

  3. Carsten Says:

    Hello Tyler,

    thanks for this post. Very interesting.

    best regards
    carsten

  4. Patrick Wolf Says:

    I like the hierarchy example!

    Patrick

  5. Marcelo Burgos Says:

    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.

  6. Prakash Says:

    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.

  7. Tyler Muth Says:

    Prakash,

    I suggest you post this question on the APEX forum. I’d like to keep the comments related to the post itself.

    Tyler

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>