Skip Headers
Oracle® Database Express Edition 2 Day Plus Application Express Developer Guide
Release 2.1

Part Number B25310-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

5 How to Work with Check Boxes

In Oracle Application Express, you can create check boxes as form elements, or you can create check boxes in reports. Check boxes on a form work similarly to lists of values. When you define an item to be a check box, you need to provide the check box values in the List of Values section of the Item Attributes page. You define check boxes on a report using the supplied function HTMLDB_ITEM.CHECKBOX.

This tutorial illustrates the different ways you can create check boxes within the demonstration application, Sample Application and explains how to reference and process the values of checked boxes.

This section contains the following topics:

About Sample Application

Application Builder installs with a number of demonstration applications. In this exercise you create check boxes within the demonstration application, Sample Application.

To see if Sample Application is installed:

  1. Log in to the Database Home Page using your database username and password.

  2. Click the down arrow on the right side of the Application Builder icon.

  3. From the menu, select Demonstrations.

    The Demonstration Applications page appears, displaying links to demonstration applications.

  4. Locate Sample Application and check the Status column:

    1. If the Status column displays Installed, return to the Database Home Page.

    2. If the Status column displays Not Installed, select Install in the Action column. Follow the on-screen instructions.

Creating a Single Value Check Box on a Form

As shown in Figure 5-1, the Add/Modify Products form enables users to add new products to the database.

Figure 5-1 Add/Modify Products Form in Sample Application

Description of Figure 5-1 follows
Description of "Figure 5-1 Add/Modify Products Form in Sample Application"

In this exercise you change the Product Available radio group to a check box and then change the position of the label.

Topics in this section include:

Change Product Available Radio Group to a Check Box

To change the Product Available radio group to a check box:

  1. On the Database Home Page, click the Application Builder icon.

  2. Click Sample Application.

    The Application home page appears.

  3. Navigate to the Page Definition for page 6:

    1. In the Page field, enter 6 and click Go.

    2. Click 6 - Add/Modify.

    The Page Definition for page 6 appears. The Product Available radio group is an item named P6_PRODUCT_AVAIL.

  4. Under Items, select P6_PRODUCT_AVAIL.

  5. From Display As, select Checkbox.

  6. Scroll down to List of Values.

  7. Under List of Values:

    1. From Named LOV, select Select Named LOV.

    2. In List of values definition, enter:

      STATIC:;Y
      
      

      This list of values is a static list. In this example, the display value is null and the return value is Y. If a display value were provided, it would appear to the left of the check box and could be used in place of the label.

  8. Scroll back to the top of the page and click Apply Changes.

    The Page Definition appears.

  9. Click the Run Page icon in the upper right corner. (See Figure 5-2).

  10. If prompted to enter a username and password:

    1. For User Name, enter either demo or admin.

    2. For Password, enter your database user name in lowercase letters.

    3. Click Login.

      Sample Application appears.

  11. Navigate to page 6. Select Edit Page 1 from the Developer Toolbar at the bottom of the page.

    The Page Definition appears.

  12. In the Page field, enter 6 and click Go.

  13. Click the Run Page icon in the upper right corner.

As shown in Figure 5-3, note that the Product Available item now displays as a check box.

Figure 5-3 Product Available Item as a Check Box

Description of Figure 5-3 follows
Description of "Figure 5-3 Product Available Item as a Check Box"

Alter the Check Box Position

Next, you move the check box label to the right side of the check box.

To change the position of the check box label:

  1. Navigate to the Page Definition for page 6. Select Edit Page 6 from the Developer Toolbar.

  2. Under Items, select P6_PRODUCT_AVAIL.

  3. Scroll down to Label.

  4. For Label, delete the text Product Available.

  5. Scroll down to Lists of Values.

  6. Under List of Values, change the List of values definition to:

    STATIC:Product Available;Y
    
    
  7. Scroll back to the top of the page and click Apply Changes.

  8. Run the page by clicking the Run Page icon.

As shown in Figure 5-4, note that the label Product Available now displays to the right of the check box.

Figure 5-4 Product Available Label Moved to the Right

Description of Figure 5-4 follows
Description of "Figure 5-4 Product Available Label Moved to the Right"

Removing the label and adding the display value to the LOV causes the Application Express engine to render the check box first and then the display value, Product Available.

Change Default Check Box Behavior

