OO ALV 事件詳細使用(增加按鈕、增加下拉框等)

report  Z_OOALV.

type-pools: icon.
class lcl_event_receiver definition deferred.

tables: sflight.

data:
      gi_index_rows type lvc_t_row,
      g_selected_row like lvc_s_row.
data: gt_fieldcat type lvc_t_fcat,
      wa_fieldcat type lvc_s_fcat,
      tmp_fieldcat type lvc_t_fcat,
      gs_layout type lvc_s_layo.

data:
     o_event_receiver type ref to lcl_event_receiver.
data:zcw type  lvc_t_cell,
     wa_cw type lvc_s_cell.


data:m1_flag type i value 1.           "手工输入的开关

data:zd type sflight.

data:zkey(1) type c.
data:zc type string.
data:ztext type string.
data:l_lines type i.
data:zrow_no type lvc_t_roid,
     zno1 type lvc_s_roid.

*****************************定义类
class lcl_event_receiver definition.
  public section.
    methods:
     handle_toolbar for event toolbar of cl_gui_alv_grid
       importing
         e_object e_interactive,

     handle_user_command for event user_command of cl_gui_alv_grid
       importing e_ucomm,

****************************************以下定义的方法必要的时候可激活
*Hotspot click control
     handle_hotspot_click
        for event hotspot_click of cl_gui_alv_grid
         importing e_row_id e_column_id es_row_no ,

*Double-click control
     handle_double_click
        for event double_click of cl_gui_alv_grid
         importing e_row e_column es_row_no,

*To be triggered before user commands
     handle_before_user_command
        for event before_user_command of cl_gui_alv_grid
         importing e_ucomm,


*Controlling data changes when ALV Grid is editable
     handle_data_changed
        for event data_changed of cl_gui_alv_grid
          importing er_data_changed ,

*To be triggered after data changing is finished
     handle_data_changed_finished
        for event data_changed_finished of cl_gui_alv_grid
          importing e_modified ,


*To control menu buttons
      handle_menu_button
        for event menu_button of cl_gui_alv_grid
          importing e_object e_ucomm .

endclass.                    "lcl_event_receiver DEFINITION

data: tab_toolbar type  ttb_button,
      no_toolbar type stb_button.

types: begin of st_sflight.
        include structure sflight.
types:  traffic_light type c.
types:  line_color(4) type c.
types: ptype_dd_hndl type int4 .
types: cellstyles type lvc_t_styl.
types: end of st_sflight.

data:g_wa_sflight type st_sflight.
data:it_sflight like table of sflight,
     wa_sflight like sflight.
data: gi_sflight type standard table of st_sflight.
data: ok_code like sy-ucomm.


data:
     go_grid             type ref to cl_gui_alv_grid,
     go_custom_container type ref to cl_gui_custom_container.


*****************************" 选择屏幕窗口对像表结构表
tables sscrfields.
selection-screen:
           begin of line,
             pushbutton 2(10) s1 user-command c1,
             pushbutton 12(10) s2 user-command c2,
           end of line.


**************************为下拉列表的定义
type-pools: vrm.
data: it_val type vrm_values,
      w_line like line of it_val.
*****************************




initialization.
**********************初始下拉列表
  w_line-key = '1'.
  w_line-text = '销售部'.
  append w_line to it_val.
  clear w_line.
  w_line-key = '2'.
  w_line-text = '国贸部'.
  append w_line to it_val.
  clear w_line.
  w_line-key = '3'.
  w_line-text = '洁具销售部'.
  append w_line to it_val.
  clear w_line.
  call function 'VRM_SET_VALUES'
    exporting
      id     = 'zsxdw'
      values = it_val.
*************************按钮初始化
  s1 = '按钮1'.
  s2 = '按钮2'.


  select-options:zid for sflight-carrid.
  select-options:zcoid for sflight-connid.
  parameter:zsxdw as listbox visible length 10.

***************************选择屏幕按钮的触发
at selection-screen.
  case sscrfields-ucomm.
    when 'C1'.
      zid-low = 'AC'.
      zid-sign = 'I'.
      zid-option = 'EQ'.
      append zid.
      zsxdw = '1'.
    when 'C2'.
      zid-low = 'AF'.
      zid-sign = 'I'.
      zid-option = 'EQ'.
      clear zid[].
      append zid.
      zsxdw = '2'.
  endcase.



