 |
|
Oracle Database Tips by Donald Burleson |
Updating
the Database from APEX Form Checkboxes
The session state for a checkbox item is formatted
as a colon delimited string. In order to update the database with the
checkbox data, it is necessary to destruct the colon delimited string
into an array. It can then be looped through the array and the data
placed into a database table.
The code in file ch10_2.sql is stored in an On
Submit - After Computations and Validations page process.
There is something very important to understand
with checkboxes. Figure 10.2 shows five checkboxes. If the user
selects three of the checkboxes and submits, the session state would
look something like 2:3:1. Assume the session state for the
P2501_CLASS_ID page item was set to 5:2:3:1 during page rendering and
the user un-selected the Calculus item. Now, the problem is how to
remove the data from the XR_STUDENT_CLASS table indicating the student
has withdrawn from the Calculus class? The common practice is to
delete all data in the XR_STUDENT_CLASS table for the student and
re-insert the data from the P2501_CLASS_ID session state.
Listing 10.2 shows a DELETE statement which
deletes all the current data in the XR_STUDENT_CLASS table. The
session state for the P2501_CLASS_ID checkbox is then converted into
an array. At that point, the process loops through the array and
inserts records to the XR_STUDENT_CLASS table indicating the class the
student is enrolled in. This will probably seem like an unnecessary
task at first, but this is the usual method in APEX.
Working with Checkboxes
in Reports and Tabular Forms
Working with checkboxes on a form is one thing,
but working with checkboxes in reports or tabular forms is totally
different. How to add checkboxes to reports and tabular forms is the
sort of question often asked on the Oracle HTLM DB forum.
Populating Checkboxes
in a Report Region
The code used to populate the report in Figure
10.4 is shown in ch10_3.sql. The key to populating a report with a
checkbox is to use the htmldb_item.checkbox API function. The
parameters used are a subset of the available parameters, but an
explanation of them is necessary for a full understanding of what is
happening. Table 10.1 explains some of the parameters for the
htmldb_time.checkbox API function.
Running the SQL statement from ch10_3.sql in an
SQL Command window will result in what is shown in Figure 10.5. The
htmldb_item.checkboxfunction
returns the HTML syntax for an input of type checkbox. Also, take
note of the name. It is set to f10 because the P_IDX parameter was
set to ten. The value of each of the checkboxes is determined from
the CLASS_ID column. Finally, all checkboxes will be displayed as
checked.
Updating the Database
from Report Checkboxes
Updating the database from checkboxes in a report
is similar to that of checkboxes on a form. A separate On Submit page
process will be used to handle the data from the checkboxes. Our
example shows a report region. However, when using a tabular form, it
should be allowed to perform its database updates, and then run the
process to handle the checkboxes. Either way, a separate process will
be created.
The code in file ch10_4.sql is stored in an On
Submit - After Computations and Validations page process. It will be
executed when the page is submitted by pressing the Apply Changes
button shown in Figure 10.2.
When the page is submitted, the values from the
checkboxes are submitted in an array. Referencing the array is done
by using the htmldb_application
package. The g_f10 item is an array defined in the package. As with
the checkboxes in forms, the report checkboxes are only submitted in
the g_f10 array. Therefore, if there are four checkboxes checked,
there will be four values in the array. In the PL/SQL code in
ch10_4.sql, the first step is to update the AVAILABLE_FLAG in all the
records of the CLASS table. Then the appropriate records are updated
with a value of Y where the value in the g_f10 array item is equal to
that of the CLASS_ID.
As I said in the beginning, "Checkboxes are rather
tricky." Using the examples described in the preceding pages, it
should be possible to do most of what a developer will ever need to do
in APEX in regards to checkboxes.
The above book excerpt is from:
Easy HTML-DB
Oracle Application Express
Create
Dynamic Web Pages with OAE
ISBN 0-9761573-1-4
Michael Cunningham & Kent Crotty
http://www.rampant-books.com/book_2005_2_html_db.htm |