2008년 12월 23일 화요일

[Sample code]CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE


REPORT ZTEST_DYNAMIC_ALV.

TYPE-POOLS: SLIS.

FIELD-SYMBOLS: TYPE STANDARD TABLE,
.

DATA: ALV_FLDCAT TYPE SLIS_T_FIELDCAT_ALV,
IT_FLDCAT TYPE LVC_T_FCAT.

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
PARAMETERS: P_FLDS(5) TYPE C.
SELECTION-SCREEN END OF BLOCK B1.

START-OF-SELECTION.

* build the dynamic internal table
PERFORM BUILD_DYN_ITAB.

* write 5 records to the alv grid
DO 5 TIMES.
PERFORM BUILD_REPORT.
ENDDO.

* call the alv grid.
PERFORM CALL_ALV.


************************************************************************
* Build_dyn_itab
************************************************************************
FORM BUILD_DYN_ITAB.

DATA: NEW_TABLE TYPE REF TO DATA,
NEW_LINE TYPE REF TO DATA,
WA_IT_FLDCAT TYPE LVC_S_FCAT.

* Create fields .
DO P_FLDS TIMES.
CLEAR WA_IT_FLDCAT.
WA_IT_FLDCAT-FIELDNAME = SY-INDEX.
WA_IT_FLDCAT-DATATYPE = 'CHAR'.
WA_IT_FLDCAT-INTLEN = 5.
APPEND WA_IT_FLDCAT TO IT_FLDCAT .
ENDDO.

* Create dynamic internal table and assign to FS
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = IT_FLDCAT
IMPORTING
EP_TABLE = NEW_TABLE.

ASSIGN NEW_TABLE->* TO .

* Create dynamic work area and assign to FS
CREATE DATA NEW_LINE LIKE LINE OF .
ASSIGN NEW_LINE->* TO .

ENDFORM. "build_dyn_itab

*********************************************************************
* Form build_report
*********************************************************************
FORM BUILD_REPORT.

DATA: FIELDNAME(20) TYPE C.
DATA: FIELDVALUE(5) TYPE C.
DATA: INDEX(3) TYPE C.
FIELD-SYMBOLS: .

DO P_FLDS TIMES.

INDEX = SY-INDEX.

* Set up fieldvalue
CONCATENATE 'FLD' INDEX INTO
FIELDVALUE.
CONDENSE FIELDVALUE NO-GAPS.

ASSIGN COMPONENT INDEX OF STRUCTURE TO .
= FIELDVALUE.

ENDDO.

* Append to the dynamic internal table
APPEND TO .


ENDFORM. "build_report

************************************************************************
* CALL_ALV
************************************************************************
FORM CALL_ALV.

DATA: WA_CAT LIKE LINE OF ALV_FLDCAT.

DO P_FLDS TIMES.
CLEAR WA_CAT.
WA_CAT-FIELDNAME = SY-INDEX.
WA_CAT-SELTEXT_S = SY-INDEX.
WA_CAT-OUTPUTLEN = '5'.
APPEND WA_CAT TO ALV_FLDCAT.
ENDDO.


* Call ABAP List Viewer (ALV)
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
IT_FIELDCAT = ALV_FLDCAT
TABLES
T_OUTTAB = .

ENDFORM. "call_alv

댓글 1개:

익명 :

Hello! Thank you for this sample, it was very useful to me :)