Oracle® HTML DB 2 Day Developer Release 1.6 Part Number B14377-01 |
|
|
PDF · Mobi · ePub |
JavaScript is commonly used in Web applications as logic executed in the Web browser to provide functionality such data validations or dynamic user interface behaviors. Oracle HTML DB provides several ways to include JavaScript libraries and endless ways to incorporate the calling of JavaScript functions into the user interface.
This tutorial describes some usage scenarios for JavaScript and includes details about how to implement them in your application.
This section contains the following topics:
There are two primary places to include JavaScript functions:
In the HTML Header attribute of the page
In a .js file in the page template.
One way to include JavaScript into your application is add it to the HTML Header attribute of the page. This is good approach for functions that are very specific to a page as well as a convenient way to test a function before you include it in the .js
file.
You can add JavaScript functions to a page by simply entering the code into the HTML Header attribute of the Page Attributes page. For example, adding following would test
function accessible from anywhere on the current page.
<script language="JavaScript1.1" type="text/javascript> function test(){ alert('This is a test.'); } </script>
In Oracle HTML DB you can reference a .js
file in the page template. This approach makes all the JavaScript in that file accessible to the application. This is the most efficient approach since a .js
file loads on the first page view of your application and is then cached by the browser.
The following demonstrates how to include a .js file in the header section of a page template.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>#TITLE#</title> #HEAD# <script src="#APP_IMAGES#custom.js" type="text/javascript"></script> </head> <body #ONLOAD#>#FORM_OPEN#
When you reference an item, the best approach is to reference by ID. If you view the HTML source of an Oracle HTML DB page in a Web browser, you would notice that all items have an ID attribute. This ID corresponds to name of the item, not the item label. For example, if you create an item with the name P1_FIRST_NAME
and a label of First Name
, the ID will be P1_FIRST_NAME.
Knowing the item ID enables you to use the JavaScript function getElementById
to get and set item attributes and values. The following example demonstrates how to reference an item by ID and display its value in an alert box.
<script language="JavaScript1.1" type="text/javascript"> function firstName(){ alert('First Name is ' + document.getElementById('P1_FIRST_NAME').value ); } // or a more generic version would be function displayValue(id){ alert('The Value is ' + document.getElementById(id).value ); } </script> // Then add the following to the "Form Element Attributes" Attribute of the item: onChange="javascript:displayValue('P1_FIRST_NAME');"
Calling a JavaScript from a button is a great way to confirm a request. Oracle HTML DB actually uses this technique for the delete operation of most objects. For example, when you delete a button, a JavaScript Alert Box appears asking you to confirm your request. Consider the following example:
<script language="JavaScript1.1" type="text/javascript"> function deleteConfirm(msg) { var confDel = msg; if(confDel ==null) confDel= confirm("Would you like to perform this delete action?"); else confDel= confirm(msg); if (confDel== true) doSubmit('Delete'); } </script>
This example creates a function to confirm a delete action and then calls that function from a button. Note that the function optionally submits the page and sets the value of the internal variable :REQUEST
to "Delete," thus performing the delete using a process that conditionally executes based on the value of request.
Note that when you create the button you would need to select the Action Redirect to URL without submitting page . Then, you would specify a URL target such as the following:
javascript:confirmDelete('Would you like to perform this delete action?');
Client side validations give immediate feedback to users using a form. One very common JavaScript validation is field not null. For example, you can create a function in the HTML Header attribute of a page and then call that function from an item.
Creating this type of JavaScript validation involves the following steps:
Create a new application on the EMP table.
Create an item on page 1 called P1_ENAME that has a label of Employee Name.
Add a function to the HTML Header attribute on page 3
Edit the P3_ENAME item on page 3 to call the function
Topics in this section include:
To create a new application on the EMP
table:
Navigate to the Workspace home page.
Click Create Application.
On Select Creation Method, select Based on Existing Table and click Next.
On Identify Table/View Owner, select the owner of the EMP
table and click Next.
On Identify Table /View Name, select EMP and click Next.
On Table User Interface Defaults, accept the defaults and click Next.
For Summarize by Column, select the columns JOB, MGR, and DEPTNO and click Next.
On Aggregate by Column, select SAL and COMM and click Next.
Accepts the remaining defaults and click Next.
Click Create.
To view the application, click Run Application.
When prompted for a user name and password:
For User Name, enter the name of your workspace
For Password, enter the password for your workspace
The application contains the following pages:
a standard report
an insert form
an update form
a success form (indicates when a record is successfully inserted)
an analysis menu page
analysis reports
analysis charts
a login page
Select Edit Application from the Developer Toolbar.
To add function to the HTML Header attribute on page 2:
Navigate to the Page Definition for page 2, Insert Form.
On the Page Definition, click Edit Attributes.
The Page Attributes appear.
In HTML Header, enter the following:
<script language="JavaScript1.1" type="text/javascript"> function notNull(object){ if(object.value=="") alert('This field must contain a value.'); } </script>
Click Apply Changes.
The next step is to edit the P2_ENAME
item and add code to the HTML Form Element Attributes attribute to call the function.
To edit the P2_ENAME
item to call the function:
Navigate to the Page Definition for page 2, Insert Form.
Under Items, select P2_ENAME.
Scroll down to Element.
In HTML Form Element Attributes, enter the following:
onBlur="javascript:notNull(this);"
Click Apply Changes.
Run the page. If you position the cursor in the Ename
field and click Create, the following message appears:
This field must contain a value.
While Oracle HTML DB enables you to conditionally display a page item, it is important to note that a page must be submitted for any changes on the page to be evaluated. The following example demonstrates how to use JavaScript to disable a form element based on the value of another form element.
First, you write a function and place it in the HTML Header attribute of the page containing your update form. Second, you call the function from an item on the page. The following example demonstrates how to add a JavaScript function to prevent users from adding commissions to employees who are not in the Sales Department (deptno = 30).
Topics in this section include:
To add a function to the HTML Header attribute on page 3:
Navigate to the Page Definition for page 3, Update Form.
On the Page Definition, click Edit Attributes.
The Page Attributes appear.
In HTML Header, enter the following:
<script language="JavaScript1.1" type="text/javascript"> // This function takes in: // 1. A string expression to evaluate. For example: // 'document.getElementById(\'P2_DEPTNO\').value==\'0\' // Notice the quotes are escaped using a "\" // 2. One or more arguments which are item ID's as strings. For example: // ...,'P2_ENAME','P2_SAL'...); // Notice the ID's are the item names, NOT item labels function disFormItems(testString,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10){ if(theTest){ for(var i=1;i<12;i++){ if (arguments[i]){ disItem = document.getElementById(arguments[i]); disItem.style.background = '#cccccc'; disItem.disabled = true; } } } else{ for(var i=1;i<12;i++){ if (arguments[i]){ disItem = document.getElementById(arguments[i]); disItem.disabled = false; disItem.style.background = '#ffffff'; } } } } </script>
Click Apply Changes.
The next step is to edit the P3_DEPTNO
item and add code to the HTML Form Element Attributes attribute to call the function.
To edit the P3_DEPTNO
item to call the function:
Navigate to the Page Definition for page 3.
Under Items, select P3_DEPTNO.
Scroll down to Element.
In HTML Form Element Attributes, enter the following:
onChange="javascript:disFormItems('document.getElementById(\'P3_DEPTNO\').value!=\'30\'','P3_COMM');"
Click Apply Changes.
To change the P3_DEPTNO
to display as a select list:
Navigate to the Page Definition for page 3.
Under Items, select P3_DEPTNO.
Under Display As, select Select List.
Under List of Values:
From Display Null, select No.
In List of Values definition, enter:
SELECT dname_id, deptno FROM dept
Click Apply Changes.
Finally, you need to create a call to the disFormItems
function after the page is rendered to disable P3_COMM
if the selected employee is not a SALES representative. A good place to make this call would be from the Region Footer of the region which contains the items.
To disable P3_COMM
when the page is first loaded:
Navigate to the Page Definition for page 3.
Under Regions, select EMP.
Scroll down to Header and Footer Text.
In Region Footer Header, enter the following:
<script language="JavaScript1.1" type="text/javascript"> // This code calls the function when the page loads, thus setting the items state correctly. disFormItems('document.getElementById(\'P3_DEPTNO\').value!=\'30\'','P3_COMM'); </script>
Click Apply Changes.
Run the page.
Figure Figure 9-1 demonstrates the completed form.
In the following example, there are four text boxes in a region. The fourth text box contains the sum of the other three. To calculate this sum you will add a JavaScript function to the HTML Header attribute and then call that function from the first three items
To call the function from the first three items:
Navigate to the appropriate Page Definition.
On the Page Definition, click Edit Attributes.
The Page Attributes appear.
In HTML Header, enter the following:
<script language="JavaScript1.1" type="text/javascript"> function sumItems(){ function getVal(item){ if(document.getElementById(item).value != "") return parseFloat(document.getElementById(item).value); else return 0; } document.getElementById('P1_TOTAL').value = getVal('P1_ONE') + getVal('P1_TWO') + getVal('P1_THREE'); } </script>
Click Apply Changes.
To call the function from all three items:
Navigate to the appropriate Page Definition.
For each item:
Select the item name.
Scroll down to Element.
In HTML Form Element Attributes, enter:
onChange="javascript:sumItems();"
Click Apply Changes.