Adding to or deleting from a collection using a dynamic action and AJAX callbacks/ ondemand process.
The dynamic action is bound to the checkbox of the report using the jQuery selector [name="f01"].
There is a when condition of this.triggeringElement.checked.
True action
The true action is of type Execute JavaScript with the following code
var l_state_id, l_data,l_options ;
l_state_id = this.triggeringElement.value;
l_data = {x01:l_state_id};
l_options = {dataType:'text'}
apex.server.process('SaveID',l_data,l_options);
SaveID is the name of AJAX callback function which has the following code
APEX_COLLECTION.ADD_MEMBER('TEST_STATES',apex_application.g_x01);
False action
On false the action is also of type Execute JavaSCript code where the code is almost identical to the true action.
The difference is the name of the AJAX callback process being called.
The code of the AJAX callback function is
declare
cursor c_col(b_id in varchar2
,b_name in varchar2)
is
select col.seq_id
from apex_collections col
where col.collection_name = b_name
and trim(upper(col.c001)) = trim(upper(b_id))
;
v_sequence number;
begin
htp.p('Id '||apex_application.g_x01);
open c_col(apex_application.g_x01,'TEST_STATES');
fetch c_col
into v_sequence;
close c_col;
htp.p('Sequence id'||v_sequence);
if v_sequence is not null
then
apex_collection.delete_member('TEST_STATES',v_sequence);
end if;
end;
To see the effect refesh actions are added to the dynamic action.
Collections
And the collection is created in a before header process
if not APEX_COLLECTION.COLLECTION_EXISTS('TEST_STATES')
then
APEX_COLLECTION.CREATE_COLLECTION('TEST_STATES');
end if;
The documentation provides more information on
APEX_COLLECTION
Reports
For completeness here are the select statements of the reports
select apex_item.checkbox(1,DEMO_STATES.ST,null,col.c001) as ST,
DEMO_STATES.STATE_NAME as STATE_NAME
from DEMO_STATES DEMO_STATES
, apex_collections col
where col.c001(+) = demo_states.st
and ( col.collection_name = 'TEST_STATES' or col.collection_name is null)
select sta.state_name
from demo_states sta
where exists (select ''
from apex_collections col
where col.collection_name = 'TEST_STATES'
and col.c001 = sta.st
)