Ina
Ina

Reputation: 3

How to save checked state of checkbox field after clicking on button in Apex 5?

I have interactive report with this query:

select htmldb_item.checkbox(1,rownum) checked,
col_1,att_dt,reason
from table;

I am new in Apex. I would like to create button which save checked state of column checked. Can you help me with dynamic action here?

Upvotes: 0

Views: 1926

Answers (1)

Koen Lostrie
Koen Lostrie

Reputation: 18665

To work with checkboxes in a report, use APEX_ITEM.CHECKBOX2 instead. I would no longer use htmldb_item since that is from much older versions of apex. The documentation is pretty exhaustive: there is an example of how to render it in a report and an example of how to create an on-submit process. Are you sure you need a dynamic action for this ? Typically this would not be done in a dynamic action but in a page process that is run on submit (which is the "traditional" way to do things in apex).

This is how you would do it with a page process:

  1. Create table
-- create tables
create table checkbox_test (
    id                             number generated by default on null as identity 
                                   constraint checkbox_test_id_pk primary key,
    name                           varchar2(30 char),
    checked_flag                   varchar2(1 char) default on null N'
)
;

-- load data
 
insert into checkbox_test (id, name, checked_flag ) values (1, 'Machine Portal', 'N');
insert into checkbox_test (id, name, checked_flag ) values (2, 'Blockchain System Rollout', 'N');
insert into checkbox_test (id, name, checked_flag ) values (3, 'JavaScript Web Project', 'N');
insert into checkbox_test (id, name, checked_flag ) values (4, 'Pay Equity Module', 'N');
insert into checkbox_test (id, name, checked_flag ) values (5, 'Cloud transition', 'N');
commit;
  1. Create interactive report with source sql
select ID,
       NAME,
       CHECKED_FLAG,
       APEX_ITEM.CHECKBOX2(1,id,CASE WHEN checked_flag = 'Y' THEN 'CHECKED' ELSE NULL END ) "Select"
  from CHECKBOX_TEST
  1. Create a SAVE button with action "Submit Page"

  2. Create a Page Process to update your table with pl/sql code:

DECLARE
  l_found BOOLEAN;
BEGIN
FOR r IN (SELECT * FROM checkbox_test) LOOP
  l_found := false;
  FOR I in 1..APEX_APPLICATION.G_F01.COUNT LOOP
    IF r.id = to_number(APEX_APPLICATION.G_F01(i)) THEN
      UPDATE checkbox_test 
         SET checked_flag = 'Y' 
       WHERE id = r.id
         AND checked_flag = 'N';
      l_found := true;
    END IF;
  END LOOP;
   -- this row was not checked - need to update if it was unchecked
  IF NOT l_found THEN
    UPDATE checkbox_test 
       SET checked_flag = 'N' 
     WHERE id = r.id
       AND checked_flag = 'Y';    
  END IF;  
END LOOP;
END;

Upvotes: 1

Related Questions