Programming Guide : Working with Arrays, Table Fields, and Collections : Table Field Operations : Controlling Multiple Table Fields with a Single Control Button
 
Share this page          
Controlling Multiple Table Fields with a Single Control Button
You can use one control button to provide operations for multiple table fields. In fact, if you have multiple table fields on a frame, it is necessary to use a single ControlButton so that the Control Button accelerator keys will be applied to the table field that currently has input focus. Assume you have a frame with two table fields, for example:
Checkouts
This table field tracks DVDs as they are checked out and in by each customer.
Reservations
This table field tracks reservations held by customers for specific DVDs.
As customers check out a DVD, they also can reserve another disc.
You could add operations to the control buttons provided by default with each of these table fields. However, to reduce duplication of code, you can write generic code for operations common to both table fields and provide a single control button for both.
Independent control buttons are created with no menu operations. To include the standard table field operations (Insert Before Current Row, Delete Current Row, and Delete All Rows) that are provided by default with every table field control button, you can copy the 4GL code for these operations into the independent control button. You can find this code using View Processed Script in the Frame Editor. These three default table field operations are excellent examples of coding table fields generically.
How You Can Sort Table Field Data Generically
Sorting array data by specifying a column in its associated table field is a common operation. You can put a generic sort operation into a control button that is attached to a specific table field or is an independent control button field.
The following event block for a Control Button's Option Menu's Menu List lets users perform this operation, determining the names of the table field and column at runtime:
/* Script for MENULIST */
on click =

declare
   table_field = TableField default null;
   tf_array = ArrayObject default null;
   col_name = varchar(32) not null;
   sort_order = integer not null;
enddeclare
begin
/* Find out what table and column we are in,
** if any. */
if CurFrame.InputFocusField is not null then
   table_field =
   CurFrame.InputFocusField.WhichTableField();
endif;
if table_field is null then
   message 'You must be in a table field to sort
   it. ';
   resume;
endif;
col_name = CurFrame.InputFocusField.Name;
/* Point tf_array to the array underlying
** the tablefield */
table_field.GetFieldValue(value = byref(tf_array));
/*
** Use CurEnumValue to specify sort order.
** Note that the first item in the menu list
** is labeled sort in ascending order, and the
** second item to sort in descending order.
*/
if MenuList(CurFrame.TriggerField).CurEnumValue = 1
   then sort_order = AS_ASC;
else
   sort_order = AS_DESC;
endif;
/* Note the colon in front of col_name is required
** to indicate that col_name is a variable, not the
** literal column name.*/
tf_array.sort(:col_name = sort_order);
end;
For more information about the Sort method of the ArrayObject system class, see How Sorting Arrays Works. For information about using the WhichTableField method of the TableField system class, see WhichTableField Method.