There are many approaches possible, to create or generate field catalog
for ALV classic style grid.
Consider following program. It defines an internal table, reads some data,
creates fieldcat and displays ALV grid with REUSE function module.
Below three possible scenarios to define a fieldcat. First and second are trivial,
perhaps the third can be interesting and useful.
Here example:
REPORT zkm_test.
TYPE-POOLS:
slis.
DATA:
BEGIN OF gt_usr OCCURS 0,
bname LIKE usr01-bname,
trdat LIKE usr02-trdat,
END OF gt_usr,
gt_fcat TYPE slis_t_fieldcat_alv.
START-OF-SELECTION.
SELECT * FROM usr02
APPENDING CORRESPONDING FIELDS OF TABLE gt_usr.
PERFORM build_fcat_1.
* PERFORM build_fcat_2.
* PERFORM build_fcat_3.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = gt_fcat
TABLES
t_outtab = gt_usr.
Scenario 1: simply specify all needed columns.
FORM build_fcat_1.
DATA:
ls_fcat TYPE slis_fieldcat_alv.
CLEAR ls_fcat.
ls_fcat-col_pos = 1.
ls_fcat-ref_tabname = 'USR02'.
ls_fcat-fieldname = 'BNAME'.
APPEND ls_fcat TO gt_fcat.
CLEAR ls_fcat.
ls_fcat-col_pos = 2.
ls_fcat-ref_tabname = 'USR02'.
ls_fcat-fieldname = 'TRDAT'.
APPEND ls_fcat TO gt_fcat.
ENDFORM.
Scenario 2: if you have many columns, it can be nice to do it with a form.
FORM build_fcat_2.
PERFORM add_fc USING 'USR02' 'BNAME' 10 'Username'.
PERFORM add_fc USING 'USR02' 'TRDAT' 10 'Last login'.
ENDFORM.
FORM add_fc USING pi_tabname pi_fieldname pi_outputlen pi_seltext.
DATA:
ls_fcat TYPE slis_fieldcat_alv,
lv_col TYPE i.
DESCRIBE TABLE gt_fcat LINES lv_col.
ADD 1 TO lv_col.
ls_fcat-col_pos = lv_col.
ls_fcat-ref_tabname = pi_tabname.
ls_fcat-fieldname = pi_fieldname.
ls_fcat-seltext_l = pi_seltext.
ls_fcat-seltext_m = pi_seltext.
ls_fcat-seltext_s = pi_seltext.
ls_fcat-reptext_ddic = pi_seltext.
ls_fcat-outputlen = pi_outputlen.
APPEND ls_fcat TO gt_fcat.
ENDFORM.
Scenario 3: use function module ‘REUSE_ALV_FIELDCATALOG_MERGE’ to generate fieldcat.
This function module parses your program, looks for specified internal table
and generates fieldcat.
You have to respect some limitations and special effects:
- your internal table with data for ALV should be declared with ‘OCCURS’
(it doesn’t matter if you use addition ‘WITH HEADER LINE’ or not),
you can’t declare your internal table with ‘TYPE TABLE OF’. - the fields of internal table should be declared with ‘LIKE’ or ‘INCLUDE STRUCTURE’,
you can’t declare the fields with ‘TYPE’. - No line of your program can be longer then 72 chars, otherwise occurs the short dump READ_REPORT_LINE_TOO_LONG with exception CX_SY_READ_SRC_LINE_TOO_LONG
- because function ‘REUSE_ALV_FIELDCATALOG_MERGE’ buffers the fieldcat, you have switch off buffering to see the changes, for example if you extend the internal table with new fields.
FORM build_fcat_3.
DATA:
lv_repid LIKE sy-repid,
lv_alvbuffer(11) TYPE c.
lv_alvbuffer = 'BFOFF EUOFF'.
SET PARAMETER ID 'ALVBUFFER' FIELD lv_alvbuffer.
lv_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = lv_repid
i_internal_tabname = 'GT_USR'
i_inclname = lv_repid
CHANGING
ct_fieldcat = gt_fcat.
ENDFORM.