Tuesday, February 07, 2017

Changing the label of an item in Oracle APEX dynamically

Today I got the question how to change the label of an item in Oracle Application Express (APEX) based on some condition. I actually had this requirement myself a couple of times, so maybe other people too.

Here’s an example; whenever we change the Source item, we want the Affected Item to change it’s label:

The use case: after change of the source item, the label of the affected item changes

The first thing that comes to mind (if you already know a little bit of APEX); lets use a Dynamic Action: on change of the Source item we will fire (in this example we will only fire when the value is A):

Dynamic Action in APEX

Now which action should we use when the dynamic action fires?

Default possibility of actions

Set Value will typically set the value of an Item, but what about the Label?
If I don’t find the option, I typically look for a plugin or write some code myself. In this case I wrote a bit of JavaScript, for example:

var newLabel = 'My new label for ' + $v('P2_SOURCE_ITEM');

This will set the label to "My new label for " and then the value of the item, at least if you select in the Affected Elements the item that needs the label change.

Whenever I think about writing custom code, my mind says “you should create a plugin for that”.
So I actually started to write an Oracle APEX Plug-in called “Set Label” (https://github.com/dgielis/orclapex-plugin-set-label)

While I was trying the plugin and writing up the things I needed to do, I guess something happend in my mind. I missed the obvious, it suddenly came to my mind there’s a much simpler solution to this…

You can actually use the Set Value action… just add after your item _LABEL, that’s it.

Use the Set Value dynamic action but add _LABEL to change the label of the item

Here’s the result:


Sometimes developing is much more simple than initially thought, you just have to see it :)

Update 16-FEB: if you want to make your label dynamic, in the Set Values Dynamic Action, you can specify SQL / PL/SQL:

PL/SQL Expression

or Javascript :



Firoz Mahmud said...

Very helpful tutorial.
Bu i have a question, when i want to change item label dynamically from database how it possible please give me a solution :)

Firoz Mahmud said...

Very helpful post for apex users...
But i have a simple question,
How can i want to change item label dynamically from database query?
Please give us a solution :)


Dimitri Gielis said...

Hi Firoz,

The Set Value dynamic action, has a Set Type attribute; next to Static Assignment you can set it to SQL Query or PL/SQL Function, which can return values of your table.

Hope that helps,

Mark Lancaster said...

Very elegant solution..
Everything should be made as simple as possible, but no simpler.

Dietmar Aust said...

Very elegant indeed :).

I still like the idea of writing a plugin for that. Thereby you add a small layer of abstraction and decouple from the implementation which the APEX team has chosen for now.

It seems that it is unlikely to break in the future ... but you never know. Also the plugin could be extended to take a json array as input, e.g. getNewLabels( pageId ) to return a mapping of all page items and their respective new labels.

This is a common use case for multi-tenant applications.

Just a thought.