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.affectedElements).attr('id')+'_LABEL').html(newLabel);

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:

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 :

JavaScript

18 comments:

Md. Firoz Mahmud - Bangladesh 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 :)

Md. Firoz Mahmud - Bangladesh 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 :)

Regards...
Firoz

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,
Dimitri

Mark Lancaster said...

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

Unknown 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.

Cheers,
~Dietmar.

Anonymous said...

This is what I exactly searched for. Big Thanks for sharing your knowledge.
Thomasso

Anonymous said...

Thank you! Very nice! How did you figure out the _LABEL thing?

Gean Costa said...

Thank you very much! It helped me a lot.
But I have a question: if I want to change two labels from two itens of my application using a single javascript code, is that possible?

Dimitri Gielis said...

Sure you can comma separate the affected elements.

Anonymous said...

Hi. Can you also change column header title of Interactive Report/Grid thru Dynamic Actions? If so, how?

Anonymous said...

Thank you. Great idea.

Anonymous said...

Does this solution work in APEX 4.2?

Venkat K said...

How do you change the label color based on the corresponding field value. For example, I have an item P1_CODE where the label color should be red if P1_CODE is not null and the label color should be the default when P1_CODE is null?
Thanks.
Venkat

Anonymous said...

Thanks a lot for this simple and working solution :-)

Anonymous said...

if i put the label is empty, how i can do to hide the item?

Thanks

Anju said...

If my requirement is rename the label wherever it is used in any of apex application , Do we have any easy way to find

Ganesh Nimkar said...

GREAT WORK...!!!

Laxman said...

Thank you. That was simple and powerful.