In certain circumstances, you may want a check box to be enabled by default. You can accomplish this by setting the default value attribute of the check box item. One disadvantage of this approach is that you need to perform some extra steps to disable it. Because of the way you defined the Product Available check box, it is virtually impossible to disable it.

Consider the following example:

  1. Within Sample Application, navigate to the Home page by selecting the Home tab.

  2. From the Tasks list, click Add a New Product.

  3. On the Add/Modify Products page:

    1. Fill in the required fields (fields marked with an asterisk).

    2. Disable the Product Available check box.

    3. Click Create.

  4. Disable the Product Available check box again and click Apply Changes.

    The Products page appears.

  5. Select the Edit icon for product you just added.

    Notice that the Product Available check box is enabled even though you disabled it twice when you added the product. This behavior results from the fact:

    • The Product Available check box has a default value of Y.

    • When Product Available is NULL, it defaults to the default value Y which enables the check box.

Add a Computation

You can alter this behavior by adding a computation that remembers the state of the check box.

To add a computation that tracks the state of the check box:

  1. Navigate to the Page Definition for page 6. Select Edit Page 6 from the Developer Toolbar.

  2. Under Page Processing, Computations, click the Create icon.

  3. For Location, select Item on This Page and click Next.

  4. For Item:

    1. For Compute Item, select P6_PRODUCT_AVAIL.

    2. For Computation Point, select After Submit.

    3. For Computation Type, select Static Assignment.

    4. Click Next.

  5. For Computation, enter the following and click Next.

    N
    
    

    Note:

    Since this item is a check box, set the value of the item to something other than Y or NULL.

    Next, create a condition that controls when the computation executes.

  6. On Condition:

    1. For Condition Type, select Value of Item in Expression 1 is NULL.

    2. In Expression 1, enter:

      P6_PRODUCT_AVAIL
      
      

      Because of these settings, this computation will only execute when the value of the check box item, P6_PRODUCT_AVAIL is NULL.

  7. Click Create.

To test the new computation:

  1. Click the Run Page icon.

    Sample Application appears.

  2. Navigate to the Home page by selecting the Home tab.

  3. From the Tasks list, select Add a New Product.

  4. On the Add/Modify Product page:

    1. Fill in the required fields (fields marked with an asterisk).

    2. Disable the Product Available check box.

    3. Click Create.

  5. Disable the Product Available check box again and click Apply Changes.

    The Products page appears.

  6. Select the Edit icon for product you just added.

    Note that the Product Available check box is not selected.

Creating Multi Value Check Box to Filter Content

In the next exercise, you create a multi value check box on the Product page. This check box enables users to filter the report by selecting a category.

Topics in this section include:

Create a Multi Value Check Box

To a create muli value check box:

  1. Navigate to the Page Definition for page 3:

    1. Click the Edit Page link on the Developer Toolbar.

    2. Enter 3 in the Page field and click Go.

      The Page Definition for page 3 appears.

  2. Under Items, click the Create icon.

  3. For Item Type, select Check Box and click Next.

  4. On Display Position and Name:

    1. In Item Name, enter P3_SHOW.

    2. For Sequence, accept the default.

    3. For Region, select Products (1) 10.

    4. Click Next.

  5. For List of Values:

    1. For Named LOV, accept the default.

    2. For Display Null Option, select No.

    3. In List of Values Query, enter:

      SELECT distinct category a, category b
      FROM demo_product_info
      ORDER BY 1
      
      

      Note that to create a multi value check box, the List of Values query needs to return more than one row.

    4. Click Next.

  6. On Item Attributes:

    1. For Label, remove the default text by clicking Clear.

    2. Accept the remaining defaults and click Next.

  7. For Source:

    1. For Item Source, select Static Assignment.

    2. For Item Source Value, enter:

      Audio:Computer:Phones:Video
      
      

      When a multi value check box is submitted, the value of the item is a colon delimited string of values. Using this string as the source ensures all boxes are checked when Application Express engine renders the page.

    3. Click Create Item.

Alter Check Box Display Values

To edit check box display values (or labels) to appear in bold:

  1. Navigate to the Page Definition for page 3.

  2. Under Items, select P3_SHOW.

  3. Scroll down to Element.

  4. In Form Element Option Attributes, enter:

    class="fielddatabold"
    
    

    Form Element Option Attributes are used exclusively for check boxes and radio buttons and control the way the Application Express engine renders individual options.

  5. Click Apply Changes.

