APEX: Save a user’s checkbox selection on local PC
You want a checkbox item on a page which is a preference, you want it to be remembered for the user across login sessions, but you don’t want the overhead of storing it in a database table. You might choose to store the value in a cookie instead. It may be lost (e.g. when the user clears their cookies or changes to a different browser or another computer), but we don’t mind – it’s just a preference.
1. Create checkbox item, e.g. P1_STAY_ON_PAGE
Display As = Checkbox
Template = No Label
List of values definition = STATIC2:Stay on page;Y
2. Add dynamic action to the checkbox item to save it when it’s changed
Event = Change
Selection Type = Item(s)
Item(s) = P1_STAY_ON_PAGE
Condition = (none)
True Action = Execute JavaScript Code
Fire On Page Load = (No)
Code = SetCookie("APEX_P1_STAY_ON_PAGE",$v("P1_STAY_ON_PAGE"));
3. Add dynamic action on page load to load it
Event = Page Load
True Action = Execute JavaScript Code
Code = $s("P1_STAY_ON_PAGE",GetCookie("APEX_P1_STAY_ON_PAGE"));
Note that the cookie name (“APEX_P1_STAY_ON_PAGE” in this example) is up to you to choose. Probably best to try making it specific to your application so it doesn’t clash with anything else.
praganitsch
14 August 2014 - 4:04 pm
You might want to consider looking at localStorage as an alternative to Cookies. Cookies are being sent to the server with each http-request, localStorage is in the browser only.
Tom
19 August 2014 - 6:32 pm
I would also like to point out that, if you do want to use cookies, you should use apex.storage.setCookie/getCookie (http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35127/javascript_api.htm#BGBDAACG)
The old “GetCookie” and “SetCookie” are functions in the legacy.js file and are tagged deprecated.