有时候需要在工具条上加个按钮:
*************知道行号了,可以从内表中读取数据,然后做相应操作***********
WHEN OTHERS.
ENDCASE.
ENDMETHOD. "user_command
ENDCLASS. "lcl_envent_receiver IMPLEMENTATION
有一种方法是通过ALV控件相关事件接口实现
1)ALV控件的工具条处理事件,定义新按钮和功能码
2)ALV控件的功能码处理事件,定义用户单击按钮产生的功能码
首先
INCLUDE
.
然后定义事件类和对象
"事件定义
CLASS lcl_envent_receiver DEFINITION DEFERRED.
DATA event_receiver TYPE REF TO lcl_envent_receiver.
CLASS lcl_envent_receiver DEFINITION DEFERRED.
DATA event_receiver TYPE REF TO lcl_envent_receiver.
*----------------------------------------------------------------------*
* CLASS lcl_envent_receiver DEFINITION
*----------------------------------------------------------------------*
* 定义事件类
*----------------------------------------------------------------------*
CLASS lcl_envent_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handle_toolbar "工具条句柄
FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive,
handle_user_command "用户命令句柄
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
ENDCLASS. "lcl_envent_receiver DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_envent_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_envent_receiver IMPLEMENTATION.
METHOD handle_toolbar.
DATA:
ls_toolbar TYPE stb_button.
CLEAR ls_toolbar-butn_type.
APPEND ls_toolbar TO e_object->mt_toolbar. "添加一个按钮到工具条
CLEAR ls_toolbar.
MOVE 'SHOW_DETA' TO ls_toolbar-function. "设置此按钮的功能码
MOVE icon_ppe_vnode TO ls_toolbar-icon. "图标
MOVE '明细' TO ls_toolbar-quickinfo. "显示信息,文字
MOVE '明细' TO ls_toolbar-text.
MOVE '' TO ls_toolbar-disabled.
APPEND ls_toolbar TO e_object->mt_toolbar. "将明细按钮添加到工具条
ENDMETHOD. "handle_toolbar
METHOD handle_user_command.
DATA:
lt_rows TYPE lvc_t_row.
CASE e_ucomm.
WHEN 'SHOW_DETA'. "当单击 明细按钮时的事件
CALL METHOD alv_grid->get_selected_rows
IMPORTING
et_index_rows = lt_rows. "返回的是选择的行号1234等
* CLASS lcl_envent_receiver DEFINITION
*----------------------------------------------------------------------*
* 定义事件类
*----------------------------------------------------------------------*
CLASS lcl_envent_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handle_toolbar "工具条句柄
FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive,
handle_user_command "用户命令句柄
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
ENDCLASS. "lcl_envent_receiver DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_envent_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_envent_receiver IMPLEMENTATION.
METHOD handle_toolbar.
DATA:
ls_toolbar TYPE stb_button.
CLEAR ls_toolbar-butn_type.
APPEND ls_toolbar TO e_object->mt_toolbar. "添加一个按钮到工具条
CLEAR ls_toolbar.
MOVE 'SHOW_DETA' TO ls_toolbar-function. "设置此按钮的功能码
MOVE icon_ppe_vnode TO ls_toolbar-icon. "图标
MOVE '明细' TO ls_toolbar-quickinfo. "显示信息,文字
MOVE '明细' TO ls_toolbar-text.
MOVE '' TO ls_toolbar-disabled.
APPEND ls_toolbar TO e_object->mt_toolbar. "将明细按钮添加到工具条
ENDMETHOD. "handle_toolbar
METHOD handle_user_command.
DATA:
lt_rows TYPE lvc_t_row.
CASE e_ucomm.
WHEN 'SHOW_DETA'. "当单击 明细按钮时的事件
CALL METHOD alv_grid->get_selected_rows
IMPORTING
et_index_rows = lt_rows. "返回的是选择的行号1234等
*************知道行号了,可以从内表中读取数据,然后做相应操作***********
WHEN OTHERS.
ENDCASE.
ENDMETHOD. "user_command
ENDCLASS. "lcl_envent_receiver IMPLEMENTATION
上面是定义部分
具体哪里控制?
在显示之后:
CALL
METHOD alv_grid
->set_table_for_first_display
EXPORTING
i_structure_name = 'SPFLI'
CHANGING
it_outtab = g_it_spfli
.
IF sy -subrc <> 0 .
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF .
EXPORTING
i_structure_name = 'SPFLI'
CHANGING
it_outtab = g_it_spfli
.
IF sy -subrc <> 0 .
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF .
"创建命令接受对象
CREATE OBJECT event_receiver.
"设置句柄
SET HANDLER event_receiver->handle_user_command "响应用户操作
FOR alv_grid.
SET HANDLER event_receiver->handle_toolbar "工具条
FOR alv_grid.
CALL METHOD alv_grid->set_toolbar_interactive. "调用接口设置工具条
SET HANDLER event_receiver->handle_user_command "响应用户操作
FOR alv_grid.
SET HANDLER event_receiver->handle_toolbar "工具条
FOR alv_grid.
CALL METHOD alv_grid->set_toolbar_interactive. "调用接口设置工具条