- SALV增加了按钮,那么该怎么实现点击了按钮实现交互功能呢?可以通过注册事件并且在对应的method中写入相关逻辑,来实现点击按钮后的逻辑。
- 通过自定义状态栏的方式添加按钮:http://t.csdnimg.cn/lMF16
- 通过使用派生类的方式添加按钮:http://t.csdnimg.cn/wz4Ro
- 实现步骤
- 定义一个用户命令事件处理类:这是用于专门用来处理用户命令的
CLASS lcl_event_handler DEFINITION. PUBLIC SECTION. METHODS: on_user_command FOR EVENT added_function OF cl_salv_events IMPORTING e_salv_function. ENDCLASS. CLASS lcl_event_handler IMPLEMENTATION. METHOD on_user_command. IF e_salv_function = 'PRI'. "用户点击按钮的命令码 MESSAGE '您点击了自定义按钮' TYPE 'I'. ENDIF. ENDMETHOD.
- 在生成SALV实例对象后获取派生类的事件对象,并将事件注册给用户命令事件处理对象
METHOD alv_show. DATA: lx_msg TYPE REF TO cx_salv_msg. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = mo_salv CHANGING t_table = mt_data ). CATCH cx_salv_msg INTO lx_msg. ENDTRY. "调用设置事件方法 me->set_events( CHANGING co_alv = mo_salv ). ENDMETHOD.
METHOD set_events. " 获取事件对象 DATA(lo_events) = co_alv->get_event( ). " 创建用户命令事件响应处理对象 DATA(lo_event_handler) = new lcl_event_handler( ). "用户命令事件 注册给 创建用户命令事件响应处理对象 SET HANDLER lo_event_handler->on_user_command FOR lo_events. ENDMETHOD.
- 实现效果
- 完整代码
CLASS lcl_event_handler DEFINITION. PUBLIC SECTION. METHODS: on_user_command FOR EVENT added_function OF cl_salv_events IMPORTING e_salv_function. ENDCLASS. CLASS lcl_event_handler IMPLEMENTATION. METHOD on_user_command. IF e_salv_function = 'PRI'. MESSAGE '您点击了自定义按钮' TYPE 'I'. ENDIF. ENDMETHOD. "on_user_command ENDCLASS. CLASS lcl_model DEFINITION. PUBLIC SECTION. TYPES:BEGIN OF TY_dATA, vbeln TYPE vbak-vbeln, erdat TYPE erdat, erzet TYPE erzet, ernam TYPE ernam, auart TYPE auart. TYPES:END OF ty_data. DATA mt_data TYPE TABLE OF ty_data. DATA mo_salv TYPE REF TO cl_salv_table. "获取数据的方法 METHODS get_data. "展示SALV的方法 METHODS alv_show. PRIVATE SECTION. METHODS: set_status CHANGING co_alv TYPE REF TO cl_salv_table. METHODS: set_events CHANGING co_alv TYPE REF TO cl_salv_table. ENDCLASS. CLASS lcl_model IMPLEMENTATION. METHOD get_data. SELECT vbeln erdat erzet ernam auart INTO TABLE mt_data FROM vbak UP TO 20 ROWS. ENDMETHOD. METHOD alv_show. DATA: lx_msg TYPE REF TO cx_salv_msg. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = mo_salv CHANGING t_table = mt_data ). CATCH cx_salv_msg INTO lx_msg. ENDTRY. me->set_status( CHANGING co_alv = mo_salv ). me->set_events( CHANGING co_alv = mo_salv ). mo_salv->display( ). ENDMETHOD. METHOD set_status. " 调用自定义pf status co_alv->set_screen_status( pfstatus = 'SALV_STANDARD'"状态栏名 report = sy-repid set_functions = co_alv->c_functions_all ). ENDMETHOD. METHOD set_events. " 获取事件对象 DATA(lo_events) = co_alv->get_event( ). " 创建用户命令事件响应处理对象 DATA(lo_event_handler) = new lcl_event_handler( ). "用户命令事件 注册给 创建用户命令事件响应处理对象 SET HANDLER lo_event_handler->on_user_command FOR lo_events. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA(lo_model) = NEW lcl_model( ). lo_model->get_data( ). lo_model->alv_show( ).