動態的實現任意表的ALV顯示02

mmccking發表於2009-06-15
ABAP 動態的實現任意表的ALV顯示(學習動態構建表格和結構)[@more@]

第一部分:程式框架
REPORT ZALV_20090304_EVENT.

INCLUDE ZALV_DATA_INTI.
INCLUDE ZALV_EVENT_F01.

START-OF-SELECTION.

PERFORM SUB_ALV_DATA_GET.
第二部分:INCLUDE ZALV_DATA_INTI.
TYPE-POOLS:ABAP.
PARAMETERS P_NAME TYPE OBJNAME.
DATA LT_TABLE TYPE TABLE OF DFIES.
DATA LS_TABLE TYPE DFIES.

CALL FUNCTION 'DDIF_NAMETAB_GET'
EXPORTING
TABNAME = P_NAME
TABLES
* X031L_TAB =
DFIES_TAB = LT_TABLE
EXCEPTIONS
NOT_FOUND = 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.

DATA LR_STRUC TYPE REF TO CL_ABAP_STRUCTDESCR.
DATA LR_TABLE TYPE REF TO CL_ABAP_TABLEDESCR.
DATA LR_TYPE TYPE REF TO CL_ABAP_TYPEDESCR.
DATA LR_DATA TYPE REF TO CL_ABAP_DATADESCR.
DATA L_STRING TYPE STRING.
DATA LT_COMP TYPE ABAP_COMPONENT_TAB.
DATA LS_COMP LIKE LINE OF LT_COMP.
DATA E_WA TYPE REF TO DATA.
DATA E_TABLE TYPE REF TO DATA.


FIELD-SYMBOLS TYPE STANDARD TABLE.
FIELD-SYMBOLS TYPE ANY.

LOOP AT LT_TABLE INTO LS_TABLE.
CONCATENATE LS_TABLE-TABNAME '-' LS_TABLE-FIELDNAME INTO L_STRING.
* L_STRING = LS_COMP-NAME.
LS_COMP-NAME = LS_TABLE-FIELDNAME.
CALL METHOD CL_ABAP_DATADESCR=>DESCRIBE_BY_NAME
EXPORTING
P_NAME = L_STRING
RECEIVING
P_DESCR_REF = LR_TYPE
EXCEPTIONS
TYPE_NOT_FOUND = 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.


LS_COMP-TYPE ?= LR_TYPE.
APPEND LS_COMP TO LT_COMP.
CLEAR LS_COMP.
ENDLOOP.

CALL METHOD CL_ABAP_STRUCTDESCR=>CREATE
EXPORTING
P_COMPONENTS = LT_COMP
RECEIVING
P_RESULT = LR_STRUC
.

CALL METHOD CL_ABAP_TABLEDESCR=>CREATE
EXPORTING
P_LINE_TYPE = LR_STRUC
RECEIVING
P_RESULT = LR_TABLE
.

CREATE DATA E_WA TYPE HANDLE LR_STRUC.
CREATE DATA E_TABLE TYPE HANDLE LR_TABLE.


ASSIGN E_WA->* TO .
ASSIGN E_TABLE->* TO .
第三部分:INCLUDE ZALV_EVENT_F01.
FORM SUB_ALV_DATA_GET .

SELECT * INTO CORRESPONDING FIELDS OF TABLE FROM (P_NAME).
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_STRUCTURE_NAME = P_NAME
TABLES
T_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. " SUB_ALV_DATA_GET

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ABAP動態建立表結構的方法


DATA: LR_EXCEPTION TYPE REF TO CX_ROOT.
DATA: LT_COMP TYPE ABAP_COMPONENT_TAB.
DATA: LS_COMP LIKE LINE OF LT_COMP.
DATA: L_FIELD TYPE STRING.
DATA: LR_STRUC TYPE REF TO CL_ABAP_STRUCTDESCR.
DATA: LR_TYPE TYPE REF TO CL_ABAP_TYPEDESCR.
DATA: LR_TABLE TYPE REF TO CL_ABAP_TABLEDESCR.
TRY.
LOOP AT IT_QUERYFIELDS.
CONCATENATE IT_QUERYFIELDS-TABLENAME '-' IT_QUERYFIELDS-FIELDNAME INTO L_FIELD.
IF I_NOTABLENAME EQ 'X'.
LS_COMP-NAME = IT_QUERYFIELDS-FIELDNAME.
ELSE.
CONCATENATE IT_QUERYFIELDS-TABLENAME '_' IT_QUERYFIELDS-FIELDNAME INTO LS_COMP-NAME.
ENDIF.
CALL METHOD CL_ABAP_DATADESCR=>DESCRIBE_BY_NAME
EXPORTING
P_NAME = L_FIELD
RECEIVING
P_DESCR_REF = LR_TYPE
EXCEPTIONS
TYPE_NOT_FOUND = 1.
IF SY-SUBRC <> 0.
E_RC = 1.
EXIT.
ENDIF.
LS_COMP-TYPE ?= LR_TYPE.
APPEND LS_COMP TO LT_COMP.
CLEAR LS_COMP.
ENDLOOP.

CHECK E_RC = 0.
CALL METHOD CL_ABAP_STRUCTDESCR=>CREATE
EXPORTING
P_COMPONENTS = LT_COMP
RECEIVING
P_RESULT = LR_STRUC.

CALL METHOD CL_ABAP_TABLEDESCR=>CREATE
EXPORTING
P_LINE_TYPE = LR_STRUC
RECEIVING
P_RESULT = LR_TABLE.

CREATE DATA E_WA TYPE HANDLE LR_STRUC.
CREATE DATA E_TABLE TYPE HANDLE LR_TABLE.

CATCH CX_ROOT INTO LR_EXCEPTION .
CALL METHOD LR_EXCEPTION->IF_MESSAGE~GET_TEXT
RECEIVING
RESULT = E_MESSAGE.
E_RC = 1.
ENDTRY.
程式碼分析:ABAP_COMPONENT_TAB,在程式中,首先要用TYPE-POOLS:ABAP.宣告一下。這個段程式碼是一個RFC的程式碼。宣告部分不在這裡。
注意查詢ABAP_COMPONTET_TAB的結構。其中 的 NAME和 TYPE需要注意賦值的變數的型別。
LS_COMP-TYPE ?= LR_TYPE. 子類繼承。將 LR_TYPE的內容 賦給LS_COMP-TYPE 注意他的型別TYPE RRF TO DATADESCR. LR_TYPE TYPE RRF TO TYPEDESCR.
[url=]CL_ABAP_TYPEDESCR[/url]
|
|--
[url=]CL_ABAP_DATADESCR[/url]
| |
| |--
[url=]CL_ABAP_ELEMDESCR[/url]
| |--
[url=]CL_ABAP_REFDESCR[/url]
| |--
[url=]CL_ABAP_COMPLEXDESCR[/url]
| |
| |--
[url=]CL_ABAP_STRUCTDESCR[/url]
| |--
[url=]CL_ABAP_TABLEDESCR[/url]
|
|--
[url=]CL_ABAP_OBJECTDESCR[/url]
|
|--
[url=]CL_ABAP_CLASSDESCR[/url]
|--
[url=]CL_ABAP_INTFDESCR[/url]
這個時候建立的是一個型別引用,用 CREATE DATA E_WA 接受這個型別。
E_WA的型別
和E_TABLE型別都是 TYPE REF TO DATA.
當要使用這個動態建立的表結構的時候,就需要FILED-SYBOMLS: <> TYPE.
ASSIGN E_WA->* TO <>.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8356764/viewspace-1023159/,如需轉載,請註明出處,否則將追究法律責任。

相關文章