Saving Current Values with Cascading LOV's:
Say you've got two LOVs...STATES and CITIES. They both default to 'ALL' and 'ALL'. Since CITIES is dependent on STATES, as soon as STATES is changed, CITIES is blanked out. What should happen is that CITIES gets re-evaluated as in the following example... let's say STATES is ALL and CITIES is "Houston". If one then changes STATES to "Texas", CITIES should remain "Houston" as that is a valid value for CITIES.So basically, is it possible to maintain the selected value of an item if that same value exists in the list of values after refreshing?
Click here to see the demo but continue reading to learn how it all works…
There are a three main events you need to be concerned with when it comes to cascading selects:
- change
- apexbeforerefresh
- apexafterrefresh
In this example we have two select lists: parent and child. If you change the value of the child select list then the change event will fire and that’s it. But if you change the value of the parent select list a lot more happens to the child select. Here are some of the highlights:
- The current LOV values are cleared out
- The apexbeforerefresh event is triggered
- An AJAX request brings back new values. This only happens if
- optimize refresh is set to false
- optimize refresh is set to true and all parent items are not null
- The apexafterrefresh event is triggered
- The change event is fired
The first dynamic action will store the current value of the select list so that we can access it later. This will typically happen when the change event fires but it will also happen when the page first loads. Here’s how to create the first Dynamic Action:
- Right click the child select list and select Creation Dynamic Action.
- Select Advanced.
- Click Next >.
- Enter a name for the first Dynamic Action.
- Click Next >.
- Set Event to Change.
- Click Next >.
- Set Action to Execute JavaScript Code.
- Set Code to: $(this.triggeringElement).attr('data-last-value', $(this.triggeringElement).val());
- Click Next >.
- Click Create.
- Right click the item (again) and select Create Dynamic Action.
- Select Advanced.
- Click Next >.
- Enter a name for the second Dynamic Action.
- Click Next >.
- Set Event to After Refresh.
- Click Next >.
- Set Action to Execute JavaScript Code.
- Set Code to: $(this.triggeringElement).children('option[value="' + $(this.triggeringElement).attr('data-last-value') + '"]').attr('selected','selected');
- Click Next >.
- Click Create.
No comments:
Post a Comment