Generic solution for cascading select lists/lovs
After implementing several times a select list/popup lov, which is depending on another item on the same page (see the AJAX example from Carl Backstrom), I thought that it was time to come up with a more generic solution where I don't have to implement javascript code and on-demand processes for each lov anymore.

I thought if the render engine of APEX is able to generate the SQL statements on the fly, I should be too ;-)
My goals where that I
So I have put together some Javascript and PL/SQL code and a How-To-Do, so that you are able to integrate it into your existing applications.
The solution has been tested with APEX 2.2.1 and Firefox and IE 6. But it should also work for all 2.x versions.
Integration into your application
Follow the steps in my How to integrate posting.
Just want to use the cascading lov? Then you only have to create
That's it! All the rest is done automatically when you execute your application.
So what did we just do?
We created a two new regions on the page 0 (page 0 is executed for every page), the first will add the Javascript library into the html-page, and the second will call the PL/SQL package which will determine the lov dependencies for a page. It will generate the necessary javascript code on the fly. The Javascript code will call the generic on-demand process with all the necessary item values the lov query requires. The PL/SQL package which the on-demand process is calling, executes the defined lov query with the values entered on the html-page. The result is send back and used to repopulate the lov on the HTML page.
BTW, it's no problem when you have your own javascript code in an "onchange" event, the code generated by the library will be executed after your code.
If you want to disable the dependency checking for a lov, just write $APEXLIB_IGNORE_LOV$ into the "comments" of the page item.
Try it if this generic solution will also works for you. Feedback always welcome!
PS: I wish that some similar functionality will make it into APEX 3.0...
Example
On apex.oracle.com I put together a simple example which you can try. Afterwards the steps to create it.

In case
if it doesn't work as expected, check if the error console of your browser (Firefox: Tools/Error Console) shows some errors. I would recommend that you get a Javascript debugger like FireBug, set a breakepoint at vResult = vAjaxRequest.get("XML"); , step into the get("XML") and check the results there. Maybe there is some problem with your SQL statement. The responseText will show what APEX is sending back.

