The DISPLAY ARRAY Statement
The DISPLAY ARRAY
statement lets the user view the contents of an
array of records, scrolling through the display.
The example defines a program array of records, each record having members that
correspond to the fields of the screen records defined in the form specification file.
The DISPLAY ARRAY
statement displays all the records in the program
array into the rows of the screen array. Typically the program array has many more rows
of data than will fit on the screen.
The COUNT attribute
When using a static array, the number of rows to be displayed is defined by the
COUNT
attribute. If you do not use the COUNT
attribute, the runtime system cannot determine how much data to display, and so the screen
array remains empty.
When using a dynamic array, the number of rows to be displayed is defined by the number of
elements in the dynamic array; the COUNT
attribute is ignored.
Example:
01
DISPLAY ARRAY
cust_arr TO
sa_cust.*
This statement will display the program array cust_arr
to the form fields
defined in the sa_cust
screen array of the form.
By default, the DISPLAY ARRAY
statement does not terminate until the user
accepts or cancels the dialog; the Accept
and Cancel
actions are predefined and display on the form. Your program can accept the dialog
instead, using the ACCEPT DISPLAY
instruction.
The ARR_CURR function
When the user accepts or cancels a dialog, the ARR_CURR
built-in
function returns the index (subscript number) of the row in the program array that was
selected (current).
Example Library module: cust_lib.4gl
The cust_lib.4gl module contains the library function
display_custarr
, that can be reused by other
programs that reference the customer
table.
01
SCHEMA
custdemo
02
03
FUNCTION
display_custarr()
04
05
DEFINE
cust_arr DYNAMIC ARRAY OF RECORD
06
store_num LIKE
customer.store_num,
07
store_name LIKE
customer.store_name,
08
city LIKE
customer.city,
09
state LIKE
customer.state,
10
zip_code LIKE
customer.zip_code,
11
contact_name LIKE
customer.contact_name,
12
phone LIKE
customer.phone
13
END RECORD,
14
cust_rec RECORD
15
store_num LIKE
customer.store_num,
16
store_name LIKE
customer.store_name,
17
city LIKE
customer.city,
18
state LIKE
customer.state,
19
zip_code LIKE
customer.zip_code,
20
contact_name LIKE
customer.contact_name,
21
phone LIKE
customer.phone
22
END RECORD,
23
ret_num LIKE
customer.store_num,
24
ret_name LIKE
customer.store_name,
25
curr_pa SMALLINT
26
27
OPEN WINDOW
wcust WITH FORM
"manycust"
28
29
DECLARE
custlist_curs CURSOR FOR
30
SELECT
store_num,
31
store_name,
32
city,
33
state,
34
zip_code,
35
contact_name,
36
phone
37
FROM
customer
38
ORDER BY
store_num
39
40
41
CALL
cust_arr.clear()
42
FOREACH
custlist_curs INTO
cust_rec.*
43
CALL
cust_arr.appendElement()
44
LET
cust_arr[cust_arr.getLength()].* = cust_rec.*
45
END FOREACH
46
47
LET
ret_num = 0
48
LET
ret_name = NULL
49
50
IF
(cust_arr.getLength() > 0) THEN
51
DISPLAY
ARRAY
cust_arr TO
sa_cust.*
52
IF NOT INT_FLAG THEN
53
LET
curr_pa = arr_curr()
54
LET
ret_num = cust_arr[curr_pa].store_num
55
LET
ret_name = cust_arr[curr_pa].store_name
56
END IF
57
END IF
58
59
CLOSE WINDOW
wcust
60
RETURN
ret_num, ret_name
61
62
END FUNCTION
- Lines
05
thru13
define a local program array,cust_arr
. - Lines
14
thru22
define a local program record,cust_rec
. This record is used as temporary storage for the row data retrieved by theFOREACH
loop in line 42. - Lines
23
and24
define local variables to hold the store number and name values to be returned to the calling function. - Line
25
defines a variable to store the value of the program array index. - Line
27
opens a window with the form containing the array. - Lines
29
thru38
DECLARE
the cursorcustlist_curs
to retrieve the rows from thecustomer
table. - Line
40
sets the variableidx
to 0, this variable will be incremented in theFOREACH
loop. - Line
41
clear the dynamic array. - Line
42 uses
FOREACH
to retrieve each row from the result set into the program record,cust_rec
. - Lines
43
thru44
are executed for each row that is retrieved by theFOREACH
. They append a new element to the arraycust_arr
, and transfer the data from the program record into the new element, using the methodgetLength
to identify the index of the element. When theFOREACH
statement has retrieved all the rows the cursor is closed and theFOREACH
is exited. - Lines
47
and48
Initialize the variables used to return the customer number and customer name. - Lines
50
thru57
If the length of thecust_arr
array is greater than 0, theFOREACH
statement did retrieve some rows. - Line
52
DISPLAY ARRAY
turns control over to the user, and waits for the user to accept or cancel the dialog. - Line
52
TheINT_FLAG
variable is tested to check if the user validated the dialog. - Line
53
If the user has validated the dialog, the built-in functionARR_CURR
is used to store the index for the program array element the user had selected (corresponding to the highlighted row in the screen array) in the variablecurr_pa
. - Lines
54
and55
The variablecurr_pa
is used to retrieve the current values ofstore_num
andstore_name
from the program array and store them in the variablesret_num
andret_name
. - Line
59
closes the window. - Line
60
returnsret_num
andret_name
to the calling function.