下面介绍调用REUSE_ALV_GRID_DISPLAY_LVC创建ALV时,如何显示单选按钮(radio button),以及对应的单选按钮选择相应。
简单逻辑:
1,radio button的ICON: '@TB@' (选中)和 '@SR@' (未选中)
2,注册 事件lcl_event_receiver=>hotspot_click对应的method,用来处理单击radio button时的图标变更。
完整例子代码:
REPORT ztest_alv_radiobutton. TYPE-POOLS:abap, slis. DATA: BEGIN OF gt_list OCCURS 0, sel TYPE char4, val TYPE i, END OF gt_list. DATA: go_alv_grid TYPE REF TO cl_gui_alv_grid. *&---------------------------------------------------------------------* *& CLASS LCL_EVENT_RECEIVER DEFINITION *&---------------------------------------------------------------------* CLASS lcl_event_receiver DEFINITION. PUBLIC SECTION. CLASS-METHODS: * hotspot事件 hotspot_click FOR EVENT hotspot_click OF cl_gui_alv_grid IMPORTING e_row_id e_column_id es_row_no. ENDCLASS. "LCL_EVENT_RECEIVER DEFINITION *&---------------------------------------------------------------------* *& CLASS LCL_EVENT_RECEIVER DEFINITION *&---------------------------------------------------------------------* CLASS lcl_event_receiver IMPLEMENTATION. METHOD hotspot_click. DATA: ls_list LIKE gt_list. CLEAR ls_list. READ TABLE gt_list INTO ls_list INDEX e_row_id. IF gt_list-sel NE '@TB@'. ls_list-sel = '@SR@'."radio button选中的图标 MODIFY gt_list FROM ls_list TRANSPORTING sel WHERE sel NE '@SR@'. ls_list-sel = '@TB@'."没有选中radio button的图标 MODIFY gt_list FROM ls_list INDEX e_row_id TRANSPORTING sel . ENDIF. PERFORM frm_ref_alv. ENDMETHOD. "HANDLE_ONF4 ENDCLASS. "LCL_EVENT_RECEIVER IMPLEMENTATION *&---------------------------------------------------------------------* *& START-OF-SELECTION *&---------------------------------------------------------------------* START-OF-SELECTION. PERFORM frm_get_data. PERFORM frm_dsp_data. *&---------------------------------------------------------------------* *& Form FRM_GET_DATA *&---------------------------------------------------------------------* FORM frm_get_data . DO 10 TIMES. IF sy-index = 1. gt_list-sel = '@TB@'. ELSE. gt_list-sel = '@SR@'. ENDIF. gt_list-val = sy-index. APPEND gt_list. ENDDO. ENDFORM. " FRM_GET_DATA *&---------------------------------------------------------------------* *& Form FRM_DSP_DATA *&---------------------------------------------------------------------* FORM frm_dsp_data . DATA: ls_layo TYPE lvc_s_layo, lt_fcat TYPE lvc_t_fcat, ls_fcat TYPE lvc_s_fcat, lt_event TYPE slis_t_event, ls_event TYPE slis_alv_event. ls_layo-zebra = abap_true. ls_layo-cwidth_opt = abap_true. ls_fcat-fieldname = 'SEL'. ls_fcat-hotspot = 'X'. ls_fcat-scrtext_l = ls_fcat-scrtext_m = ls_fcat-scrtext_s = '选择'. APPEND ls_fcat TO lt_fcat. CLEAR ls_fcat. ls_fcat-fieldname = 'VAL'. ls_fcat-scrtext_l = ls_fcat-scrtext_m = ls_fcat-scrtext_s = '值'. APPEND ls_fcat TO lt_fcat. CLEAR ls_fcat. ls_event-name = slis_ev_caller_exit_at_start. ls_event-form = slis_ev_caller_exit_at_start. APPEND ls_event TO lt_event. * 显示alv列表 CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING i_callback_program = sy-repid is_layout_lvc = ls_layo it_fieldcat_lvc = lt_fcat i_save = 'A' it_events = lt_event TABLES t_outtab = gt_list EXCEPTIONS program_error = 1 OTHERS = 2. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. ENDFORM. " FRM_DSP_DATA *&---------------------------------------------------------------------* *& Form CALLER_EXIT *&---------------------------------------------------------------------* FORM caller_exit USING u_exit TYPE slis_data_caller_exit. DATA: ls_f4 TYPE lvc_s_f4, lt_f4 TYPE lvc_t_f4. CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR' IMPORTING e_grid = go_alv_grid. * 设置回车事件 CALL METHOD go_alv_grid->register_edit_event EXPORTING i_event_id = cl_gui_alv_grid=>mc_evt_enter EXCEPTIONS error = 1 OTHERS = 2. * 设置光标焦点移开被修改单元格后触发事件 CALL METHOD go_alv_grid->register_edit_event EXPORTING i_event_id = cl_gui_alv_grid=>mc_evt_modified EXCEPTIONS error = 1 OTHERS = 2. SET HANDLER lcl_event_receiver=>hotspot_click FOR go_alv_grid. ENDFORM. "CALLER_EXIT *&---------------------------------------------------------------------* *& Form FRM_REF_ALV *&---------------------------------------------------------------------* FORM frm_ref_alv . DATA ls_stbl TYPE lvc_s_stbl. ls_stbl-row = abap_true. ls_stbl-col = abap_true. CALL METHOD go_alv_grid->refresh_table_display EXPORTING is_stable = ls_stbl. ENDFORM. " FRM_REF_ALV
运行结果:
&lt;img class="alignnone size-full wp-image-5160" src="http://www.baidusap.com/wp-content/uploads/2017-07-24_9-40-46.png" alt="" width="563" height="313" /&gt;以上。