I thought if the render engine of APEX is able to generate the SQL statements on the fly, I should be too ;-)
My goals where that I
- don't have to include any Javascript code or any other specific code onto the page and that I
- don't have to write an on-demand process for each select list/lov anymore.
- And of course I don't want to use selects-list with submit, I want that my application is "sexy" and uses AJAX for partial page reload!!! :-)
- So everything should be "out of the box" as APEX would already provide this feature.
- Create a generic Javascript library which handles the dependencies between the items. It should be able to clear and repopulate all depending items, if the value of an item changes.
- Write a process which fires on page 0, which reads the APEX data-dictionary to determine the relationship between the different items and generate the necessary calls to my generic Javascript library. But which has to be aware of existing Javascript code which may has already been registered for an onchange event on the item.
- Write an on-demand process who is able to execute the select statement of the lov definition and return the result to the Javascript library (see the existing AJAX examples) which will update the select item on the page.
So I have put together some Javascript and PL/SQL code and a How-To-Do, so that you are able to integrate it into your existing applications.
The solution has been tested with APEX 2.2.1 and Firefox and IE 6. But it should also work for all 2.x versions.
Integration into your application
Follow the steps in my How to integrate posting.
Just want to use the cascading lov? Then you only have to create
- ApexLib - include Javascript library
- ApexLib - Before footer with the following code
ApexLib_Lov.generateBrowserData;
ApexLib_Browser.flushJsBuffer; - Application Item APEXLIB_REFERENCE_TYPE and APEXLIB_REFERENCE_ID
- Application Process ApexLib_getLovResult
That's it! All the rest is done automatically when you execute your application.
So what did we just do?
We created a two new regions on the page 0 (page 0 is executed for every page), the first will add the Javascript library into the html-page, and the second will call the PL/SQL package which will determine the lov dependencies for a page. It will generate the necessary javascript code on the fly. The Javascript code will call the generic on-demand process with all the necessary item values the lov query requires. The PL/SQL package which the on-demand process is calling, executes the defined lov query with the values entered on the html-page. The result is send back and used to repopulate the lov on the HTML page.
BTW, it's no problem when you have your own javascript code in an "onchange" event, the code generated by the library will be executed after your code.
If you want to disable the dependency checking for a lov, just write $APEXLIB_IGNORE_LOV$ into the "comments" of the page item.
Known restrictions
- Doesn't support lovs in a report region (multi-record)
- Doesn't support lovs where the select statement is returned by a function
- Doesn't support lovs of type radio group or checkbox
- Doesn't use the translation table for the result values if the property is set
Try it if this generic solution will also works for you. Feedback always welcome!
PS: I wish that some similar functionality will make it into APEX 3.0...
Example
On apex.oracle.com I put together a simple example which you can try. Afterwards the steps to create it.
- Create a new region of type "HTML" and set region template to "Form Region"
- Add a new item with the following settings
- Type: Select List
- Select list control type: Select List (you don't need Select List with Submit, ... anymore!)
- Name: P4_REGION_ID
- Display Null: Yes
- Null Text: - select region -
- List of Values Query
SELECT REGION_NAME AS D, REGION_ID AS R
FROM REGIONS
ORDER BY REGION_NAME - Label: Region
- Item Source: Always null
- Add another item to this region
- Type: Select List
- Select list control type: Select List
- Name: P4_COUNTRY_ID
- Display Null Option: Yes
- Null Text: - select country -
- List of Values Query
SELECT COUNTRY_NAME AS D, COUNTRY_ID AS R
FROM COUNTRIES
WHERE REGION_ID = :P4_REGION_ID
ORDER BY COUNTRY_NAME - Label: Country
- Item Source: Always null
- Add another item to this region
- Type: Select List
- Select list control type: Select List
- Name: P4_LOCATION_ID
- Display Null Option: Yes
- Null Text: - select location -
- List of Values Query
SELECT CITY AS D, LOCATION_ID AS R
FROM LOCATIONS
WHERE COUNTRY_ID = :P4_COUNTRY_ID
ORDER BY CITY - Label: Location
- Item Source: Always null

In case
if it doesn't work as expected, check if the error console of your browser (Firefox: Tools/Error Console) shows some errors. I would recommend that you get a Javascript debugger like FireBug, set a breakepoint at vResult = vAjaxRequest.get("XML"); , step into the get("XML") and check the results there. Maybe there is some problem with your SQL statement. The responseText will show what APEX is sending back.
Labels: ApexLib, cascading lov







44 Comments:
can this be used in HTMLDB ver2
By
Anonymous, at 24 November, 2006 17:15
Hi,
I have tested it with Version 2.2.1. Are you using Version 2.0?
In that case some modifications have to be done.
1.) In the ApexLib.js file, all ocurrences of $x have to be replaced by html_GetElement => $x doesn't exist in V2.0
2.) You can only use the version which is loaded into the FLOW_... schema. The other version doesn't work because the APEX_* dictionary views do not exist in V2.0. What do you have to do. a) Replace the FLOWS_020200 thru FLOWS_020000 in the header of the package. b) Maybe you have to remove the where-clause restriction NVL(UPPER(ITEM_COMMENT), '@') NOT LIKE '%$APEXLIB_IGNORE_LOV$%', I'm not sure if the comment column exists in V2.0. Don't have a possibility to check that yet.
I will come up with a solution on sunday or monday.
Patrick
By
Patrick Wolf, at 25 November, 2006 12:21
Just posted an update of the description and the zip-file to cover version prior to 2.2.x
Patrick
By
Patrick Wolf, at 26 November, 2006 19:11
Hi,
I was wondering if I could use this example to populate a select list from a text field. i.e. the first select list would be a text field.
Thanks
By
Anonymous, at 05 December, 2006 22:01
Hi,
just to make sure that I did understand you correct.
The first field is a text field where you can enter any value and the second field is a select-list which uses the value entered in the text field to retrieve the data.
If you mean that than I can confirm that it will work.
Patrick
By
Patrick Wolf, at 05 December, 2006 22:09
Hi,
Yes that is what I meant.
When I try nothing happens.
I have followed the document, but I get no errors and no data.
Do I need to put a JS on the text item? onblur?
Thanks
By
Anonymous, at 05 December, 2006 22:14
Hi,
just put together an example at http://apex.oracle.com/pls/otn/f?p=33231:15:
Does it work for you? If you create a normal cascading lov as in the example, does that work?
Patrick
By
Patrick Wolf, at 05 December, 2006 22:24
No Patrick that doesn't work either.
I am using Application Express 2.2.1.00.04.
I have compiled ApexLib_Lov_for_userschema.pks/pkb in my schema.
I put the JS in page 0.
I then created the application items and process.
I then created my select lists.
Do I need to but a Javascript in my HTML form element attribute?
By
Anonymous, at 05 December, 2006 22:30
Hi,
maybe it's easier to sort that out when you contact me by e-mail. It's in my profile.
BTW, you don't have to add any JavaScript code, it's done by the library.
By
Patrick Wolf, at 05 December, 2006 22:37
hi,
Did you ever get that TextBox option to work?
...
The first field is a text field where you can enter any value and the second field is a select-list which uses the value entered in the text field to retrieve the data.
...
Thanks
By
Anonymous, at 13 December, 2006 23:46
Hi,
works also with a textfield as master. I have added an example for that onto the demo page (http://apex.oracle.com/pls/otn/f?p=33231:4::::::)
Run your application in debug mode. You should see something like "Analysing page(ApexLib_Lov.generateLovDependencies)" at the bottom. And it should also display "detected Px_xxx as cascading lov".
If you don't see that, check for typos in your lov sql statement. Or can you provide a test case on apex.oracle.com so that I can take a look.
Patrick
By
Patrick Wolf, at 14 December, 2006 09:21
Patrick,
3.3 step is not consistent with the image. I'm about arguments of ApexLib_Lov.generateLovDependencies
By
Edward, at 26 December, 2006 13:44
If I'm able to put ApexLib.js into /i/ of Application Server I don't have to double this code on the page 0? Right?
By
Edward, at 26 December, 2006 13:58
Hi Edward,
I have uploaded a new image, thanks!
You are right, you don't have to include the full content of ApexLib.js into page 0. You just have to include the following call.
<script src="/i/javascript/ApexLib.js" type="text/javascript"></script>
Or if you don't like to add such a page 0 region, you can also include this call into your page templates after the call of
<script src="/i/javascript/htmldb_get.js" type="text/javascript"></script>
Patrick
By
Patrick Wolf, at 27 December, 2006 21:05
Super cool! I too hope that something like this will be in the next release.
By
Peter Lorenzen, at 29 December, 2006 22:09
I have just used your ApexLib framework to implement some cascading select lists in an app I have built in Oracle XE and it works like a dream!
Thanks for your efforts in putting it together and providing such useful examples.
One question I do have though. Before I used the technique described here, I had created some javascript links that a user could click that would set the values for my 3 select lists in one click, therefore saving them having to manually select each time. This no longer works and I was wondering if there is some work around to achieve the same result?
Kind regards,
Stuart
By
Stuart, at 02 February, 2007 16:53
Hi Stuart,
great to hear that it works like a dream! :-)
About your other problem, is it possible that you put up an example on apex.oracle.com so that I can take a look? You find my e-mail address in the profile info or in the HowToInstall.html
Patrick
By
Patrick Wolf, at 02 February, 2007 17:24
Hello,
I have been using the dynamic LOV for many pages in my apps and it works brilliantly. Then all of a sudden, I get some javascript error on some of my pages..
html_GetElement(vClearFieldId) has no properties
and the other select list doesn't get populated any more.
Any idea why??
By
Venus, at 14 February, 2007 13:42
Hi Venus,
I think the easiest to track this down is if you write me an e-mail. You can find it in my profile.
Patrick
By
Patrick Wolf, at 14 February, 2007 15:14
Does the cascading LOV work if you put the two LOVs on Page 0?
I created some LOVs on page 1 and everything works. If I move the region to Page 0, the second LOV is not updated
By
Anonymous, at 22 February, 2007 17:20
Hi,
I didn't consider lovs on page 0.
Have already fixed it. If you write me a mail, I can send you the corrected package.
Patrick
By
Patrick Wolf, at 22 February, 2007 17:55
Patrick,
I was looking at the example online and now you use a popup lov. This is the reason I've integrated your framework. However, it's not working for me...
I have XE and Apex 2.1.
I noticed that when you open your popup lov, your new passback function is being used. But on mine its still the old one. I can find how to change this. Could you please help me?
Dan
By
Daniel, at 09 May, 2007 22:26
Dan,
have you followed chapter 3.12 "Open the Shared Components/Templates/Popup List of Values/Popup LOV" of the How To Integrate document?
You should also get the newest ApexLib_Lov.js as described in http://inside-apex.blogspot.com/2007/04/potential-problem-with-apexlib-16-and.html
If you still have problems, write me an e-mail, that's easier for communication than the blog comment.
Patrick
By
Patrick Wolf, at 09 May, 2007 23:02
Hello, Apexlib does not work to me correctly, exists some form to check the installation, Thanks
By
robert, at 29 May, 2007 00:35
Hi Robert,
what does not work correctly? The best would be to write me a mail with your symptoms.
You can find my mail address in the HowTo Install or in the Blog Profile.
Patrick
By
Patrick Wolf, at 29 May, 2007 08:16
Hi Partick,
The information in this is very useful. I need ur help in my application. I have a Tabular form in which I have given dropdown list for 2 columns. Now I want to populate data in the second dropdown based on the selected value in the first dropdown. How do I achieve this... Plesae help me..
Regards,
Pallavi
By
pallavi, at 20 June, 2007 09:36
Hi Pallavi,
the ApexLib Framework currently doesn't support that, but see Vikas demo application for a solution which might also work for you.
Patrick
By
Patrick Wolf, at 23 June, 2007 12:52
Hey,
Thanks a ton! This was exactly what I needed. I have one request though, I would like to be able to update a text area based on values chosen in the combo boxes. The text box uses a PL/SQL source wich uses bound info from the three combo boxes.
I thought maybe the code in apexlib_sql.executePlSqlCode could be used to referesh the info in the text box and if i can dig around in it I might be able to patch it up, but i figured you'd probably be way faster at it!
Thanks for a great tool!
By
Eric, at 09 July, 2007 23:11
Eric,
write me an e-mail.
Patrick
By
Patrick Wolf, at 10 July, 2007 00:18
Hi Patrick,
I implimented the ApexLib framework successfully. I have a question though. I have cascading LOVs in which one is dependent of the other. The user can cascade down to select a report they want to run. If the report uses a date range, they can select this as well via a date picker popup. Anyway, The reports generated are Oracle Reports. I'm trying to have the report open on a new page and have a parameter passed to the report. The parameter is from one of the LOVs that the user selects, IE, a City. I can't seem to get it to pass to the ORacle Report at initial run without submitting the page. Is there a way to submit the page so that the value of the LOV is passed to the report?
Thanks in advance!
Lou
By
Anonymous, at 04 October, 2007 17:11
Lou,
write me an e-mail. That's easier for communication than the blog comment.
Patrick
By
Patrick Wolf, at 04 October, 2007 18:17
Do you know how this functionality could work with HTML DB v1.6.1? I can get the Select with Submit fields to work but not the popups.
I appreciate any feedback.
Thanks.
Mark
By
Anonymous, at 27 November, 2007 15:55
Mark,
send me a mail (it's in the profile), that's easier to find out what's the problem.
Patrick
By
Patrick Wolf, at 27 November, 2007 19:55
I must have done something wrong. I followed all the instructions, but when I try to implement a drop-down test, I get the following:
ORA-06550: line 2, column 5: PLS-00201: identifier 'APEXLIB_LOV.CLEARLOVNULLVALUES' must be declared as well as others. It looks like it can't find the JavaScript, but I would appreciate some help.
I installed to its own APEXLIB schema (Apex v 3.0) with no errors. What did I miss?
Blair
By
Anonymous, at 15 January, 2008 00:15
Blair,
looks like that you haven't executed the step 6.2 (calling @ApexLib_grant_create_synonyms.sql) of the HowToInstall.html document.
Your application schema doesn't "see" the ApexLib packages in the Apexlib schema, that's why you are getting this error message. Can you check if you have executed the script and granted the privileges and synonyms to your application schema?
Patrick
By
Patrick Wolf, at 15 January, 2008 00:29
Thanks, Patrick. I had just installed a new schema for development and forgot to grant permissions.
I did run into one more error that I would appreciate your help with: "wrong number or types of arguments in call to 'INITVALIDATION' ORA-06550: line 45". Can you point me in the right direction? Are there some FAQ pages regarding your blog instructions that I missed somewhere?
Blair
By
Anonymous, at 15 January, 2008 16:32
Blair,
which version of the ApexLib framework are you using? Maybe you are using a version where the parameter "pShowRequiredWarning" wasn't supported by ApexLib_Browser.initValidation.
Always use the documentation (HowToIntegrate.html) which comes with the framework, the version you find online is always the latest one which probably doesn't match to your used version. BTW, I would suggest that you upgrade to 1.6
Patrick
By
Patrick Wolf, at 15 January, 2008 17:51
Hi,
How to Populate select list based on value of another select list in apex version 3.1. The solutions what you have provided says it can be done in apex 2.x version. I tried in apex 3.x but where condition is not supported in list of values query .
Please Help
By
M, at 09 March, 2008 17:11
Can this be made to work with checkboxes?
By
tanebhan, at 08 August, 2008 16:17
Hi,
currently it's not supported.
Patrick
By
Patrick Wolf, at 09 August, 2008 21:40
Hi Patrick
I am using ApexLib in my application and it has really done great job specially for cascading LOVs.
However I am facing a problem in which your guidance/help is needed.
In my form field B is dependent on field A. When both fields A and B are select list cascading LOV is working without any issue.
When I make field A simple Popup LOV it still works.
But the problem comes when I make field A Popup LOV (Displays description, returns key value) the field B does not show any value.
Please guide what I wrong I am doing.
ZRH
By
Anonymous, at 20 September, 2008 11:03
Hi Patrick,
First off, thank you very much for providing the code to create this LOV. It has helped me create better form options and we try to integrate it in everything we can find applicable.
I have a question about modifying the LOV and I was wondering if I could get your input on it.
I'll use the sample you provided on APEX to hopefully get my point across.
When you select a Region (Americas for example), that is when the Countries become available with "- select country -" as the default value. Is there any way for it to default to something else based on the initial entry?
What I have is a drop down of users and their roles, and based on the user selection, I want the current role they have displayed and the "- select role -" if it's null.
Is it something simple like changing the source type from "Always Null" to "SQL Query" where you pass the previous LOV value into the select statement?
Thank you again for your help!
By
Ivan, at 02 December, 2008 01:43
Hi Ivan,
that's currently not possible with the existing implementation of the cascading lovs. There is no way to define a default value.
Sorry
Patrick
By
Patrick Wolf, at 13 December, 2008 20:36
Hi,
I have oracle XE.
I created some LOVs on page, but the second LOV is not updated.
I have followed the document, but I get no errors and no data.
Analysing page(ApexLib_Lov.generateBrowserData)
...detected P14_CONTRACT_ID as cascading lov.
......references P14_RESEARCH_ID
Thanks
By
Andrey, at 03 January, 2009 15:00
Post a Comment
<< Home