二维码

[ooalv] 通过事件控制实现ALV复选框只能单选

Twilight发表于 2015-09-18 11:22txfirst 最后回复于 2018-12-25 22:11 [复制链接] 8887 1

1、程序代码:
  1. TYPE-POOLS slis.

  2. DATA: BEGIN OF gt_list OCCURS 0,
  3.         sel   TYPE char4,
  4.         val   TYPE i,
  5.         style TYPE lvc_t_styl,
  6.       END OF gt_list.
  7. DATA: gs_styl TYPE lvc_s_styl,
  8.       gs_scol TYPE lvc_s_scol.

  9. DEFINE mcr_fill_styl.
  10.   GS_STYL-FIELDNAME = &1.
  11.   GS_STYL-STYLE     = &2.
  12.   INSERT GS_STYL INTO TABLE GT_LIST-STYLE.
  13.   CLEAR GS_STYL.
  14. END-OF-DEFINITION.

  15. DATA: go_ALV_grid TYPE REF TO cl_gui_alv_grid.

  16. *&---------------------------------------------------------------------*
  17. *&      START-OF-SELECTION
  18. *&---------------------------------------------------------------------*
  19. START-OF-SELECTION.
  20.   PERFORM frm_get_data.
  21.   PERFORM frm_dsp_data.
  22. *&---------------------------------------------------------------------*
  23. *&      Form  FRM_GET_DATA
  24. *&---------------------------------------------------------------------*
  25. FORM frm_get_data .
  26.   DATA: lv_index TYPE i.
  27.   DO 3 TIMES.
  28.     lv_index = sy-index.
  29.     DO sy-index * 2 TIMES.
  30.       IF sy-index NE 1.
  31.         mcr_fill_styl:  'SEL'   cl_gui_alv_grid=>mc_style_disabled.
  32.       ELSE.
  33.         mcr_fill_styl:  'SEL'   cl_gui_alv_grid=>mc_style_enabled.
  34.       ENDIF.
  35.       gt_list-val = lv_index.
  36.       APPEND gt_list.
  37.       CLEAR gt_list.
  38.     ENDDO.
  39.   ENDDO.
  40. ENDFORM.                    " FRM_GET_DATA
  41. *&---------------------------------------------------------------------*
  42. *&      Form  FRM_DSP_DATA
  43. *&---------------------------------------------------------------------*
  44. FORM frm_dsp_data .
  45.   DATA: ls_layo  TYPE lvc_s_layo,
  46.         lt_fcat  TYPE lvc_t_fcat,
  47.         ls_fcat  TYPE lvc_s_fcat,
  48.         lt_event TYPE slis_t_event,
  49.         ls_event TYPE slis_alv_event.
  50.   ls_layo-zebra      = abap_true.
  51.   ls_layo-cwidth_opt = abap_true.
  52.   ls_layo-stylefname = 'STYLE'.
  53.   ls_fcat-fieldname = 'SEL'.
  54.   ls_fcat-checkbox = 'X'.
  55.   ls_fcat-edit = 'X'.
  56.   ls_fcat-scrtext_l =
  57.   ls_fcat-scrtext_m =
  58.   ls_fcat-scrtext_s = '选择'.
  59.   APPEND ls_fcat TO lt_fcat.
  60.   CLEAR ls_fcat.
  61.   ls_fcat-fieldname = 'VAL'.
  62.   ls_fcat-scrtext_l =
  63.   ls_fcat-scrtext_m =
  64.   ls_fcat-scrtext_s = '值'.
  65.   APPEND ls_fcat TO lt_fcat.
  66.   CLEAR ls_fcat.
  67.   ls_event-name = slis_ev_caller_exit_at_start.
  68.   ls_event-form = slis_ev_caller_exit_at_start.
  69.   APPEND ls_event TO lt_event.
  70.   ls_event-name = slis_ev_data_changed.
  71.   ls_event-form = slis_ev_data_changed.
  72.   APPEND ls_event TO lt_event.
  73. * 显示alv列表
  74.   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
  75.     EXPORTING
  76.       i_callback_program = sy-repid
  77.       is_layout_lvc      = ls_layo
  78.       it_fieldcat_lvc    = lt_fcat
  79.       i_save             = 'A'
  80.       it_events          = lt_event
  81.     TABLES
  82.       t_outtab           = gt_list
  83.     EXCEPTIONS
  84.       program_error      = 1
  85.       OTHERS             = 2.
  86.   IF sy-subrc <> 0.
  87.     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  88.           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  89.   ENDIF.
  90. ENDFORM.                    " FRM_DSP_DATA
  91. *&---------------------------------------------------------------------*
  92. *&      Form  CALLER_EXIT
  93. *&---------------------------------------------------------------------*
  94. FORM caller_exit USING u_exit TYPE slis_data_caller_exit.
  95.   DATA: ls_f4 TYPE lvc_s_f4,
  96.         lt_f4 TYPE lvc_t_f4.
  97.   CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
  98.     IMPORTING
  99.       e_grid = go_alv_grid.
  100. * 设置回车事件
  101.   CALL METHOD go_alv_grid->register_edit_event
  102.     EXPORTING
  103.       i_event_id = cl_gui_alv_grid=>mc_evt_enter
  104.     EXCEPTIONS
  105.       error      = 1
  106.       OTHERS     = 2.
  107. * 设置光标焦点移开被修改单元格后触发事件
  108.   CALL METHOD go_alv_grid->register_edit_event
  109.     EXPORTING
  110.       i_event_id = cl_gui_alv_grid=>mc_evt_modified
  111.     EXCEPTIONS
  112.       error      = 1
  113.       OTHERS     = 2.
  114. ENDFORM.                    "CALLER_EXIT
  115. *&---------------------------------------------------------------------*
  116. *&      Form  DATA_CHANGED
  117. *&---------------------------------------------------------------------*
  118. *       数据改变检查
  119. *----------------------------------------------------------------------*
  120. FORM data_changed USING or_data_changed TYPE REF TO cl_alv_changed_data_protocol.
  121.   DATA: ls_mod_cells  TYPE lvc_s_modi.
  122.   FIELD-SYMBOLS: <fs_line>  LIKE gt_list,
  123.                  <fs_value> TYPE any.
  124.   LOOP AT or_data_changed->mt_good_cells INTO ls_mod_cells.
  125.     CLEAR gt_list.
  126.     READ TABLE gt_list ASSIGNING <fs_line> INDEX ls_mod_cells-row_id.
  127.     IF sy-subrc EQ 0.
  128.       ASSIGN COMPONENT ls_mod_cells-fieldname OF STRUCTURE <fs_line> TO <fs_value>.
  129.       IF sy-subrc EQ 0.
  130.         CALL METHOD or_data_changed->get_cell_value
  131.           EXPORTING
  132.             i_row_id    = ls_mod_cells-row_id
  133.             i_fieldname = ls_mod_cells-fieldname
  134.           IMPORTING
  135.             e_value     = <fs_value>.
  136.       ENDIF.
  137.     ENDIF.
  138.     CASE ls_mod_cells-fieldname.
  139.       WHEN 'SEL'.
  140.         gt_list-sel = ''.
  141.         MODIFY gt_list TRANSPORTING sel WHERE sel IS NOT INITIAL.
  142.         LOOP AT gt_list
  143.           WHERE val EQ <fs_line>-val .
  144.           gt_list-sel = 'X'.
  145.           MODIFY gt_list TRANSPORTING sel.
  146.         ENDLOOP.
  147.       WHEN OTHERS.
  148.     ENDCASE.
  149.   ENDLOOP.
  150.   PERFORM frm_ref_alv.
  151. ENDFORM.                    " DATA_CHANGED
  152. *&---------------------------------------------------------------------*
  153. *&      Form  FRM_REF_ALV
  154. *&---------------------------------------------------------------------*
  155. FORM frm_ref_alv .
  156.   DATA ls_stbl TYPE lvc_s_stbl.
  157.   ls_stbl-row = abap_true.
  158.   ls_stbl-col = abap_true.
  159.   CALL METHOD go_alv_grid->refresh_table_display
  160.     EXPORTING
  161.       is_stable = ls_stbl.
  162. ENDFORM.                    " FRM_REF_ALV
复制代码


2、程序效果:
ALV复选框.jpg
回复

使用道具 举报

txfirst
ls_fcat-checkbox = 'X'. 这是关键
回复 支持 反对

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表