码迷,mamicode.com
首页 > 其他好文 > 详细

OOALV 简单使用

时间:2020-06-11 16:49:14      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:orm   field   字段   ase   cto   when   selected   event   clear   

report zjty_demo_08.

tables: zjty_eng.

type-pools: icon, slis.

class zcl_alv_grid definition deferred.
data:
  ok_code type sy-ucomm,

  begin of gs_english.
        include structure zjty_eng.
data: type_text    type string,
      sel,
      row_color(4),
      cell_style   type lvc_t_styl,
      cell_color   type lvc_t_scol,
      end of gs_english,
      gt_english        like table of gs_english,

      go_alv100         type ref to zcl_alv_grid,
      go_dock_container type ref to cl_gui_docking_container,

      gs_layo           type lvc_s_layo,
      gt_fcat           type lvc_t_fcat.

constants:
  begin of g_ucomm,
    back       type sy-ucomm value ‘BACK‘,
    exit       type sy-ucomm value ‘EXIT‘,

    select_all type sy-ucomm value ‘SALL‘,
  end of g_ucomm.

define append_fcat.
  append value lvc_s_fcat(
    fieldname = &1
    scrtext_s = &2
    checkbox  = &3
    hotspot   = &4
    edit      = &5
    just = ‘C‘
  ) to &6.
end-of-definition.


class zcl_alv_grid definition inheriting from cl_gui_alv_grid.
  public section.
    methods:
      constructor
        importing
          i_parent type ref to cl_gui_container,

      enable_event_handle
        importing
          toolbar      type abap_bool default abap_false
          menu_button  type abap_bool default abap_false
          user_command type abap_bool default abap_false
          data_changed type abap_bool default abap_false
          f4help       type abap_bool default abap_false
          hostpot      type abap_bool default abap_false
          double_click type abap_bool default abap_false,

      handle_toolbar for event toolbar of cl_gui_alv_grid
        importing
            e_object,

      handle_menu_button for event menu_button of cl_gui_alv_grid
        importing
            e_object,

      handle_user_command for event user_command of cl_gui_alv_grid
        importing
            e_ucomm,

      handle_data_changed for event data_changed of cl_gui_alv_grid
        importing
            er_data_changed
            e_ucomm,

      handle_hotspot_click for event hotspot_click of cl_gui_alv_grid
        importing
            e_row_id
            e_column_id
            es_row_no,

      handle_double_click for event double_click of cl_gui_alv_grid
        importing
            e_row
            e_column
            es_row_no.

endclass.

class zcl_alv_grid implementation.

  method constructor.
    super->constructor(
      i_parent = i_parent
    ).
  endmethod.

  method enable_event_handle.

    if toolbar = abap_true.
      set handler me->handle_toolbar for me. " Only me, not ALL INSTANCES.
    endif.

    if menu_button = abap_true.
      set handler me->handle_menu_button for me. " Only me, not ALL INSTANCES.
    endif.

    if user_command = abap_true.
      set handler me->handle_user_command for me. " Only me, not ALL INSTANCES.
    endif.

    if data_changed = abap_true.
      me->register_edit_event( i_event_id = cl_gui_alv_grid=>mc_evt_modified ).
      me->register_edit_event( i_event_id = cl_gui_alv_grid=>mc_evt_enter ).

      set handler me->handle_data_changed for me.
    endif.

*   ......

  endmethod.

  method handle_toolbar. "工具栏

    append value stb_button(
      butn_type = 3 "分隔符
    ) to e_object->mt_toolbar.

    append value stb_button(
      butn_type = 0 "按钮 - 返回
      function = g_ucomm-back
      text = ‘返回‘
    ) to e_object->mt_toolbar.

    append value stb_button(
      butn_type = 0 "按钮 - 全选
      function = g_ucomm-select_all
      text = ‘全选‘
    ) to e_object->mt_toolbar.

  endmethod.

  method handle_menu_button. "菜单按钮

  endmethod.


  method handle_user_command. "用户命令
    case me.
      when go_alv100.
        case e_ucomm.
          when g_ucomm-back.
            leave to screen 0.
          when g_ucomm-select_all.
            loop at gt_english assigning field-symbol(<fs_tab>).
              <fs_tab>-sel = ‘X‘.
            endloop.

            go_alv100->refresh_table_display(
              is_stable = value #( row = ‘X‘ col = ‘X‘ )
            ).

