货铺QQ群号:834508274
简单案例,上面ALV是Header,下面是Item。
一个屏幕,俩控件
效果:
上面是BKPF数据,下面是BSEG数据.
完整代码:
*&---------------------------------------------------------------------**& Report ZLM_ALV_DEMO02*&---------------------------------------------------------------------**&*&---------------------------------------------------------------------*REPORT zlm_alv_demo02.
*&----------------------------------------------------------------*& ALV 定义*&----------------------------------------------------------------
DATA:gr_header TYPE REF TO cl_gui_custom_container,gr_item TYPE REF TO cl_gui_custom_container.
DATA: gr_alv_header TYPE REF TO cl_salv_table,gr_alv_item TYPE REF TO cl_salv_table.
CLASS lcl_handle_events DEFINITION DEFERRED.
DATA: gr_events TYPE REF TO lcl_handle_events.
*&---------------------------------------------------------------------**& Class lcl_handle_events DEFINITION*&---------------------------------------------------------------------** Text*----------------------------------------------------------------------*CLASS lcl_handle_events DEFINITION.
PUBLIC SECTION.METHODS:on_user_command FOR EVENT added_function OF cl_salv_eventsIMPORTING e_salv_function,
* hotspoton_link_click FOR EVENT link_click OF cl_salv_events_tableIMPORTING row column,
* double_clickon_double_click FOR EVENT double_click OF cl_salv_events_tableIMPORTING row column.ENDCLASS. "lcl_event_receiver DEFINITION
*---------------------------------------------------------------------** CLASS lcl_handle_events IMPLEMENTATION*---------------------------------------------------------------------** §5.2 implement the events for handling the events of cl_salv_table*---------------------------------------------------------------------*CLASS lcl_handle_events IMPLEMENTATION.METHOD on_user_command.ENDMETHOD. "on_user_command
METHOD on_link_click.* PERFORM refresh_data USING row column .ENDMETHOD.METHOD on_double_click.PERFORM frm_double_click USING row column .ENDMETHOD.ENDCLASS. "lcl_event_receiver
*&----------------------------------------------------------------*& 数据定义*&----------------------------------------------------------------DATA: gt_header TYPE TABLE OF bkpf.DATA: gt_item TYPE TABLE OF bseg.
DATA: g_okcode TYPE sy-ucomm,g_toggle TYPE i.CONSTANTS: con_ok TYPE sy-ucomm VALUE 'OK',con_exit TYPE sy-ucomm VALUE 'EXIT',con_canc TYPE sy-ucomm VALUE 'CANC',con_back TYPE sy-ucomm VALUE 'BACK'.*&----------------------------------------------------------------*& 屏幕定义*&----------------------------------------------------------------TABLES bkpf.SELECT-OPTIONS:s_bukrs FOR bkpf-bukrs DEFAULT '1001',s_belnr FOR bkpf-belnr,s_gjahr FOR bkpf-gjahr NO INTERVALS NO-EXTENSION .
START-OF-SELECTION.SELECT * FROM bkpf INTO TABLE gt_headerWHEREbukrs IN s_bukrs ANDbelnr IN s_belnr ANDgjahr IN s_gjahr .
CALL SCREEN 9001.
*&---------------------------------------------------------------------**& Module MDL_ALV OUTPUT*&---------------------------------------------------------------------**&*&---------------------------------------------------------------------*MODULE mdl_alv OUTPUT.
IF gr_header IS INITIAL.
IF cl_gui_alv_grid=>offline( ) IS INITIAL.CREATE OBJECT gr_headerEXPORTINGcontainer_name = 'HEADER'.ENDIF.
TRY.cl_salv_table=>factory(EXPORTINGr_container = gr_headercontainer_name = 'HEADER'IMPORTINGr_salv_table = gr_alv_headerCHANGINGt_table = gt_header ).CATCH cx_salv_msg. "#EC NO_HANDLERENDTRY.
DATA: lr_events TYPE REF TO cl_salv_events_table.
lr_events = gr_alv_header->get_event( ).
CREATE OBJECT gr_events.
*... §6.1 register to the event USER_COMMANDSET HANDLER gr_events->on_user_command FOR lr_events.SET HANDLER gr_events->on_link_click FOR lr_events.SET HANDLER gr_events->on_double_click FOR lr_events.
*... §7 display the tablegr_alv_header->display( ).
ENDIF.
IF gr_item IS INITIAL.IF cl_gui_alv_grid=>offline( ) IS INITIAL.CREATE OBJECT gr_itemEXPORTINGcontainer_name = 'ITEM'.ENDIF.ENDIF.
TRY.cl_salv_table=>factory(EXPORTINGr_container = gr_itemcontainer_name = 'ITEM'IMPORTINGr_salv_table = gr_alv_itemCHANGINGt_table = gt_item ).CATCH cx_salv_msg. "#EC NO_HANDLERENDTRY.
gr_alv_item->display( ).
ENDMODULE.*&---------------------------------------------------------------------**& Module STATUS_9001 OUTPUT*&---------------------------------------------------------------------**&*&---------------------------------------------------------------------*MODULE status_9001 OUTPUT.* SET PF-STATUS 'xxxxxxxx'.* SET TITLEBAR 'xxx'.ENDMODULE.
FORM frm_double_click USING i_row TYPE ii_column TYPE lvc_fname.
DATA:ls_header LIKE LINE OF gt_header.
READ TABLE gt_header INTO ls_headerINDEX i_row.
CHECK sy-subrc = 0.
SELECT * FROM bsegINTO TABLE gt_itemWHEREbukrs = ls_header-bukrs ANDbelnr = ls_header-belnr ANDgjahr = ls_header-gjahr.
TRY.gr_alv_item->set_data( CHANGING t_table = gt_item ).CATCH cx_salv_no_new_data_allowed cx_salv_not_found.ENDTRY.
gr_alv_item->refresh( refresh_mode = if_salv_c_refresh=>full ). "full refresh neceENDFORM. " REFRESH_DATA*&---------------------------------------------------------------------**& Module USER_COMMAND_9001 INPUT*&---------------------------------------------------------------------** text*----------------------------------------------------------------------*MODULE user_command_9001 INPUT.
ENDMODULE.*&---------------------------------------------------------------------**& Module MDL_EXIT INPUT*&---------------------------------------------------------------------** text*----------------------------------------------------------------------*MODULE mdl_exit INPUT.
LEAVE PROGRAM.
ENDMODULE.