Change Where the Check Boxes Display

Next, you edit attributes so that the category check boxes display above the report. To accomplish this, you need to change the Display Point attribute of the region associated with P3_SHOW.

To change the display point of the Products region:

  1. Navigate to the Page Definition for page 3.

  2. Under Region, select Products.

  3. From Display Point, select Page Template Body (3. items above region content).

  4. Click Apply Changes.

If you ran page 3 now, you would notice the category check boxes display vertically. Next, you need to change the display so that the category check boxes display horizontally.

To alter where the category check boxes display:

  1. Navigate to the Page Definition for page 3.

  2. Under Items, select P3_SHOW.

  3. Scroll down to List of Values.

  4. In Columns, enter 4.

  5. Scroll back to the top of the page and click Apply Changes.

    The Page Definition for page 3 appears.

  6. Click the Run Page icon in the upper right corner.

    Note the check boxes display horizontally at the top of the page as shown in Figure 5-5.

    Figure 5-5 Category Check Boxes Moved to Top of Page

    Description of Figure 5-5 follows
    Description of "Figure 5-5 Category Check Boxes Moved to Top of Page"

Create a Go Button to Submit the Page

In order for the report to be driven by the product category check boxes, you need to submit the page.

To create a button to submit the page:

  1. Navigate to the Page Definition for page 3.

  2. Under Buttons, click the Create icon.

  3. For Button Region, select Products (1) 10 and click Next.

  4. For Position, select Create a button displayed among this region's items and click Next.

  5. For Button Attributes:

    1. For Button Name, enter P3_GO.

    2. In Sequence, enter 40.

    3. Accept the remaining defaults and click Create Button.

Next, you need to create a branch to tell the Application Express engine where to go after the user clicks the Go button.

To create a branch to page 3:

  1. Navigate to the Page Definition for page 3.

  2. Under Branches, click the Create icon.

  3. For Branch Point and Branch Type, accept the defaults and click Next.

  4. For Page, select 3, accept the remaining defaults, and click Next.

  5. Click Create Branch.

In order to generate a report based on the category check box values, you need to change the report query.

To edit the report query:

  1. Under Regions, select Products.

  2. Scroll down to Source.

  3. In Region Source, locate the WHERE clause.

  4. Change the WHERE clause to read:

    WHERE p.image_id = i.image_id (+)AND instr(':'||:P3_SHOW||':',category) > 0
    
    
  5. Scroll back to the top of the page and click Apply Changes.

  6. Run the page by clicking the Run Page icon.

As shown in Figure 5-6, notice you can filter the report by selecting a category check box and clicking Go at the top of the page.

Figure 5-6 Product Page with Category Check Boxes and Go Button

Description of Figure 5-6 follows
Description of "Figure 5-6 Product Page with Category Check Boxes and Go Button"

Adjust Default Check Box Behavior

Although the category check boxes correctly filter the content on page 3, if you deselect all the check boxes, notice the report returns all products. This behavior results from the fact that if a check box has a NULL value (that is, it is deselected), it defaults to the default value Y. The default value of Y in turn enables the check box.

You can alter this behavior by adding a computation that remembers the state of the check box.

To add a computation that tracks the state of the check box:

  1. Navigate to the Page Definition for page 3.

  2. Under Page Processing, Computations, click the Create icon.

  3. For Item Location, select Item on This Page and click Next.

  4. For Item:

    1. For Compute Item, select P3_SHOW

    2. For Computation Point, select After Submit.

    3. For Computation Type, select Static Assignment.

    4. Click Next.

  5. For Computation:

    1. In Computation, enter the following:

      none(bogus_value)
      
      
    2. Click Next.

    Note:

    A static assignment of an item needs to be something other than Y or NULL.

    Next, create a condition that controls when the computation executes.

  6. On Condition:

    1. For Condition Type, select Value of Item in Expression 1 is NULL.

    2. In Expression 1, enter:

      P3_SHOW
      
      

      As a result of these settings, this computation will only execute when the value of the check box item, P3_SHOW is NULL.

  7. Click Create.

Run the page again and deselect all the category check boxes and click Go. Notice that this time the report contains the expected result (no returned records).

Adding Check Boxes to Each Row in the Report

In the next exercise, you add a delete check box to each row in the Products report. To accomplish this you edit the report query and make a call to HTMLDB_ITEM package.

