当前位置: 代码迷 >> 综合 >> OOnbsp;ALV常用功能完整简例(热键单击…
  详细解决方案

OOnbsp;ALV常用功能完整简例(热键单击…

热度:87   发布时间:2024-01-18 14:43:50.0

OO ALV 常用功能简介

目录

OO ALV 常用功能简介... 1

一、所用类:... 2

二、效果图:... 2

单击... 2

双击... 2

F4帮助... 3

编辑某单元格并校验... 3

三、实现过程:... 3

1 创建一个屏幕9001,不需要画任何控件

2 定义数据:... 3

3 获取显示数据:... 4

4 alv 显示数据... 4



 

一、所用类:

cl_gui_alv_grid

cl_gui_docking_container

二、效果图:

     执行:

   OO <wbr>ALV常用功能完整简例(热键单击,双击,帮助,编辑,自定义工具条等)

单击

     单击航线单元格:

OO <wbr>ALV常用功能完整简例(热键单击,双击,帮助,编辑,自定义工具条等)

 

双击

    双击连接号码单元格:


OO <wbr>ALV常用功能完整简例(热键单击,双击,帮助,编辑,自定义工具条等)


 

F4帮助

   航班单元格上F4或点击 后面的小按钮

 

   (本功能在设置字段样式时实现DEFINE mac_fieldlog.

     "设置参考表和参考字段,为了提供搜索帮助
  wa_fieldcatalog
-ref_table       &4           "参考表
  wa_fieldcatalog
-ref_field       &5           "参考字段

    

      OO <wbr>ALV常用功能完整简例(热键单击,双击,帮助,编辑,自定义工具条等)

编辑某单元格并校验

 修改价格后随便点击某个按钮或者双击某个字段,触发:handle_data_changed

 

三、实现过程:

1 创建一个屏幕9001,不需要画

2 定义数据:

*&---------------------------------------------------------------

*&  ALV  全局变量
*&---------------------------------------------------------------

DATA: g_alv_grid    TYPE REF TO cl_gui_alv_grid,"ALV控件
      alv_ctner     
TYPE REF TO cl_gui_docking_container.


DATA: wa_fieldcatalog    TYPE lvc_s_fcat   "显示数据列内表工作区域
      it_fieldcatalog    
TYPE lvc_t_fcat   "显示数据列内表

DATA: g_event              TYPE REF TO alv_event_receiver.
*&---------------------------------------------------------------
*&  全局变量
*&---------------------------------------------------------------
DATA:
      
BEGIN OF str_spfli,
        
checkbox TYPE char1                     "复选框
        carrid   
TYPE  sflight-carrid       "航线承运人ID
        connid   
TYPE  sflight-connid       "航班连接 Id
        fldate   
TYPE  sflight-fldate       "航班日期
        price    
TYPE  sflight-price        "航空运费
      
END OF  str_spfli.

DATA:
      g_it_show 
LIKE TABLE OF str_spfli WITH KEY checkbox carrid.
      g_wa_show 
LIKE str_spfli.

 

 

3 获取显示数据:

  "获取数据
  
SELECT carrid
         connid
         fldate
         price
    
FROM sflight INTO CORRESPONDING FIELDS OF  TABLE g_it_show.

4 alv 显示数据

PROCESS BEFORE OUTPUT.
 
MODULE STATUS_9001.
 
"alv 显示数据
 
MODULE MDL_ALV_SHOW.

*&---------------------------------------------------------------

module MDL_ALV_SHOW output.
PERFORM zform_alv_show.

endmodule.       

*&---------------------------------------------------------------

 

FORM zform_alv_show .
  
DATA:   l_style      TYPE i,
          ls_layout    
TYPE lvc_s_layo,
          is_stable    
TYPE lvc_s_stbl,
          l_disvariant 
TYPE disvariant.

屏幕初始化
  
IF alv_ctner IS INITIAL.

    
CREATE OBJECT alv_ctner
      
EXPORTING
        repid                       
sy-repid
        dynnr                       
'9001'
      side                        cl_gui_docking_container=>dock_at_right      ”ALV贴屏幕右边,从屏幕右边开始算宽度
        side                        
cl_gui_docking_container=>dock_at_left       "ALV贴屏幕左边,从左边算屏幕宽度,
        extension                   
900                                          "屏幕宽度
        style                       
cl_gui_control=>ws_child                     "可选参数,设置ALV是否可用手动拖动大小
      
EXCEPTIONS
        cntl_error                  
1
        cntl_system_error           
2
        create_error                
3
        lifetime_error              
4
        lifetime_dynpro_dynpro_link 
5
        
OTHERS                      6.

    
IF sy-subrc <> 0.
      
MESSAGE s001(00) WITH '屏幕初始化失败'.
      
LEAVE LIST-PROCESSING.
    
ENDIF.

*添加自定义工具条
    
PERFORM frm_set_alv_toolbar.
*
    
CREATE OBJECT g_alv_grid
      
EXPORTING
        i_parent 
alv_ctner.

*构建显示字段样式
    
PERFORM frm_build_fieldcat.

    
"Set/Reset Ready for Input Status
    
CALL METHOD g_alv_grid->set_ready_for_input
      
EXPORTING
        i_ready_for_input 
1.

layout 设定样式
    
CLEAR ls_layout.
    ls_layout
-cwidth_opt 'X'.     "优化列宽

    
CLEAR l_disvariant.
    l_disvariant
-report sy-repid"当前程序

event  绑定事件
    
CREATE OBJECT g_event.


    
"自定义工具条
    
SET HANDLER g_event->toolbar             FOR g_alv_grid.  

    "数据修改事件  

SET HANDLER g_event->handle_data_changed FOR g_alv_grid.

"单击某一单元格事件
    
SET HANDLER g_event->handle_cell_click   FOR g_alv_grid.

"用户点击工具条上按钮触发的事件
    
SET HANDLER g_event->handle_user_command FOR g_alv_grid.

"双击某一单元格事件
    
SET HANDLER g_event->handle_double_click FOR g_alv_grid.

 

"以下两个方法达到的效果是:光标离开正在编辑的单元格时,

就触发了handle_data_changed
   
"如果不注释掉以下两个方法,

则只有执行下一操作时才会触发handle_data_changed,比如点保存、删除
    
CALL METHOD g_alv_grid->register_edit_event
      
EXPORTING
        i_event_id 
cl_gui_alv_grid=>mc_evt_modified.

    
CALL METHOD g_alv_grid->register_edit_event
      
EXPORTING
        i_event_id 
cl_gui_alv_grid=>mc_evt_enter.

  "显示内表数据
    
CALL METHOD g_alv_grid->set_table_for_first_display
      
EXPORTING
        is_layout            
ls_layout
        is_variant           
l_disvariant
        it_toolbar_excluding 
gt_tools
      
CHANGING
        it_fieldcatalog      
it_fieldcatalog[]
        it_outtab            
g_it_show[].

  
ELSE.

    
"冲销设置tcode  RELOAD,防止因为原来的tcode影响程序执行
    
CALL METHOD cl_gui_cfw=>set_new_ok_code
      
EXPORTING
        new_code 
'RELOAD'.

    
"ALV 控制:刷新稳定性 按行刷新,可选参数
    
CLEAR is_stable.
    is_stable
-col 'X'.

    
"刷新
    
CALL METHOD g_alv_grid->refresh_table_display
      
EXPORTING
        is_stable 
is_stable
        i_soft_refresh 
'X'.

    
"提交
    
CALL METHOD cl_gui_cfw=>flush.

  
ENDIF.
ENDFORM.                    ZFORM_ALV_SHOW

 

 

其中:

添加工具条上常用按钮

FORM frm_set_alv_toolbar .
  
REFRESH gt_tools[].

"你也可以删除标准的功能按钮


    DELETE e_object->mt_toolbar WHERE FUNCTION = '&DETAIL'.

    DELETE e_object->mt_toolbar WHERE FUNCTION = '&REFRESH'.

    DELETE e_object->mt_toolbar WHERE FUNCTION = '&LOCAL&UNDO'.

    DELETE e_object->mt_toolbar WHERE FUNCTION = '&SORT_ASC'.

    DELETE e_object->mt_toolbar WHERE FUNCTION = '&SORT_DSC'.


    DELETE e_object->mt_toolbar WHERE FUNCTION = '&FIND'.

    DELETE e_object->mt_toolbar WHERE FUNCTION = '&FIND_MORE'.

    DELETE e_object->mt_toolbar WHERE FUNCTION = '&MB_FILTER'.

    DELETE e_object->mt_toolbar WHERE FUNCTION = '&MB_SUM'.

    DELETE e_object->mt_toolbar WHERE FUNCTION = '&MB_SUBTOT'.

    DELETE e_object->mt_toolbar WHERE FUNCTION = '&MB_EXPORT'.

    DELETE e_object->mt_toolbar WHERE FUNCTION = '&MB_VARIANT'.


    DELETE e_object->mt_toolbar WHERE quickinfo = '插入行'.

    DELETE e_object->mt_toolbar WHERE quickinfo = '删除行'.

    DELETE e_object->mt_toolbar WHERE quickinfo = '剪切'.

    DELETE e_object->mt_toolbar WHERE quickinfo = '复制文本'.

    DELETE e_object->mt_toolbar WHERE quickinfo = '插入总览'.

    DELETE e_object->mt_toolbar WHERE quickinfo = '附加行'.

    DELETE e_object->mt_toolbar WHERE quickinfo = '复制行'.

    DELETE e_object->mt_toolbar WHERE quickinfo = '视图'.

    DELETE e_object->mt_toolbar WHERE quickinfo = '显示图形'.

    DELETE e_object->mt_toolbar WHERE quickinfo = '撤销'.

    DELETE e_object->mt_toolbar WHERE quickinfo = '明细'.

    DELETE e_object->mt_toolbar WHERE quickinfo = '打印'.

    DELETE e_object->mt_toolbar WHERE quickinfo = '最终用户文档'.


  
APPEND cl_gui_alv_grid=>mc_fc_check             TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_loc_cut           TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_loc_copy          TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_loc_copy_row      TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_loc_delete_row    TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_loc_insert_row    TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_loc_move_row      TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_loc_paste         TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_loc_paste_new_row TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_sum               TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_info              TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_refresh           TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_graph             TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_loc_append_row    TO gt_tools.
  
APPEND cl_gui_alv_grid=>mc_fc_loc_undo          TO gt_tools.
ENDFORM.                    FRM_SET_ALV_TOOLBAR

 

设置显示字段和其参考字段


DEFINE mac_fieldlog.
g_pos g_pos 1.                               "第几列
wa_fieldcatalog-col_pos         g_pos.         "
  wa_fieldcatalog
-fieldname       &1           "字段名称
wa_fieldcatalog-coltext         &2.            "显示名称,跟reptext效果基本相同
  wa_fieldcatalog
-reptext         &2.
  wa_fieldcatalog
-outputlen       &3           "显示长度

  
"设置参考表和参考字段,为了提供搜索帮助
  wa_fieldcatalog
-ref_table       &4           "参考表
  wa_fieldcatalog
-ref_field       &5           "参考字段

  
"设置复选框可编辑
  
if &1 eq 'CHECKBOX'.
    wa_fieldcatalog
-checkbox      'X'.
    wa_fieldcatalog
-edit          'X'.
  
endif.

  
"设置 价格PRICE字段 可修改
  
if &1 eq 'PRICE'.
    wa_fieldcatalog
-edit 'X'.
  
endif.

  
"设置   航线承运人字段 CARRID 为热键
  
if &1 eq 'CARRID'.
    wa_fieldcatalog
-hotspot 'X'.
  
endif.

  
append wa_fieldcatalog to it_fieldcatalog.
  
clear wa_fieldcatalog.
END-OF-DEFINITION.

 
FORM frm_build_fieldcat .

mac_fieldlog 'CHECKBOX' '选择        ''         .

mac_fieldlog 'CARRID'   '航线承运人  ''    'SFLIGHT' 'CARRID'.

mac_fieldlog 'CONNID'   '航班数量    ''    'SFLIGHT' 'CONNID' .
  mac_fieldlog 
'FLDATE'   '国家        ''    'SFLIGHT' 'FLDATE '.  "最后两个字段 参考表内字段,可以作为帮助
  mac_fieldlog 
'PRICE'    '起飞城市    ''    'SFLIGHT' 'PRICE'  .
ENDFORM. FRM_BUILD_FIELDCAT

 

事件的定义

*----------------------------------------------------------------
      CLASS ALV_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------

CLASS alv_event_receiver DEFINITION.
  
PUBLIC SECTION.

    
"添加工具条
    
METHODS toolbar              FOR EVENT toolbar     OF cl_gui_alv_grid
       
IMPORTING
         e_object
         e_interactive
.

    
"子都修改时触发
    
METHODS handle_data_changed  FOR EVENT data_changed OF cl_gui_alv_grid
      
IMPORTING
        er_data_changed
.


    
"某字段设置热键后,单击此单元格时触发
    
METHODS  handle_cell_click   FOR EVENT hotspot_click OF cl_gui_alv_grid
      
IMPORTING
        e_row_id
        e_column_id
        es_row_no
.

    
"双击时触发
    
METHODS handle_double_click  FOR EVENT double_click  OF cl_gui_alv_grid
      
IMPORTING
        e_row
        e_column
        es_row_no
.

    
"F1时触发
    
METHODS handle_onf1          FOR EVENT onf1          OF cl_gui_alv_grid
       
IMPORTING
        e_fieldname
        es_row_no
        er_event_data
.

    
"当用户按屏幕上按钮时触发
    
METHODS handle_user_command  FOR EVENT user_command  OF cl_gui_alv_grid
       
IMPORTING
         e_ucomm
.
    
.
ENDCLASS.                    "alv_event_receiver DEFINITION


*&---------------------------------------------------------------------*
*&  Class (Implementation)  ALV_EVENT_RECEIVER
*&---------------------------------------------------------------------*

CLASS alv_event_receiver IMPLEMENTATION.
  
METHOD handle_data_changed.

    
DATA:
          l_it_data   
TYPE  lvc_t_modi,
          l_wa_data   
LIKE LINE OF l_it_data.

    
DATA: l_price_old TYPE string,
          l_price_new 
TYPE string,
          l_msg       
TYPE string.

    l_it_data 
er_data_changed->mt_mod_cells.


    
LOOP AT er_data_changed->mt_mod_cells INTO l_wa_data WHERE  fieldname 'PRICE'.

      
READ TABLE g_it_show INTO g_wa_show INDEX l_wa_data-row_id.
      
IF sy-subrc 0.
        l_price_old 
g_wa_show-price .
        l_price_new 
l_wa_data-value .
        
IF l_price_old <> l_price_new.
          
CONCATENATE '价格由l_price_old '改为l_price_new INTO l_msg.
          
MESSAGE l_msg TYPE 'I'.
        
ENDIF.
      
ENDIF.
    
ENDLOOP.




  
ENDMETHOD.                    "handle_data_changed
  
METHOD toolbar.
    
DATA: ls_toolbar  TYPE stb_button.

    
CLEAR ls_toolbar.
    
MOVE            TO ls_toolbar-butn_type     "分隔符
    
APPEND ls_toolbar TO e_object->mt_toolbar.

    
CLEAR ls_toolbar.
    
MOVE 'SEL_ALL'       TO ls_toolbar-function.      "添加两个按钮 全选   取消全选
    
MOVE icon_select_all TO ls_toolbar-icon.
    
MOVE '全选         TO ls_toolbar-quickinfo.
    
MOVE             TO ls_toolbar-disabled.
    
APPEND ls_toolbar    TO e_object->mt_toolbar.


    
CLEAR ls_toolbar.
    
MOVE 'CAN_ALL'          TO ls_toolbar-function.
    
MOVE icon_deselect_all  TO ls_toolbar-icon.
    
MOVE '全部取消         TO ls_toolbar-quickinfo.
    
MOVE                TO ls_toolbar-disabled.
    
APPEND ls_toolbar       TO e_object->mt_toolbar.

    
CLEAR ls_toolbar.
    
MOVE             TO ls_toolbar-butn_type             "按钮
    
MOVE 'DELETE'      TO ls_toolbar-function.               "功能码
    
MOVE 'DELETE'      TO ls_toolbar-text.                   "显示名称
    
MOVE icon_cancel   TO ls_toolbar-icon.                   "图标
    
MOVE '删除选中行  TO ls_toolbar-quickinfo             "鼠标停留时的提示信息
    
MOVE           TO ls_toolbar-disabled              "可用
    
APPEND ls_toolbar  TO e_object->mt_toolbar.
  
ENDMETHOD.                    "toolbar


  
METHOD handle_cell_click.

    
"获取单击字段
    
DATA: l_msg      TYPE string,
          l_wa_click 
LIKE LINE OF g_it_show,
          l_row      
TYPE string,
          l_field    
TYPE string.


    l_row      
es_row_no-row_id              "选中行
    l_field    
e_column_id                   "选中的字段

    
READ TABLE g_it_show INTO l_wa_click INDEX es_row_no-row_id.

    
IF sy-subrc 0.
      
"得到所单击值
      
CONCATENATE '您选中第l_row  '行,字段为:l_field  ',选中值为l_wa_click-carrid INTO l_msg.
      
MESSAGE l_msg TYPE 'I'.
    
ENDIF.
  
ENDMETHOD.                    "handle_cell_click


  
METHOD handle_double_click.
    
"获取单击字段
    
DATA: l_msg      TYPE string,
          l_wa_click 
LIKE LINE OF g_it_show,
          l_row      
TYPE string,
          l_field    
TYPE string.

    l_field      
e_column-fieldname              "选中行
    l_row        
es_row_no-row_id                "选中的字段

    
READ TABLE g_it_show INTO l_wa_click INDEX es_row_no-row_id .

    
IF sy-subrc 0.
      
"得到所单击值
      
CONCATENATE '您选中第l_row  '行,字段为:l_field  ',选中值为:'  INTO l_msg.

      
CASE l_field.
        
WHEN 'CONNID'.
          
CONCATENATE l_msg l_wa_click-connid INTO l_msg.
          
MESSAGE l_msg TYPE 'I'.
        
WHEN 'FLDATE'.
          
CONCATENATE l_msg l_wa_click-fldate INTO l_msg.
          
MESSAGE l_msg TYPE 'I'.
      
ENDCASE.

    
ENDIF.
  
ENDMETHOD.                    "handle_DOUBLE_CLICK

  
METHOD handle_onf1.


  
ENDMETHOD.                    "handle_ONF1

*&---------------------------------------------------------------------------&*
*&                                                                            *
*&      HANDLE_USER_COMMAND                                                   *
*&---------------------------------------------------------------------------&*
  
METHOD handle_user_command.

    
REFRESH:lt_cols lt_rows.

    
CALL METHOD g_alv_grid->get_selected_rows
      
IMPORTING
        et_index_rows 
lt_rows.

    
CALL METHOD g_alv_grid->get_selected_columns
      
IMPORTING
        et_index_columns 
lt_cols.
    
CALL METHOD cl_gui_cfw=>flush.


    
CASE  e_ucomm.
      
WHEN 'SEL_ALL'.
        
LOOP AT g_it_show INTO g_wa_show.
          g_wa_show
-checkbox 'X'.
          
MODIFY g_it_show  FROM g_wa_show.
        
ENDLOOP.
      
WHEN 'CAN_ALL'.
        
LOOP AT g_it_show INTO g_wa_show.
          g_wa_show
-checkbox '.
          
MODIFY g_it_show  FROM g_wa_show.
        
ENDLOOP.

      
WHEN 'DELETE'.
        
LOOP AT g_it_show INTO g_wa_show WHERE checkbox IS NOT INITIAL.
          
DELETE TABLE g_it_show FROM g_wa_show.
        
ENDLOOP.
      
WHEN OTHERS.
    
ENDCASE.


    
CALL METHOD cl_gui_cfw=>set_new_ok_code
      
EXPORTING
        new_code 
'RELOAD'.
  
ENDMETHOD.                    "HANDLE_USER_COMMAND
ENDCLASS. "ALV_EVENT_RECEIVER



关于删除标准按钮,我也debug的

OO <wbr>ALV常用功能完整简例(热键单击,双击,帮助,编辑,自定义工具条等)

OO <wbr>ALV常用功能完整简例(热键单击,双击,帮助,编辑,自定义工具条等)

然后可以根据功能码或者quickinfo删除