OFBiz Tutorial – Dependent Selects for Prototype
In my first OFBiz Tutorial, I will show you how to make dependent select boxes using Prototype.
Here it is, Dependent class allows you to link two select boxes in a parent child relationship. Any two select boxes can be linked. All you need to do is
- Parent select box options should have unique value of title attribute.
- On child select box, class attribute of options should be set to title of their parent.
Here is sample code. (Copied from Dependent Select by Silver website)
<select name="selectme" id="selectme"> <option value="">-- select --</option> <option value="1" title="flowers">Flowers</option> <option value="2" title="animals">Animals</option> </select> <select name="selectme2" id="selectme2"> <option value="">-- select --</option> <option value="1" class="flowers">Sunflower</option> <option value="2" class="flowers">Rose</option> <option value="3" class="animals">Dog</option> <option value="4" class="animals">Cat</option> </select> <script> new Dependent($('selectme2'),$('selectme')); </script>
My goal was to build a dependent select box that will work in complex enterprise application scenarios. A nonstandard case is when the value of a parent select box value is change by background code. It gets even more complex when select boxes are disabled. Good news is, our Dependent class is smart enough to deal with these use scenarios.
In the process of solving these problems I learned about an interesting feature in Prototype, it has a AOP like ability. We can modify behavior of existing code by wraping function inside of a custom wrapper function. A Prototype function wrap is applied to the setValue function on Element to handle the background select value changes and disabled select box value change.
I have enjoyed building and using this new component. Download the component from its home on the web. I hope you will be able to find some interesting applications for it as well.
Anil Patel is Chief Development Officer at HotWax Media as well as an OFBiz project committer, PMC member, and active community contributor. He also studies karate! Anil will join other HotWax Media employees and advisors in periodically posting thoughts here related to OFBiz, eCommerce, ERP, and related topics.