• Aucun résultat trouvé

Updating a Collection via an On-Demand Process

Dans le document Oracle Application Express (Page 193-200)

Since a collection is only a snapshot of the original data, you can use a different approach for an update.

You can create some JavaScript code and combine that with an On-Demand Process to update the collection when a single Tabular Form item changes. The validation of the collection values will be done only if you decide to save the collection back to the source. You will include only some basic validations for numeric and date columns in this code.

The code you need to write for this demonstration consists of

• One procedure for updating a collection member

• Two small JavaScript functions

• Three application items you will use for parsing parameters: T_COL_VAL_ITEM, T_COL_SEQ_ITEM, T_COL_ATTR_ITEM

• One On-Demand Process to call your procedure

 Note Always use the same prefix for application items. In this case you are using T_, but you could use any

other letter or any combination of letters in the front.

The procedure code you will add to your package is shown in Listing 3-11.

Listing 3-11. Tabular Form—Collection— Update on Demand PROCEDURE update_emp_coll_member (

p_seq_id IN NUMBER, p_attribute_number IN NUMBER, p_attribute_value IN VARCHAR2 );

PROCEDURE update_emp_coll_member (

CHAPTER 3  TABULAR FORMS

apex_collection.update_member_attribute (p_collection_name => v_collection,

The JavaScript code shown in Listing 3-12, added to the page HTML header of application page 3, will call the On-Demand Process and check the return value of that process for errors.

Listing 3-12. Tabular Form—Collection—Ajax

<script language="JavaScript" type="text/javascript">

function LPad(ContentToSize,PadLength,PadChar) {

var PaddedString=ContentToSize.toString();

for(i=ContentToSize.length+1;i<=PadLength;i++) {PaddedString=PadChar+PaddedString;}

return PaddedString;

}

function f_update_emp_coll_member(pThis,pRownum,pAttribNo){

var v_seq_array = 'f02_' + LPad(pRownum, 4, "0");

var SeqID = $x(v_seq_array).value;

var get = new htmldb_Get(null,$x('pFlowId').value,

'APPLICATION_PROCESS=update_emp_coll_member',0);

get.add('T_COL_VAL_ITEM',pThis);

get.add('T_COL_SEQ_ITEM',SeqID);

get.add('T_COL_ATTR_ITEM',pAttribNo);

gReturn = get.get();

if(gReturn) {alert(gReturn)}

get = null;

}

</script>

The code to implement the On-Demand Process update_emp_coll_member will be a simple PL/SQL block:

BEGIN

tab_form_emp_pkg.update_emp_coll_member (:t_col_seq_item, :t_col_attr_item,

:t_col_val_item);

END;

The last thing you need to do is to call this code from the Tabular Form. You do that by entering code similar to the following for each column on the form. Place the code in the Element Attributes field under Column Properties. Following is the code for the ENAME column:

onchange="f_update_emp_coll_member(this.value,'#ROWNUM#',2);"

The third parameter of the JavaScript function is the attribute number of the column in question.

Take care to adjust that value to the right one for each column on the form. The mapping in the view code (Listing 3-9) can help you do that.

Now run page 3 and try to update some of the rows in the Tabular Form, as shown in Figure 3-31.

Figure 3-31. Updating rows on the tabular form

You can also activate Firebug and watch what happens at the browser level, as shown in Figure 3-32.

Activate Firebug and switch to the console tab. After updating a column, you should see the process running in Firebug. Opening that process will display more detail.

CHAPTER 3  TABULAR FORMS

Figure 3-32. Watching the On-Demand Process from Firebug

The final step in this exercise is to save the collection data back to the table. For that you will create

• One validation function that will take care of the data integrity

• One procedure that will save the data back to the table Listing 3-13 shows the code for these procedures.

Listing 3-13. Tabular Form—Saving Collection Data Back to the Source FUNCTION validate_collection_data

RETURN VARCHAR2;

PROCEDURE update_table_from_collection ( p_deptno IN NUMBER,

valid

PROCEDURE update_table_from_collection ( p_deptno IN NUMBER,

CHAPTER 3  TABULAR FORMS

create_emp_collection (p_deptno, v_message);

p_message :=

To be able to do an update of the table, you will need to create a button on your page 3 and name it SAVE_DATA

You can now call the validation function and the update process on application page 3. You will create a page level validation of type PL/SQL Function Returning Error Message – Validate Collection Data and make it conditional upon the new button you created (SAVE_DATA):

BEGIN

RETURN tab_form_emp_pkg.validate_collection_data;

END;

The process Save Collection to Table will be on submit, and it will be conditional when the button is pressed (SAVE_DATA). You will also enter

&T_MESSAGE.

in the Success Message of the process BEGIN

tab_form_emp_pkg.update_table_from_collection

(:p3_deptno,:t_message);

END;

Now make a couple of changes in your collection and confirm that the code works as expected.

Figure 3-33 shows some changes to be saved to the source table. Figure 3-34 shows the success message from saving those changes.

Figure 3-33. Changes to be saved to the source table

CHAPTER 3  TABULAR FORMS

Figure 3-34. The success message

 Caution The examples shown here are not complete, and they focus on single functionalities. If you want to use

the example code in your applications, you will need to complete functionality that I’ve omitted in order to keep the

examples simple. For example, you will need to take care of newly-added rows when updating a collection using

an On-Demand Process. The code shown here doesn’t do that. However, that functionality should be fairly easy to

add.

Dans le document Oracle Application Express (Page 193-200)