Rendering and Decoding Checkboxes in an Apex Page
Submitted by dmann on Sun, 10/05/2008 - 22:00
PurposeA quick example of how to render and decode checkboxes in an Apex page.
Example ObjectsEMP table from Scott example schema.
Set up the interfaceUse the APEX_ITEM.CHECKBOX function to render checkboxes in a report. An example query:
SELECT ENAME, APEX_ITEM.CHECKBOX(1,empno, Selected) as CommissionAllowed FROM ( select emp.*, (SELECT 'CHECKED' FROM DUAL WHERE comm IS NOT NULL) as Selected FROM EMP ) ORDER BY 1;This will produce output that will render checkboxes. The checkbox will be selected if the EMP. COMM field is not null. Sort of a useless example, but you can see what we're getting at.
ENAME COMMISSIONALLOWED ADAMS <input type="checkbox" name="f01" value="7876" /> ALLEN <input type="checkbox" name="f01" value="7499" CHECKED /> BLAKE <input type="checkbox" name="f01" value="7698" /> CLARK <input type="checkbox" name="f01" value="7782" /> FORD <input type="checkbox" name="f01" value="7902" /> ...
Set up the processing codeNow once your page is submitted you want to review the submitted items and act upon them. Create a Page Process after submit. Make sure to use the Conditional Processing section to attach the process to a button press. Note that after the page submit we will only be notified which checkboxes have 'CHECKED' selected. If you need to find the difference between before and after states you may need to do some fancy logic. Here I am just deleting all the records from COMM_ALLOWED and repopulating. Again, maybe a useless example, but you can see how we get the data out of the interface and work with it in PL/SQL.
DELETE FROM COMM_ALLOWED; FOR I in 1..APEX_APPLICATION.G_F01.COUNT LOOP INSERT INTO COMM_ALLOWED ( EMPNO ) VALUES ( APEX_APPLICATION.G_F01(i) ); END LOOP;