- 实现思路:将列设置成热点,热点列是可点击的,再给SALV实例对象注册点击事件即可,一般作用于点击单号跳转到前台等功能
-
"设置热点方法 METHODS: set_hotspot CHANGING co_alv TYPE REF TO cl_salv_table. ... "事件处理方法 METHODS: on_link_click FOR EVENT link_click OF cl_salv_events_table IMPORTING row column. METHOD set_hotspot. DATA lo_col TYPE REF TO cl_salv_column_table. "获取所有列对象的引用 DATA(lo_cols) = co_alv->get_columns( ). TRY. "获取群体列对象中VBELN列转换成CL_SALV_COLUMN_TABLE类型 lo_col ?= lo_cols->get_column( 'VBELN' ). "调用方法将列设置成热点 lo_col->set_cell_type( EXPORTING value = if_salv_c_cell_type=>hotspot ). CATCH cx_salv_data_error . ENDTRY. "获取SALV事件对象的引用 DATA(lo_events) = co_alv->get_event( ). " 设定EVENT HANDLER SET HANDLER me->on_link_click FOR lo_events. ENDMETHOD. METHOD on_link_click. READ TABLE me->mt_data INTO DATA(lw_data) INDEX row. IF sy-subrc = 0. MESSAGE '您点击了第' && row &&'行的' && lw_data-vbeln TYPE 'I'. ENDIF. ENDMETHOD.
- 实现效果
- 完整代码
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_layout CHANGING co_alv TYPE REF TO cl_salv_table. "设置热点方法 METHODS: set_hotspot CHANGING co_alv TYPE REF TO cl_salv_table. "事件处理方法 METHODS: on_link_click FOR EVENT link_click OF cl_salv_events_table IMPORTING row column. 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. "要生成SALV实例才能执行一下方法 me->set_status( CHANGING co_alv = mo_salv ). me->set_layout( CHANGING co_alv = mo_salv ). me->set_hotspot( CHANGING co_alv = mo_salv ). mo_salv->display( ). ENDMETHOD. METHOD set_status. DATA(lo_functions) = co_alv->get_functions( ). "cl_salv_functions_list类型的实例对象的添加状态方法 lo_functions->set_all( abap_true ). "lo_functions->set_default( abap_true ). ENDMETHOD. METHOD set_layout. DATA: lo_display TYPE REF TO cl_salv_display_settings. * 取得显示对象 lo_display = co_alv->get_display_settings( ). * 设置ZEBRA显示 lo_display->set_striped_pattern( 'X' ). * 设置Title lo_display->set_list_header( '销售订单报表' ). ENDMETHOD. METHOD set_hotspot. DATA lo_col TYPE REF TO cl_salv_column_table. "获取所有列对象的引用 DATA(lo_cols) = co_alv->get_columns( ). TRY. "获取群体列对象中VBELN列转换成CL_SALV_COLUMN_TABLE类型 lo_col ?= lo_cols->get_column( 'VBELN' ). "调用方法将列设置成热点 lo_col->set_cell_type( EXPORTING value = if_salv_c_cell_type=>hotspot ). CATCH cx_salv_data_error . ENDTRY. "获取SALV事件对象的引用 DATA(lo_events) = co_alv->get_event( ). " 设定EVENT HANDLER SET HANDLER me->on_link_click FOR lo_events. ENDMETHOD. METHOD on_link_click. READ TABLE me->mt_data INTO DATA(lw_data) INDEX row. IF sy-subrc = 0. MESSAGE '您点击了第' && row &&'行的' && lw_data-vbeln TYPE 'I'. ENDIF. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA(lo_model) = NEW lcl_model( ). lo_model->get_data( ). lo_model->alv_show( ).