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 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):
Now which action should we use when the dynamic action fires?
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.
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:
or Javascript :
18 comments:
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 :)
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
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
Very elegant solution..
Everything should be made as simple as possible, but no simpler.
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.
This is what I exactly searched for. Big Thanks for sharing your knowledge.
Thomasso
Thank you! Very nice! How did you figure out the _LABEL thing?
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?
Sure you can comma separate the affected elements.
Hi. Can you also change column header title of Interactive Report/Grid thru Dynamic Actions? If so, how?
Thank you. Great idea.
Does this solution work in APEX 4.2?
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
Thanks a lot for this simple and working solution :-)
if i put the label is empty, how i can do to hide the item?
Thanks
If my requirement is rename the label wherever it is used in any of apex application , Do we have any easy way to find
GREAT WORK...!!!
Thank you. That was simple and powerful.
Post a Comment