start-of-selection.


  perform get_data.

  set screen 200.


  data: num1(4) type n value '800'.

                                                            "初始化窗口800。

*********************************************根据选择读下拉列表
  read table it_val with key key = zsxdw into w_line.
  zkey = w_line-key.
  ztext =  w_line-text.

**********************************************************屏幕的输出处理
module status_0200 output.
  set pf-status 'ZSTANDARD'.
**********************************************初始客户在标准屏幕上的控件
  if go_custom_container is initial.
    create object go_custom_container
      exporting container_name = 'ALV_OOO'.
    create object go_grid
      exporting
        i_parent = go_custom_container.

    perform load_data_into_grid.
  endif.

  create object o_event_receiver."创造事件对象
*************************************注册时间对象
  set handler o_event_receiver->handle_user_command for go_grid.
  set handler o_event_receiver->handle_toolbar for go_grid.
  set handler o_event_receiver->handle_menu_button for go_grid .
  set handler o_event_receiver->handle_double_click for go_grid .
  set handler o_event_receiver->handle_hotspot_click for go_grid .
*   SET HANDLER o_event_receiver->handle_button_click FOR go_grid .
  set handler o_event_receiver->handle_before_user_command for go_grid.
  set handler o_event_receiver->handle_data_changed for go_grid.
  set handler o_event_receiver->handle_data_changed_finished for
go_grid.

  call method go_grid->set_toolbar_interactive.
  call method cl_gui_control=>set_focus
    exporting
      control = go_grid.



endmodule.                 " STATUS_0100  OUTPUT

**********************************************************屏幕的输入处理
module user_command_0200 input.
  case ok_code.
    when 'EXIT' or 'BACK' or 'UP' or 'CANCEL'.
      leave to  screen 0.
    when 'ZZSAVE'.
      data :show_num type n.
      show_num = zc.
      message show_num type 'S'.
      perform save_changes.


    when 'ZF'.
      show_num = zc.
      message show_num type 'S'.
  endcase.
endmodule.                 " USER_COMMAND_0100  INPUT

*&---------------------------------------------------------------------*
*&      Form  load_data_into_grid
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form
  load_data_into_grid .
  data:
   l_layout type disvariant.
  l_layout-report = sy-repid.

  loop at gi_sflight into g_wa_sflight.
    if g_wa_sflight-paymentsum < 1000.
      g_wa_sflight-traffic_light = '1'.
      g_wa_sflight-line_color    = 'C610'.
    elseif g_wa_sflight-paymentsum => 1000 and
           g_wa_sflight-paymentsum < 2000.
      g_wa_sflight-traffic_light = '2'.
    else.
      g_wa_sflight-traffic_light = '3'.
    endif.
    modify gi_sflight from g_wa_sflight.
  endloop.



  perform prepare_field_catalog changing gt_fieldcat .
  perform prepare_layout changing gs_layout .
  perform prepare_drilldown_values.

  go_grid->set_table_for_first_display(
    exporting
*          I_BUFFER_ACTIVE               = I_BUFFER_ACTIVE
*          I_BYPASSING_BUFFER            = I_BYPASSING_BUFFER
*          I_CONSISTENCY_CHECK           = I_CONSISTENCY_CHECK
      i_structure_name              = 'SFLIGHT'
*          IS_VARIANT                    = IS_VARIANT
      i_save                        = 'A'
*          I_DEFAULT                     = 'X'
      is_layout                     = gs_layout
*          IS_PRINT                      = IS_PRINT
*          IT_SPECIAL_GROUPS             = IT_SPECIAL_GROUPS
*          IT_TOOLBAR_EXCLUDING          = IT_TOOLBAR_EXCLUDING
*          IT_HYPERLINK                  = IT_HYPERLINK
*          IT_ALV_GRAPHICS               = IT_ALV_GRAPHICS
*          IT_EXCEPT_QINFO               = IT_EXCEPT_QINFO
*          IR_SALV_ADAPTER               = IR_SALV_ADAPTER
    changing
      it_outtab                     = gi_sflight
      it_fieldcatalog               = gt_fieldcat
*          IT_SORT                       = IT_SORT
*          IT_FILTER                     = IT_FILTER
         ).


endform.                    " load_data_into_grid


