- 几乎所有的功能报表都会有那么一个选择列,问了业务顾问,业务顾问说是用户不习惯使用报表原生的选择模式。
- 效果图
- SALV的选择列是通过将列设置成checkbox_hotspot样式,注册单击事件完成勾选功能的。
- 完成步骤
- 将SEL列设置成checkbox_hotspot样式
METHOD set_columns. * 取得全部列的对象 DATA(lo_cols) = co_alv->get_columns( ). TRY. "修改列SEL的标签名字和输出长度 lo_cols->get_column( 'SEL' )->set_long_text( '选择' ). "长描述 lo_cols->get_column( 'SEL' )->set_medium_text( '选择' )."中描述 lo_cols->get_column( 'SEL' )->set_short_text( '选择' ). "短描述 lo_cols->get_column( 'SEL' )->set_output_length( 4 ). "修改列成为checkbox_hotspot样式 DATA lo_col TYPE REF TO cl_salv_column_list. lo_col ?= lo_cols->get_column( 'SEL' ). lo_col->set_cell_type( if_salv_c_cell_type=>checkbox_hotspot ). CATCH cx_salv_not_found. ENDTRY. ENDMETHOD.
- 获取SALV事件对象,并对单击事件作处理
METHOD set_columns. "获取事件对象 DATA: lo_events TYPE REF TO cl_salv_events_table. lo_events = co_alv->get_event( ). "注册单击事件 SET HANDLER me->on_link_click FOR lo_events. ENDMETHOD. "单击事件的处理 METHOD on_link_click. READ TABLE me->mt_data ASSIGNING FIELD-SYMBOL(<fs_data>) INDEX row. CHECK sy-subrc IS INITIAL. IF <fs_data>-sel IS INITIAL. <fs_data>-sel = 'X'. ELSE. CLEAR <fs_data>-sel. ENDIF. me->mo_salv->refresh( ). ENDMETHOD.
- 完整代码
CLASS lcl_model DEFINITION. PUBLIC SECTION. TYPES:BEGIN OF TY_dATA, sel TYPE char1, 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_columns 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 CORRESPONDING FIELDS OF 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_columns( 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_columns. * 取得全部列的对象 DATA(lo_cols) = co_alv->get_columns( ). * 设置自动优化列宽度 lo_cols->set_optimize( 'X' ). * 修改列VBELN的标签名字和输出长度 TRY. lo_cols->get_column( 'SEL' )->set_long_text( '选择' ). "长描述 lo_cols->get_column( 'SEL' )->set_medium_text( '选择' )."中描述 lo_cols->get_column( 'SEL' )->set_short_text( '选择' ). "短描述 lo_cols->get_column( 'SEL' )->set_output_length( 4 ). DATA lo_col TYPE REF TO cl_salv_column_list. lo_col ?= lo_cols->get_column( 'SEL' ). lo_col->set_cell_type( if_salv_c_cell_type=>checkbox_hotspot ).. CATCH cx_salv_not_found. ENDTRY. DATA: lo_events TYPE REF TO cl_salv_events_table. lo_events = co_alv->get_event( ). SET HANDLER me->on_link_click FOR lo_events. ENDMETHOD. "SET_COLUMNS METHOD on_link_click. READ TABLE me->mt_data ASSIGNING FIELD-SYMBOL(<fs_data>) INDEX row. CHECK sy-subrc IS INITIAL. IF <fs_data>-sel IS INITIAL. <fs_data>-sel = 'X'. ELSE. CLEAR <fs_data>-sel. ENDIF. me->mo_salv->refresh( ). ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA(lo_model) = NEW lcl_model( ). lo_model->get_data( ). lo_model->alv_show( ).