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
05thru13define a local program array,cust_arr. - Lines
14thru22define a local program record,cust_rec. This record is used as temporary storage for the row data retrieved by theFOREACHloop in line 42. - Lines
23and24define local variables to hold the store number and name values to be returned to the calling function. - Line
25defines a variable to store the value of the program array index. - Line
27opens a window with the form containing the array. - Lines
29thru38DECLAREthe cursorcustlist_cursto retrieve the rows from thecustomertable. - Line
40sets the variableidxto 0, this variable will be incremented in theFOREACHloop. - Line
41clear the dynamic array. - Line
42 usesFOREACHto retrieve each row from the result set into the program record,cust_rec. - Lines
43thru44are 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 methodgetLengthto identify the index of the element. When theFOREACHstatement has retrieved all the rows the cursor is closed and theFOREACHis exited. - Lines
47and48Initialize the variables used to return the customer number and customer name. - Lines
50thru57If the length of thecust_arrarray is greater than 0, theFOREACHstatement did retrieve some rows. - Line
52DISPLAY ARRAYturns control over to the user, and waits for the user to accept or cancel the dialog. - Line
52TheINT_FLAGvariable is tested to check if the user validated the dialog. - Line
53If the user has validated the dialog, the built-in functionARR_CURRis 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
54and55The variablecurr_pais used to retrieve the current values ofstore_numandstore_namefrom the program array and store them in the variablesret_numandret_name. - Line
59closes the window. - Line
60returnsret_numandret_nameto the calling function.