*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_receiver implementation.
  method handle_toolbar.
    constants:
      c_button_normal           type i value 0,
      c_menu_and_default_button type i value 1,
      c_menu                    type i value 2,
      c_separator               type i value 3,
      c_radio_button            type i value 4,
      c_checkbox                type i value 5,
      c_menu_entry              type i value 6.
    data:
        ls_toolbar  type stb_button,
        no_toolbar type stb_button.


    clear ls_toolbar.
    move c_separator to ls_toolbar-butn_type.
    tab_toolbar = e_object->mt_toolbar.
**************************屏幕更新的时候  按钮 只添加一次。
   read table tab_toolbar with key function = 'ZCHANGE' into no_toolbar.
    if sy-subrc <> 0.
      clear ls_toolbar.
****************************添加按钮 函数
      move 'ZCHANGE'        to ls_toolbar-function.
      move  icon_change    to ls_toolbar-icon.
      move 'Change flight' to ls_toolbar-quickinfo.
      move '修改'        to ls_toolbar-text.
      move ' '             to ls_toolbar-disabled.
      append ls_toolbar  to e_object->mt_toolbar.
    endif.
    clear no_toolbar.
    read table tab_toolbar with key function = 'ZSAVE' into no_toolbar.
    if sy-subrc <> 0.
      clear ls_toolbar.
      move 'ZSAVE'        to ls_toolbar-function.
      move  icon_system_save    to ls_toolbar-icon.
      move '保存表里的数据到系统数据库' to ls_toolbar-quickinfo.
      move '保存到数据库'        to ls_toolbar-text.
      move ' '             to ls_toolbar-disabled.
      append ls_toolbar    to e_object->mt_toolbar.
    endif.
read table tab_toolbar with key function = 'ZSELECTION' into no_toolbar.
    if sy-subrc <> 0.
      clear ls_toolbar.
      move 'ZSELECTION'        to ls_toolbar-function.
      move '编辑列' to ls_toolbar-quickinfo.
      move '编辑列'        to ls_toolbar-text.
      move ' '             to ls_toolbar-disabled.
      append ls_toolbar    to e_object->mt_toolbar.
    endif.
    read table tab_toolbar with key function = 'ZINPUT' into no_toolbar.
    if sy-subrc <> 0.
      clear ls_toolbar.
      move 'ZINPUT'        to ls_toolbar-function.
      move '手工输入' to ls_toolbar-quickinfo.
      move '手工输入'        to ls_toolbar-text.
      move ''             to ls_toolbar-disabled.
    endif.
    append ls_toolbar    to e_object->mt_toolbar.
    read table tab_toolbar with key function = 'ZREF' into no_toolbar.
    if sy-subrc <> 0.
      clear ls_toolbar.
      move 'ZREF'        to ls_toolbar-function.
      move '刷新' to ls_toolbar-quickinfo.
      move '刷新'        to ls_toolbar-text.
      move  ''             to ls_toolbar-disabled.
      append ls_toolbar    to e_object->mt_toolbar.
    endif.
  endmethod.                    "handle_toolbar
  method handle_user_command.
    case e_ucomm.
      when 'ZCHANGE'.
*        CALL FUNCTION 'ZCHANGE'.
*****************8判断是否选择行, 没有则提示

        call method go_grid->get_selected_rows
          importing
            et_index_rows = gi_index_rows
            et_row_no     = zrow_no.
        describe table gi_index_rows lines l_lines.
        if l_lines = 0.
          call function 'POPUP_TO_DISPLAY_TEXT'
            exporting
              titel        = '操作提示! '
              textline1    = '请选择你要修改的行记录之后,'
              textline2    = '   再点“修改”按钮。'
              start_column = 4
              start_row    = 4.
          exit.
        endif.

        loop at gi_index_rows into g_selected_row.
     read table gi_sflight index g_selected_row-index into g_wa_sflight.
          move-corresponding g_wa_sflight to zd.
        endloop.
        leave to screen '200'.

      when 'ZSAVE'.
        describe table gi_index_rows lines l_lines.
        clear it_sflight.
        loop at gi_sflight into g_wa_sflight.
          move-corresponding g_wa_sflight to wa_sflight.
          append wa_sflight to it_sflight.
        endloop.
        modify sflight from table it_sflight.
        if sy-subrc = 0.
          message s000(zd003) with '保存成功!'.
        else.
          message e000(zd003) with '保存不成功!'.
        endif.

      when 'ZSELECTION'.
************************888框架操作
        go_grid->get_frontend_fieldcatalog(
          importing
            et_fieldcatalog = gt_fieldcat
               ).

        loop at gt_fieldcat into wa_fieldcat.
          if wa_fieldcat-fieldname = 'PRICE' .
            wa_fieldcat-edit = 'X' .
            modify gt_fieldcat from wa_fieldcat.
          endif .
        endloop .
        call method go_grid->set_frontend_fieldcatalog
          exporting
            it_fieldcatalog = gt_fieldcat.
        call method go_grid->refresh_table_display.
        call method cl_gui_cfw=>flush.

        data: zvalue type string.

        go_grid->get_selected_cells(
          importing
            et_cell = zcw
               ).
        loop at zcw into wa_cw.
          read table gi_sflight into g_wa_sflight index wa_cw-row_id.
          concatenate 'g_wa_sflight-' wa_cw-col_id into zvalue.
          message s000(zd003) with '你选择的值为:'  (zvalue).
        endloop.


      when 'ZINPUT'.
        perform adjust_editables using gi_sflight.
      when 'ZREF'.
        loop at gi_sflight into g_wa_sflight.
          clear g_wa_sflight-cellstyles.
          modify gi_sflight from g_wa_sflight.
        endloop.
        m1_flag = 1.
        call method go_grid->refresh_table_display.
        call method cl_gui_cfw=>flush.

    endcase.

  endmethod.                    "handle_user_command

*Handle Hotspot Click
  method handle_hotspot_click .
*    PERFORM handle_hotspot_click USING e_row_id e_column_id es_row_no .
  endmethod .                    "handle_hotspot_click

*Handle Double Click
  method handle_double_click .
    perform handle_double_click using e_row e_column es_row_no .
  endmethod .                    "handle_double_click

*Handle Before User Command
  method handle_before_user_command .
*    PERFORM handle_before_user_command USING e_ucomm .
  endmethod .                    "handle_before_user_command

*Handle Data Changed
  method handle_data_changed .
*    PERFORM handle_data_changed USING er_data_changed .
  endmethod.                    "handle_data_changed

*Handle Data Changed Finished
  method handle_data_changed_finished .
*    PERFORM handle_data_changed_finished USING e_modified .
  endmethod .                    "handle_data_changed_finished

*Handle Menu Buttons
  method handle_menu_button .
*    PERFORM handle_menu_button USING e_object e_ucomm .
  endmethod .                    "handle_menu_button
endclass.               "lcl_event_receiver


*&---------------------------------------------------------------------*
*&      Form  save_changes
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form save_changes .

  data: l_traffic_light type c.
  if zd-carrid <> '' and zd-connid <> '' and zd-fldate <> ''.
    move-corresponding zd to g_wa_sflight.
    modify gi_sflight index g_selected_row-index from g_wa_sflight.

    call method go_grid->refresh_table_display.
    call method cl_gui_cfw=>flush.
    clear zd.
  else.
    message i000(zd003) with '请填写前三项!'.
  endif.

endform.                    " save_changes

*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form
  get_data .
  select *
    from sflight
    into corresponding fields of table gi_sflight
    where carrid in zid and connid in zcoid.
endform.                    " get_data
*&---------------------------------------------------------------------*
*&      Form  prepare_field_catalog
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_GT_FIELDCAT  text
*----------------------------------------------------------------------*
form
  prepare_field_catalog  changing pt_fieldcat type lvc_t_fcat .
  data ls_fcat type lvc_s_fcat .
  clear ls_fcat .
  ls_fcat-fieldname = 'FLDATE' .
*  ls_fcat-ref_table = 'SFLIGHT' .
*  ls_fcat-ref_table = 'CONNID' .
  ls_fcat-outputlen = '10' .
*  ls_fcat-emphasize = 'C410'.
  ls_fcat-drdn_hndl = '1' .     "设置为下拉表形式
*  ls_fcat-drdn_field = 'FLDATE'.

*ls_fcat-EDIT = 'X'.

  append ls_fcat to pt_fieldcat .
    clear ls_fcat .
*    ls_fcat-drdn_field = 'PLANETYPE'.
  ls_fcat-fieldname = 'PLANETYPE' .
*  ls_fcat-ref_table = 'SFLIGHT' .
*  ls_fcat-ref_table = 'CONNID' .
  ls_fcat-outputlen = '10' .
*  ls_fcat-emphasize = 'C410'.
  ls_fcat-drdn_hndl = '2' .     "设置为下拉表形式
*  ls_fcat-drdn_field = 'PLANETYPE'.
  append ls_fcat to pt_fieldcat .

endform.                    " prepare_field_catalog
*&---------------------------------------------------------------------*
*&      Form  prepare_layout
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_GS_LAYOUT  text
*----------------------------------------------------------------------*
form
  prepare_layout  changing ps_layout type lvc_s_layo.
*************************************************布局设置
  ps_layout-grid_title = 'Flights'."ALV的标题
  ps_layout-excp_fname = 'TRAFFIC_LIGHT'.
  ps_layout-info_fname = 'LINE_COLOR'.
  ps_layout-zebra = 'X' .
  ps_layout-sel_mode = 'D'.
  ps_layout-stylefname = 'CELLSTYLES' .

endform.                    " prepare_layout

*--- Drilldown values
form  prepare_drilldown_values.

  data lt_ddval type lvc_t_drop .
  data ls_ddval type lvc_s_drop .
  ls_ddval-handle = '1' .
  ls_ddval-value = '20080502' .
  append ls_ddval to lt_ddval .
  ls_ddval-handle = '1' .
  ls_ddval-value = 'JSF-44' .
  append ls_ddval to lt_ddval .
  ls_ddval-handle = '1' .
  ls_ddval-value = 'KMDA-53' .
  append ls_ddval to lt_ddval .
  ls_ddval-handle = '1' .
  ls_ddval-value = 'SS3O/N' .
  append ls_ddval to lt_ddval .

  go_grid->set_drop_down_table(
      it_drop_down       = lt_ddval
         ).


  data lt_val type lvc_t_drop .
*  DATA ls_ddval TYPE lvc_s_drop .
  ls_ddval-handle = '2' .
  ls_ddval-value = 'aaa' .
  append ls_ddval to lt_val .
  ls_ddval-handle = '2' .
  ls_ddval-value = 'bbb' .
  append ls_ddval to lt_val .
  ls_ddval-handle = '2' .
  ls_ddval-value = 'ccc' .
  append ls_ddval to lt_val .
  ls_ddval-handle = '2' .
  ls_ddval-value = 'ddd' .
  append ls_ddval to lt_val .

  go_grid->set_drop_down_table(
      it_drop_down       = lt_val
         ).
endform. " prepare_drilldown_values

*&---------------------------------------------------------------------*
*&      Form  handle_double_click
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->I_ROW      text
*      -->I_COLUMN   text
*      -->I_ROW      text
*      -->I_COLUMN   text
*      -->IS_ROW_NO  text
*----------------------------------------------------------------------*
form
  handle_double_click using i_row type lvc_s_row i_column type
lvc_s_col is_row_no type lvc_s_roid.
  data: zvalue type string.

  read table gi_sflight into g_wa_sflight index is_row_no-row_id .
  concatenate 'g_wa_sflight-' i_column into zvalue.
  message s000(zd003) with '你双击的单元格值为:'  (zvalue).
endform. .                    "handle_double_click
*&---------------------------------------------------------------------*
*&      Form  adjust_editables
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GI_SFLIGHT  text
*----------------------------------------------------------------------*
form
  adjust_editables  using   pt_list like gi_sflight .
  if m1_flag = 1.
    data ls_listrow like line of pt_list .
    data ls_stylerow type lvc_s_styl .
    data lt_styletab type lvc_t_styl .
    loop at pt_list into ls_listrow .

      if ls_listrow-carrid = 'AC' .
        ls_stylerow-fieldname = 'FLDATE' .
        ls_stylerow-style = cl_gui_alv_grid=>mc_style_disabled .
        append ls_stylerow to lt_styletab .
      endif .
      if ls_listrow-connid = '0455' .
        ls_stylerow-fieldname = 'PAYMENTSUM' .
        ls_stylerow-style = cl_gui_alv_grid=>mc_style_enabled .
        append ls_stylerow to lt_styletab .
      endif .
      clear ls_stylerow.
      loop at lt_styletab  into ls_stylerow.
        append ls_stylerow to ls_listrow-cellstyles.
*INSERT LINES OF lt_styletab INTO ls_listrow-cellstyles .
        modify pt_list from ls_listrow .
      endloop.

      clear lt_styletab.
      call method go_grid->refresh_table_display.
      call method cl_gui_cfw=>flush.
    endloop .
    m1_flag = 0.
  endif.

endform.                    " adjust_editable

你可能感兴趣的:(command,table,OO,Go,button,menu)