*            go_alv100->select_all_rows( ).
*
*            data rows_no type lvc_t_roid. " 用OOALV的选择的话其实是可以不用SEL字段的
*            go_alv100->get_selected_rows(
*              importing
*              et_row_no = rows_no
*            ).
*            break-point.

          when others.
        endcase.
      when others.
    endcase.
  endmethod.

  method handle_data_changed. "数据更改

  endmethod.

  method handle_hotspot_click. "热点单击

  endmethod.

  method handle_double_click. "双击

  endmethod.

endclass.

*INITIALIZATION.

start-of-selection.
  perform get_english_data.

end-of-selection.
  perform call_alv_screen tables gt_english using 0100.

form get_english_data.
  select type,
         content,
         translat
    into corresponding fields of table @gt_english[]
    from zjty_eng.

  loop at gt_english assigning field-symbol(<fs_tab>).
    case <fs_tab>-type.
      when 1.
        <fs_tab>-type_text = ‘单词‘.

        <fs_tab>-cell_color = value lvc_t_scol(
          ( fname = ‘CONTENT‘ color = value #( col = 4 int = 1 ) )
          ( fname = ‘TRANSLAT‘ color = value #( col = 3   int = 1 ) )
          ).

      when 2.
        <fs_tab>-type_text = ‘句子‘.

        <fs_tab>-row_color = ‘C210‘.

        if sy-tabix mod 3 = 0.
          <fs_tab>-cell_style = value lvc_t_styl(
          ( fieldname = ‘TRANSLAT‘ " style = cl_gui_alv_grid=>mc_style_f4
            style = cl_gui_alv_grid=>mc_style_enabled )
          ).
        endif.
      when others.
    endcase.
  endloop.

  sort gt_english by type content.
endform.


form call_alv_screen tables lt_outtab using lv_dynnr.

  if lt_outtab[] is initial.
    message ‘未发现数据!‘ type ‘S‘ display like ‘E‘.
    return.
  else.
    message |共 { lines( lt_outtab ) } 条数据.| type ‘S‘.
    call screen lv_dynnr.
  endif.

endform.


module init_screen_0100 output.

  set pf-status ‘STATUS_100‘ excluding ‘‘.
  clear ok_code.

  if go_dock_container is not bound.
    go_dock_container = new cl_gui_docking_container(
      repid = sy-repid
      dynnr = sy-dynnr
      extension = 1500
    ).

    go_alv100 = new zcl_alv_grid(
      i_parent = go_dock_container
    ).

    gs_layo = value lvc_s_layo(
                zebra = abap_true
                cwidth_opt = abap_true
                no_f4 = abap_false
*                edit = abap_true
                sel_mode = ‘D‘
                box_fname = ‘SEL‘
                stylefname = ‘CELL_STYLE‘
*                info_fname = ‘ROW_COLOR‘  " BUG 加入后 SEL 列将不会显示
                ctab_fname = ‘CELL_COLOR‘
              ).

    refresh gt_fcat.
    append_fcat ‘SEL‘ ‘选择‘ ‘X‘ ‘‘ ‘X‘ gt_fcat.
*    APPEND_FCAT ‘TYPE‘ ‘类型‘ ‘‘ ‘‘ ‘‘ GT_FCAT.
    append_fcat ‘TYPE_TEXT‘ ‘类型‘ ‘‘ ‘‘ ‘‘ gt_fcat.
    append_fcat ‘CONTENT‘ ‘内容‘ ‘‘ ‘X‘ ‘‘ gt_fcat.
    append_fcat ‘TRANSLAT‘ ‘翻译‘ ‘‘ ‘‘  ‘‘ gt_fcat.

    go_alv100->enable_event_handle(
      toolbar = abap_true
      user_command = abap_true
    ).

    go_alv100->set_table_for_first_display(
      exporting
        is_layout = gs_layo
        is_variant = value disvariant(  report = sy-repid handle  = 1  )
      changing
        it_fieldcatalog = gt_fcat
        it_outtab = gt_english
    ).


  else.
    go_alv100->refresh_table_display(
      is_stable = value lvc_s_stbl( row = abap_true col = abap_true )
    ).
  endif.

endmodule.


module modify_screen_0100 output.

  loop at screen.

  endloop.

endmodule.


module user_command_0100 input.

  case ok_code.
    when ‘‘.
    when others.
  endcase.

endmodule.


module exit_command_0100 input.

  case ok_code.
    when g_ucomm-back.
      leave to screen 0.
    when others.
  endcase.

endmodule.

技术图片

OOALV 简单使用

标签:orm   field   字段   ase   cto   when   selected   event   clear   

原文地址:https://www.cnblogs.com/imimjx/p/13093649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!