HTMLDB_ITEM is a supplied package for generating certain form elements dynamically. In this instance, you use HTMLDB_ITEM.CHECKBOX to generate check boxes in the Products report. When the page is submitted, the values of the check boxes are stored in global package arrays. You can reference these values using the PL/SQL variables HTMLDB_APPLICATION.G_F01 to HTMLDB_APPLICATION.G_F50 based on the p_idx parameter value that was passed in.

Topics in this section include:

Call HTMLDB_ITEM.CHECKBOX

To edit the query to call HTMLDB_ITEM.CHECKBOX:

  1. Navigate to the Page Definition for page 3.

  2. Under Regions, click Products.

  3. Scroll down to Source.

  4. In Region Source, add the new line appearing in bold face to the query.

    SELECT p.product_id edit_product, p.product_id view_product_id,
    htmldb_item.checkbox(1,p.product_id) del, 
    p.product_name, p.product_description, p.category, p.product_avail, p.list_price, 
    '<img src="p?n=' || nvl(i.file_object_id,0) || '" height="50" width="50" />' img
    FROM demo_product_info p, demo_images i
    WHERE p.image_id = i.image_id (+)
    AND instr(':'||:P3_SHOW||':',category) > 0
    
    

    HTMLDB_ITEM is an Oracle Application Express supplied package you can use to generate certain form elements dynamically. Note the value passed in for p_idx in the above example is 1. You reference the check box values using the global variable HTMLDB_APPLICATION.G_F01 later on.

  5. Scroll to the top of the page and select the Report Attributes tab.

  6. Under Column Attributes, locate the Del column.

  7. Click the Up arrow until the DEL column is directly beneath VIEW_PRODUCT_ID. (See Figure 5-7).

    Figure 5-7 Report Column Attributes Page

    Description of Figure 5-7 follows
    Description of "Figure 5-7 Report Column Attributes Page"

  8. Click Apply Changes.

Add a Button to Submit Check Box Array Values

To add a button to submit the check box array values:

  1. Navigate to the Page Definition for page 3.

  2. Under Buttons, click the Create icon.

  3. For Button Region, select Products 1 (10) and click Next.

  4. For Position, select Create a button in a region position and click Next.

  5. For Button Attributes:

    1. For Button Name, enter DELETE_PRODUCTS.

    2. For Label, enter Delete Products.

    3. Accept the remaining defaults and click Next.

  6. For Button Template, accept the default selection and click Next.

  7. For Display Properties:

    1. For Position, select Top of Region.

    2. Accept the remaining defaults and click Next.

  8. For Position, select Top of Region and click Next.

  9. For Branching, select 3 Products and click Create Button.

Add a Process

To add a process that executes when the user clicks the Delete Products button:

  1. Under Page Processing, Processes, click the Create icon.

  2. For Process Type, select PL/SQL and click Next.

  3. For Process Attributes:

    1. For Name, enter Delete Products.

    2. For Point, select On Submit - After Computations and Validations.

    3. Click Next.

  4. Enter the following PL/SQL process and click Next:

    FOR i in 1..HTMLDB_APPLICATION.G_F01.count
    LOOP
       DELETE FROM demo_product_info
       WHERE product_id = HTMLDB_APPLICATION.G_F01(i);
    END LOOP;
    
    

    HTMLDB_ITEM is an Oracle Application Express supplied package you can use to generate certain form elements dynamically. When a page is submitted, the values of each column are stored in global package arrays, which you can reference using the PL/SQL variable HTMLDB_APPLICATION.G_F01 to HTMLDB_APPLICATION.G_F50. In this exercise, the value passed in for product_id in EMPNO column is 1, so you reference the EMPNO column values using the global variable HTMLDB_APPLICATION.G_F01.

  5. On Messages:

    1. In Success Message, enter:

      Product(s) deleted.
      
      
    2. In Failure Message, enter:

      Unable to delete product(s).
      
      
    3. Click Next.

  6. Click Create Process.

  7. Run the page by clicking the Run Page icon in the upper right corner.

    Notice the Delete Products button appears above the report as shown in Figure 5-8. To remove a product from the report, select the Del check box and click Delete Products.

    Figure 5-8 Products Report with Delete Products Check Box

    Description of Figure 5-8 follows
    Description of "Figure 5-8 Products Report with Delete Products Check Box"