ALV – Dynamische Erzeugung von internen Tabellen

Die dynamische Erzeugung von internen Tabellen mit einer erst zur Laufzeit bekannten Anzahl von Spalten ist eine beliebte Kopfnuss für ABAP-Einsteiger (und auch manchen altgedienten Profi…). Man kann das alles selber programmieren, oder aber bestehende Funktionsbausteine nutzen. Diesen hier aus dem ALV-Baukasten finde ich besonders praktisch…


REPORT ZZALVCREATE .
*------------------------------------------------------------
* WHAT DOES THIS PROGRAM DO?
*
* Generically create an internal table, and show it's
* contents in an ALV grid. This sample selects some data
* from MARD, but you can enter any other transparent table.
*
* Sample program was tested under SAP R/3 4.6C, but should
* run on higher releases as well.
*
* Note: This works by generating subroutine pools. You
* will receive dumps if you do not care how often
* REUSE_ALV_TABLE_CREATE is called.
*
* This program is provided "as-is" and solely designed
* for testing purposes. No liability whatsoever is accepted.
* If you plan to use it for production use, it is in your
* responsibility to do proper testing according to the
* state of the art.
*
* Sample program provided by Matthias Köper
* https://www.be-team.org
*
*------------------------------------------------------------type-pools slis.

parameters: p_type type tabname default 'MARD'.

data: gt_fcat type SLIS_T_FIELDCAT_ALV
, gt_data type table of mard
, dref type ref to data
.
field-symbols: <fs>
, <tab> type table
.

start-of-selection.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME               = 'ZZALVCREATE'
*   I_INTERNAL_TABNAME           =
I_STRUCTURE_NAME             = p_type
*   I_CLIENT_NEVER_DISPLAY       = 'X'
*   I_INCLNAME                   =
*   I_BYPASSING_BUFFER           =
*   I_BUFFER_ACTIVE              =
CHANGING
CT_FIELDCAT                  = gt_fcat
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.

CALL FUNCTION 'REUSE_ALV_TABLE_CREATE'
EXPORTING
IT_FIELDCAT        = gt_fcat
I_CALLBACK_PROGRAM = 'ZZALVCREATE'
I_FORMNAME         = 'TEST'.

*---------------------------------------------------------------------*
*       FORM TEST                                                     *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
form TEST tables gt_data.

assign gt_data[] to <tab>.

select * from (p_type) into corresponding fields of table <tab>
up to 100 rows.

create data dref type (p_type).
assign dref->* to <fs> casting type (p_type).

loop at <tab> into <fs>.
exit.
endloop.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
*     I_INTERFACE_CHECK                 = ' '
*     I_BYPASSING_BUFFER                =
*     I_BUFFER_ACTIVE                   = ' '
*     I_CALLBACK_PROGRAM                = ' '
*     I_CALLBACK_PF_STATUS_SET          = ' '
*     I_CALLBACK_USER_COMMAND           = ' '
*     I_CALLBACK_TOP_OF_PAGE            = ' '
*     I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*     I_CALLBACK_HTML_END_OF_LIST       = ' '
*     I_STRUCTURE_NAME                  =
*     I_BACKGROUND_ID                   = ' '
*     I_GRID_TITLE                      =
*     I_GRID_SETTINGS                   =
*     IS_LAYOUT                         =
IT_FIELDCAT                       = gt_fcat[]
*     IT_EXCLUDING                      =
*     IT_SPECIAL_GROUPS                 =
*     IT_SORT                           =
*     IT_FILTER                         =
*     IS_SEL_HIDE                       =
*     I_DEFAULT                         = 'X'
*     I_SAVE                            = ' '
*     IS_VARIANT                        =
*     IT_EVENTS                         =
*     IT_EVENT_EXIT                     =
*     IS_PRINT                          =
*     IS_REPREP_ID                      =
*     I_SCREEN_START_COLUMN             = 0
*     I_SCREEN_START_LINE               = 0
*     I_SCREEN_END_COLUMN               = 0
*     I_SCREEN_END_LINE                 = 0
*     IT_ALV_GRAPHICS                   =
*     IT_ADD_FIELDCAT                   =
*     IT_HYPERLINK                      =
*     I_HTML_HEIGHT_TOP                 =
*     I_HTML_HEIGHT_END                 =
*     IT_EXCEPT_QINFO                   =
*   IMPORTING
*     E_EXIT_CAUSED_BY_CALLER           =
*     ES_EXIT_CAUSED_BY_USER            =
TABLES
T_OUTTAB                          = <tab>
*   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.


Beitrag veröffentlicht

in

von

Schlagwörter: