ometimes you want to use an ALV Grid directly as output in a report (not a Dynpro).
The magic function to this is:
**ALVGrid
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
IT_FIELDCAT = L_IT_FIELDCAT
I_GRID_TITLE = L_VAR_TITLE
TABLES
T_OUTTAB = <L_VAR_OUTTAB>
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
You can generate your Fieldcat wth another Helpful function:
**Generate Fieldcat
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = SY-CPROG
I_INTERNAL_TABNAME = L_VAR_TABNAME
I_INCLNAME = SY-CPROG
CHANGING
CT_FIELDCAT = L_IT_FIELDCAT
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
As you can see here,the Fieldcat is generated from a local internam
table. It is mandatory to give the include the where the table is
defined. If the data structure is not in DDIC but local as well you need
to give the include where it is defined, or include it in TOP. Very
important is, that local structures are only read correctly when defined
with LIKE and INCLUDE structure as Datatypes. TYPES and normalINCLUDE
wont work, since the ddic information is not read there. Its also
written in the Manual of the function. So always RTFM cause i did it way
too late ;)
Here a sample as an MBEWH Extension is locally defined:
DATA: BEGIN OF TP_MBEWH_EXTENDED.
" MBEWH
INCLUDE STRUCTURE MBEWH.
" MARA with Key MATNR
DATA: YYBEZBE LIKE MARA-YYBEZBE,
MTART LIKE MARA-MTART,
MATKL LIKE MARA-MATKL,
MEINS LIKE MARA-MEINS,
"T001 with Key BUKRS from T001K with Key BWKEY
WAERS LIKE T001-WAERS,
"T023T with Key MATKL
WGBEZ LIKE T023T-WGBEZ.
DATA: END OF TP_MBEWH_EXTENDED.
As Update here is a small piece of Code to dynamically alocate Tables and Fieldcats to 3 different Tables:
FORM BUILD_ALV.
CHECK P_TABLE IS NOT INITIAL.
DATA: L_IT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
L_VAR_TABNAME TYPE SLIS_TABNAME,
L_VAR_TITLE TYPE LVC_TITLE,
L_VAR_LINE_CNT_OUT TYPE C LENGTH 20.
FIELD-SYMBOLS: <L_VAR_OUTTAB> TYPE STANDARD TABLE.
**Je nach Auwahl der Tabelle wird die Struktur für den Feldkatalog und
* die zugehörige Ergebnis iTab für das ALV gewählt.
CASE P_TABLE.
WHEN 'S31'.
L_VAR_TABNAME = 'TP_S031_EXTENDED'.
ASSIGN IT_S031_EXTENDED TO <L_VAR_OUTTAB>.
WHEN 'S32'.
L_VAR_TABNAME = 'TP_S032_EXTENDED'.
ASSIGN IT_S032_EXTENDED TO <L_VAR_OUTTAB>.
WHEN 'MBH'.
L_VAR_TABNAME = 'TP_MBEWH_EXTENDED'.
ASSIGN IT_MBEWH_EXTENDED TO <L_VAR_OUTTAB>.
ENDCASE.
**Feldkatalog schlicht nach Tabelle erstellen
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = SY-CPROG
I_INTERNAL_TABNAME = L_VAR_TABNAME
I_INCLNAME = SY-CPROG
CHANGING
CT_FIELDCAT = L_IT_FIELDCAT
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
"Feldkatalog individuelle Sortierung
CASE P_TABLE.
WHEN 'S31'.
PERFORM REORDER_FIELDCAT_S031 CHANGING L_IT_FIELDCAT.
WHEN 'S32'.
PERFORM REORDER_FIELDCAT_S032 CHANGING L_IT_FIELDCAT.
WHEN 'MBH'.
PERFORM REORDER_FIELDCAT_MBEWH CHANGING L_IT_FIELDCAT.
ENDCASE.
*Anzahl der Zeilen
DESCRIBE TABLE <L_VAR_OUTTAB> LINES L_VAR_LINE_CNT_OUT.
***Tabellenname in den Titel nehmen
* CONCATENATE TEXT-100 L_VAR_TABNAME L_VAR_LINE_CNT_OUT TEXT-101
* INTO L_VAR_TITLE RESPECTING BLANKS.
**ALVGrid generieren.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
IT_FIELDCAT = L_IT_FIELDCAT
I_GRID_TITLE = L_VAR_TITLE
TABLES
T_OUTTAB = <L_VAR_OUTTAB>
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. "DISPLAY_ALV_REPORT
Then at last you might want to reorder the Fieldcat
positions in your Output afterwards. Therem ight be better ways to do
this (comments welcome), but most of what i tried before was very
complicated an not error prone. The problem are some Entry which are
also hidden an not seen and mostly have empty Tabnames. So i just went
and took the First entry i knew, reordered everything from this Sy-Tabix
on forward and renumerated everythin in the end.
*&---------------------------------------------------------------------*
*& Form REORDER_FIELDCAT_MBEWH
*&---------------------------------------------------------------------*
* Feldkatalog anpassungen:
*----------------------------------------------------------------------*
* <--P_L_IT_FIELDCAT Feldcatalog zu ändern
*----------------------------------------------------------------------*
FORM REORDER_FIELDCAT_MBEWH CHANGING C_IT_FIELDCAT
TYPE SLIS_T_FIELDCAT_ALV.
DATA: L_WA_FIELDCAT_TMP TYPE SLIS_FIELDCAT_ALV,
L_VAR_TABIX TYPE SY-INDEX,
L_VAR_COL_POS_CNT TYPE I VALUE 1.
FIELD-SYMBOLS <FS_WA_FIELDCAT_POS> TYPE SLIS_FIELDCAT_ALV.
"Index der Material Spalte lesen (MATNR)
READ TABLE C_IT_FIELDCAT
WITH KEY FIELDNAME = 'MATNR' TRANSPORTING NO FIELDS.
L_VAR_TABIX = SY-TABIX + 1 .
"WGBEZ
READ TABLE C_IT_FIELDCAT
INTO L_WA_FIELDCAT_TMP
WITH KEY FIELDNAME = 'WGBEZ'.
IF SY-SUBRC = 0.
"Löschen und neu einfügen
DELETE C_IT_FIELDCAT
WHERE FIELDNAME = 'WGBEZ'.
INSERT L_WA_FIELDCAT_TMP INTO C_IT_FIELDCAT INDEX L_VAR_TABIX.
ENDIF.
"MATKL verschieben
READ TABLE C_IT_FIELDCAT
INTO L_WA_FIELDCAT_TMP
WITH KEY FIELDNAME = 'MATKL'.
IF SY-SUBRC = 0.
"Löschen und neu einfügen
DELETE C_IT_FIELDCAT
WHERE FIELDNAME = 'MATKL'.
INSERT L_WA_FIELDCAT_TMP INTO C_IT_FIELDCAT INDEX L_VAR_TABIX.
ENDIF.
"MTART
READ TABLE C_IT_FIELDCAT
INTO L_WA_FIELDCAT_TMP
WITH KEY FIELDNAME = 'MTART'.
IF SY-SUBRC = 0.
"Löschen und neu einfügen
DELETE C_IT_FIELDCAT
WHERE FIELDNAME = 'MTART'.
INSERT L_WA_FIELDCAT_TMP INTO C_IT_FIELDCAT INDEX L_VAR_TABIX.
ENDIF.
"MAKTX
READ TABLE C_IT_FIELDCAT
INTO L_WA_FIELDCAT_TMP
WITH KEY FIELDNAME = 'MAKTX'.
IF SY-SUBRC = 0.
"Löschen und neu einfügen
DELETE C_IT_FIELDCAT
WHERE FIELDNAME = 'MAKTX'.
INSERT L_WA_FIELDCAT_TMP INTO C_IT_FIELDCAT INDEX L_VAR_TABIX.
ENDIF.
"Index der Periode Spalte lesen (LFMON)
READ TABLE C_IT_FIELDCAT
WITH KEY FIELDNAME = 'LFMON' TRANSPORTING NO FIELDS.
L_VAR_TABIX = SY-TABIX + 1 .
"WAERS
READ TABLE C_IT_FIELDCAT
INTO L_WA_FIELDCAT_TMP
WITH KEY FIELDNAME = 'WAERS'.
IF SY-SUBRC = 0.
"Löschen und neu einfügen
DELETE C_IT_FIELDCAT
WHERE FIELDNAME = 'WAERS'.
INSERT L_WA_FIELDCAT_TMP INTO C_IT_FIELDCAT INDEX L_VAR_TABIX.
ENDIF.
"MEINS
READ TABLE C_IT_FIELDCAT
INTO L_WA_FIELDCAT_TMP
WITH KEY FIELDNAME = 'MEINS'.
IF SY-SUBRC = 0.
"Löschen und neu einfügen
DELETE C_IT_FIELDCAT
WHERE FIELDNAME = 'MEINS'.
INSERT L_WA_FIELDCAT_TMP INTO C_IT_FIELDCAT INDEX L_VAR_TABIX.
ENDIF.
"Positionen neu schreiben
LOOP AT C_IT_FIELDCAT ASSIGNING <FS_WA_FIELDCAT_POS>
WHERE TABNAME NE ''.
<FS_WA_FIELDCAT_POS>-COL_POS = L_VAR_COL_POS_CNT.
L_VAR_COL_POS_CNT = L_VAR_COL_POS_CNT + 1.
ENDLOOP.
ENDFORM. " REORDER_FIELDCAT_MBEWH
Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Team Lead
Contact : serkurumsal@yandex.com
Social Plugin