I am using Oracle ApEx and have a standards SQL Report with the following apex_item.checkbox call, i.e.:
select
apex_item.checkbox(1, empno) cbox,
"EMPNO",
"ENAME",
"JOB",
"MGR",
"HIREDATE",
"SAL",
"COMM",
"DEPTNO"
from "EMP"
where
(
instr(upper("ENAME"),upper(nvl(:P1_REPORT_SEARCH,"ENAME"))) > 0 or
instr(upper("JOB"),upper(nvl(:P1_REPORT_SEARCH,"JOB"))) > 0
)
My problem is, apart from having this report within one of my pages, I also have other items on the page that are required fields, so when the user clicks on 3 of the 10 checkboxes based on the above SQL and submits the page, the user is presented with an error, indicating that an item on the page is required.
Now when the user is presented with this error, the 3 out of 10 checkboxes that were checked are now unchecked.
Can I maintain checkbox persistence/state based on the above scenario? as I don't want the user re-checking the records they already checked.
I am interested in solving this with an Oracle ApEx means if possible. However, a jQuery approach may also work.
i found some way to do this, with a mix of apex and jquery:
On submit process: get the state of the checkboxes (through apex_application) and store them through apex_collections
After resfresh: get the values through an ajax callback and set them again with some javascript code.
I made a report (Report 1), based on emp
, with your query (but left out the where-clause).
My on-submit process where i get the checked boxes (through apex_application you only get the checked ones!), and keep them in a collection. This collection is created or emptied each submit. I left the debug
messages in, so you might track them.
Oh, and the value stored is empno
, since you based the checkbox item on that column, and not 1 or 0.
apex_debug_message.log_message('on submit point');
APEX_COLLECTION.CREATE_OR_TRUNCATE_COLLECTION('COLL_REPORT1');
FOR i IN 1..APEX_APPLICATION.G_F01.COUNT LOOP
apex_debug_message.log_message('loop chk: '||i||': '||TO_NUMBER(APEX_APPLICATION.G_F01(i)));
APEX_COLLECTION.ADD_MEMBER(p_collection_name => 'COLL_REPORT1',
p_n001 => TO_NUMBER(APEX_APPLICATION.G_F01(i))
);
END LOOP;
Then create an ajax callback process: I make a json array here, which simply holds the values of the checkboxes, and helps me easily parse the values afterwards. Collection is truncated after retrieval.
DECLARE
v_first BOOLEAN := TRUE;
v_json VARCHAR2(4000);
BEGIN
v_json := '[';
FOR r IN (SELECT n001 FROM APEX_collections
WHERE collection_name = 'COLL_REPORT1')
LOOP
apex_debug_message.log_message('stored value: '||r.n001);
IF NOT v_first THEN
v_json := v_json ||',';
ELSE
v_first := FALSE;
END IF;
v_json := v_json || r.n001;
END LOOP;
v_json := v_json || ']';
APEX_COLLECTION.TRUNCATE_COLLECTION('COLL_REPORT1');
htp.p(v_json);
END;
Then the values need to be retrieved and set, which is done through a dynamic action.
The code:
var oDBGet = new htmldb_Get(null, &APP_ID., 'APPLICATION_PROCESS=get_checked_boxes', &APP_PAGE_ID.);
var arrBoxes = jQuery.parseJSON(oDBGet.get());
$.each(arrBoxes,
function( intIndex, objValue ){
$("input[name='f01'][value='"+objValue+"']").attr("checked","checked");
});
This code simply calls the ajax callback and parses the returned string as a json object (which is an array due to [value,value,value] syntax).
I then loop over this array and set the values on the input elements with name f01
(dynamically created checkboxes, this is the based on the id given to apex_item.checkbox), and where the value equals that of the value we have stored. Through attr
the box gets checked!
Here's hoping i didn't overdo it :)