Thursday, January 28, 2016

Stinkin' Badges

Ever since APEX 5, the poor Navigation Bar has taken a back seat to the Navigation Menu. And for good reason, as the Navigation Menu offers a much more intuitive and flexible way to provide site-wide navigation that looks great, is responsive and just plain works. However, the Navigation Bar can and does still serve a purpose. Most application still use it to display the Logout link and perhaps the name of the currently signed on user. Some applications use it to also provide a link to a user's profile or something similar.

Another use for the Navigation Bar is to present simple metrics via badges. You've seen the before: the little red numbered icons that hover in the upper-right corner of an iPhone or Mac application, indicating that there's something that needs attention. Whether you consider them annoying or helpful, truth be told, they are a simple, minimalistic way to convey that something needs attention.

Fortunately, adding a badge to a Navigation Bar entry in the Universal Theme in APEX 5 is tremendously simple. In fact, it's almost too simple! Here's what you need to do:
First, navigate to the Shared Components of your application and select Navigation Bar List. From there, click Desktop Navigation Bar. There will likely only be one entry there: Log Out.

2016 01 28 08 40 05

Click Create List Entry to get started. Give the new entry a List Entry Label and make sure that the sequence number is lower than the Log Out link. This will ensure that your badged item displays to the left of the Log Out link. Optionally add a Target page. Ideally, this will be a modal page that will pop open from any page. This page can show the summary of whatever the badge is conveying. Next, scroll down to the User Defined Attributes section. Enter the value that you want the badge to display in the first (1.) field. Ideally, you should use an Application or Page Item here with this notation: &ITEM_NAME. But for simplicity's sake, it's OK to enter a value outright.
Run your application, and have a look:

2016 01 28 08 48 45

Not bad for almost no work. But we can make it a little better. You can control the color of the badge with a single line of CSS, which can easily be dropped in the CSS section of Theme Roller. Since most badges are red, let's make ours red as well. Run your application and Open Theme Roller and scroll to the bottom of the options. Expand the Custom CSS region and enter the following text:

.t-Button--navBar .t-Button-badge { background-color: red;}

Save your customizations, and note that the badge should now be red:

2016 01 28 08 49 49

Repeat for each metric that you want to display in your Navigation Bar.

4 comments:

Leigh Cowie said...

Great little tutorial.

Is there away to have the background color different for two separate badges? I.e. Red for system alerts and another color for simple notifications?

Chimpanzee said...

Great little tip - thanks. I don't think this is documented anywhere but will come in very handy. I've also noticed that you can use the 2nd User Defined Attrubute to add an additional class to the <li> element. You could also enter a Page or Application Item in here that's procedurally set depending on the value, eg:

if :BADGE_VALUE > 0 then
:BADGE_CLASS := 'hot';
else
:BADGE_CLASS := 'neutral';
end if;

This allows you to style the badge differently if action is required using some CSS like

li.hot .t-Button-badge {background-color:red}
li.neutral .t-Button-badge {background-color:gray}

Or you can just declare a static class directly in the 2nd attribute if, like Leigh above, that's all you need.

Thanks again.

Chimpanzee said...

Hi Leigh. See my comment below on how to apply a class to a specific item and thus, style the badge differently.

Leigh Cowie said...

Thanks for that appreciate it.