REPORT ztest.
TYPE-POOLS:abap,slis.
DATA t_fieldcat TYPE lvc_t_fcat.
DATA w_fieldcat TYPE lvc_s_fcat.
DATA w_layout TYPE lvc_s_layo.
DATA:gt_ddval TYPE lvc_t_drop,
gw_ddval TYPE lvc_s_drop.
DATA: gt_events TYPE slis_t_event,
gw_events TYPE slis_alv_event.
DATA: t_vbap TYPE STANDARD TABLE OF vbap,
w_vbap TYPE vbap.
DATA:t_makt TYPE STANDARD TABLE OF makt,
w_makt TYPE makt.
TYPES: BEGIN OF ty_maktx,
spras TYPE makt-spras,
END OF ty_maktx.
DATA: t_maktx TYPE STANDARD TABLE OF ty_maktx,
w_maktx TYPE ty_maktx.
DATA: d_ref TYPE REF TO data,
lt_alv_cat TYPE TABLE OF lvc_s_fcat,
ls_alv_cat LIKE LINE OF lt_alv_cat.
DATA: dy_table TYPE REF TO data,
dy_line TYPE REF TO data.
FIELD-SYMBOLS : <dyn_table> TYPE table ,
<dyn_wa> TYPE ANY,
<dyn_field> TYPE ANY.
START-OF-SELECTION.
SELECT * INTO CORRESPONDING FIELDS OF TABLE t_vbap" from vbap where vbeln = '0000005133'.
UP TO 100 ROWS FROM vbap.
SORT t_vbap BY matnr.
DELETE ADJACENT DUPLICATES FROM t_vbap COMPARING matnr.
SELECT * INTO CORRESPONDING FIELDS OF TABLE t_makt
FROM makt FOR ALL ENTRIES IN t_vbap
WHERE matnr = t_vbap-matnr.
DATA: lt_makt TYPE STANDARD TABLE OF makt WITH HEADER LINE.
SORT t_makt BY spras.
lt_makt[] = t_makt.
DELETE ADJACENT DUPLICATES FROM lt_makt COMPARING spras.
LOOP AT lt_makt.
w_maktx-spras = lt_makt-spras.
APPEND w_maktx TO t_maktx.
ENDLOOP.
PERFORM creat_event_exits.
PERFORM frm_show_alv.
***---------------------------------------------**
FORM caller_exit USING ls_data TYPE slis_data_caller_exit.
DATA: l_ref_alv TYPE REF TO cl_gui_alv_grid.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = l_ref_alv.
CALL METHOD l_ref_alv->set_drop_down_table
EXPORTING
it_drop_down = gt_ddval.
ENDFORM. "caller_exit
*&---------------------------------------------------------------------*
*& Form creat_event_exits
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM creat_event_exits .
gw_events-name = 'CALLER_EXIT'.
gw_events-form = 'CALLER_EXIT'.
APPEND gw_events TO gt_events.
ENDFORM. "CREAT_EVENT_EXITS
*&---------------------------------------------------------------------*
*& Form FRM_SHOW_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_show_alv .
PERFORM frm_fieldcat USING:
' ' ' ' ' ' ' ' 'L' 'VBELN' '凭证号' 'VBELN' 'VBAP',
' ' ' ' ' ' ' ' 'L' 'POSNR' '行项目' 'POSNR' 'VBAP',
' ' ' ' ' ' ' ' 'L' 'MATNR' '物料号' 'MATNR' 'VBAP' ,
' ' ' ' ' ' ' ' 'L' 'NETWR' '净价值' 'NETWR' 'VBAP',
' ' ' ' ' ' ' ' 'L' 'WAERK' '货币' 'WAERK' 'VBAP',
' ' ' ' ' ' ' ' 'L' 'KWMENG' '数量' 'KWMENG' 'VBAP'.
LOOP AT t_maktx INTO w_maktx.
IF w_maktx-spras = '1'.
w_fieldcat-fieldname = '1'.
w_fieldcat-coltext = '中文描述'.
w_fieldcat-ref_field = 'MAKTX'.
w_fieldcat-ref_table = 'MAKT'.
APPEND w_fieldcat TO t_fieldcat.
CLEAR w_fieldcat.
ENDIF.
IF w_maktx-spras = 'E'.
w_fieldcat-fieldname = 'E'.
w_fieldcat-coltext = '英文描述'.
w_fieldcat-ref_field = 'MAKTX'.
w_fieldcat-ref_table = 'MAKT'.
APPEND w_fieldcat TO t_fieldcat.
CLEAR w_fieldcat.
ENDIF.
IF w_maktx-spras = 'Z'.
w_fieldcat-fieldname = 'Z'.
w_fieldcat-coltext = '附加描述'.
w_fieldcat-ref_field = 'MAKTX'.
w_fieldcat-ref_table = 'MAKT'.
APPEND w_fieldcat TO t_fieldcat.
CLEAR w_fieldcat.
ENDIF.
ENDLOOP.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = t_fieldcat
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table>.
CREATE DATA dy_line LIKE LINE OF <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
* 设置显示格式
PERFORM frm_layout.
* 填充数据
PERFORM frm_fill_data.
* ALV显示
PERFORM frm_alv_show.
ENDFORM. " FRM_SHOW_ALV
*&---------------------------------------------------------------------*
*& Form FRM_LAYOUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_layout .
w_layout-zebra = 'X'.
w_layout-cwidth_opt = 'X'.
ENDFORM. " FRM_LAYOUT
*&---------------------------------------------------------------------*
*& Form FRM_ALV_SHOW
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_alv_show .
DATA:l_repid TYPE sy-repid.
l_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = w_layout
it_fieldcat_lvc = t_fieldcat
it_events = gt_events
TABLES
t_outtab = <dyn_table>.
ENDFORM. " FRM_ALV_SHOW
*&---------------------------------------------------------------------*
*& Form frm_fieldcat
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->FU_KEY text
* -->FU_CHECKBOX text
* -->FU_EDIT text
* -->FU_HOTSPOT text
* -->FU_JUST text
* -->FU_FIELDNAME text
* -->FU_SELTEXT text
*----------------------------------------------------------------------*
FORM frm_fieldcat USING fu_key
fu_checkbox
fu_edit
fu_hotspot
fu_just
fu_fieldname
fu_seltext
fu_field
fu_table.
CLEAR w_fieldcat.
w_fieldcat-key = fu_key.
w_fieldcat-checkbox = fu_checkbox.
w_fieldcat-edit = fu_edit.
w_fieldcat-hotspot = fu_hotspot.
w_fieldcat-just = fu_just.
w_fieldcat-fieldname = fu_fieldname.
w_fieldcat-coltext = fu_seltext.
w_fieldcat-ref_field = fu_field. " 参照系统字段,不然生成的内表字段类型为默认 C10
w_fieldcat-ref_table = fu_table.
APPEND w_fieldcat TO t_fieldcat.
ENDFORM. " FRM_FIELDCAT
*&---------------------------------------------------------------------*
*& Form FRM_FILL_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_fill_data .
SORT t_makt BY matnr.
LOOP AT t_vbap INTO w_vbap.
ASSIGN COMPONENT 'VBELN' OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = w_vbap-vbeln .
ASSIGN COMPONENT 'POSNR' OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = w_vbap-posnr .
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = w_vbap-matnr .
ASSIGN COMPONENT 'NETWR' OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = w_vbap-netwr .
ASSIGN COMPONENT 'WAERK' OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = w_vbap-waerk .
ASSIGN COMPONENT 'KWMENG' OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = w_vbap-kwmeng .
LOOP AT t_maktx INTO w_maktx.
READ TABLE t_makt INTO w_makt WITH KEY spras = w_maktx-spras matnr = w_vbap-matnr.
IF sy-subrc = 0.
ASSIGN COMPONENT w_maktx-spras OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = w_makt-maktx .
ENDIF.
ENDLOOP.
APPEND <dyn_wa> TO <dyn_table>.
ENDLOOP.
ENDFORM. " FRM_FILL_DATA