SAP ABAP SALV实现 按钮与双击事件

使用SALV实现的弹窗效果:
弹窗触发条件可以是按钮,或者通过双击等等
实现弹窗以及添加功能按钮过程:
  data: go_funlst     type ref to cl_salv_functions_list.
  data: go_layout     type ref to cl_salv_layout.
  data: lv_name_no type sy-uname.
  data:lv_date_end type sy-datum.


  data: lo_cols type ref to cl_salv_columns.
*...Process individual columns
  data: lo_column type ref to cl_salv_column.


  data: cxroot type ref to cx_root,
        excmsg type c.
  data: lv_row type i.


  clear:go_salv_table,go_funlst.


  check gt_print_info[] is not initial.


  cl_salv_table=>factory(
    exporting
      list_display   = if_salv_c_bool_sap=>false    " ALV Displayed in List Mode
    importing
      r_salv_table   = go_salv_table
    changing
      t_table        = gt_print_info[] ).


*...取得全部列的对象
  lo_cols = go_salv_table->get_columns( ).
*   设置自动优化列宽度
  lo_cols->set_optimize( 'X' ).


  go_funlst = go_salv_table->get_functions( ).
  go_funlst->set_all( 'X' ).    "重新设置列名称


  go_layout = go_salv_table->get_layout( ).


*隐藏字段属性
  lo_column ?= lo_cols->get_column( 'SEL' ).  "隐藏不想展示的字段
  lo_column->set_visible( cl_salv_column_table=>false ).
  lo_column ?= lo_cols->get_column( 'ROWS' ).
  lo_column->set_visible( cl_salv_column_table=>false ).
  if sy-langu eq '1'. 
    go_salv_table->get_columns( )->get_column( 'ROW' )->set_long_text( '行' ). "设置弹窗ALV的字段名
    go_salv_table->get_columns( )->get_column( 'QTY_PNT' )->set_long_text( '打印份数' ).
    go_salv_table->get_columns( )->get_column( 'MEINS_P' )->set_long_text( '单位' ).
  else.
    go_salv_table->get_columns( )->get_column( 'ROW' )->set_long_text( 'Row' ).
    go_salv_table->get_columns( )->get_column( 'QTY_PNT' )->set_long_text( 'Print_qty' ).
    go_salv_table->get_columns( )->get_column( 'MEINS_P' )->set_long_text( 'Unit' ).
  endif.


  if go_salv_table is bound."设置窗口大小与位置-
    go_salv_table->set_screen_popup(      start_column = 30      end_column = 100      start_line = 5      end_line = 20 ).
    "单击/双击 事件注册调用
    go_events = go_salv_table->get_event( ).
    create object o_events.
    set handler lcl_event_handler=>handle_double_click for go_events.  
    set handler o_events->on_user_command for go_events.  "注册按钮事件

    "设置自定义 PF STATUS 
    go_salv_table->set_screen_status(
      pfstatus      =  'SALV_STANDARD'  "GUI 状态
      report        =  'ZHY_MM_F_GRMAT_QRCODE_TAG'
      set_functions = go_salv_table->c_functions_all ).


    "设置选择模式
    go_selections = go_salv_table->get_selections( ).
    go_selections->set_selection_mode( if_salv_c_selection_mode=>row_column ).

    go_salv_table->display( ).  "展示
    go_salv_table->refresh( ).
  endif.
准备工作:
1、复制一个标准的GUI状态到程序中
2、创建一个INCLUDE程序,实现双击、功能按钮等(ps:SALV的双击触发功能,是我通过注册事件实现的)
*----------------------------------------------------------------------*
*       CLASS lcl_salv_buddy DEFINITION
*----------------------------------------------------------------------*
class lcl_salv_buddy definition inheriting from cl_salv_controller create private final.


  public section.


*----------------------------------------------------------------------*
* GET_CONTROL_RTTI - Returns runtime type information for the control that is behind a SALV object.
*----------------------------------------------------------------------*
    class-methods: get_control_rtti importing i_salv         type ref to cl_salv_model_base
                                    exporting e_adapter_type type salv_de_adapter_type
                                              e_control_rtti type ref to cl_abap_typedescr,


*----------------------------------------------------------------------*
* GET_CONTROL - Returns the control that is behind the SALV object.
*   MUST be called after the DISPLAY method of the SALV object, so that its control gets created.
*   See method GET_CONTROL_RTTI above for a correspondence between what you supply in paramter I_SALV and what you get back in parameter R_CONTROL.
*----------------------------------------------------------------------*
      get_control importing i_salv           type ref to cl_salv_model_base
                  returning value(r_control) type ref to object,


*----------------------------------------------------------------------*
* SET_EDITABLE - Enables OR disables editing on a CL_SALV_TABLE object.
*----------------------------------------------------------------------*
      set_editable importing value(i_fieldname) type csequence optional
                             i_salv_table       type ref to cl_salv_table
                             value(i_editable)  type abap_bool default abap_true
                             value(i_refresh)   type abap_bool default abap_true.


  private section.


*----------------------------------------------------------------------*
* GET_CONTROL_INTERNAL - It is the guts of method GET_CONTROL. It does all the heavy work and method GET_CONTROL gets all the credits
*----------------------------------------------------------------------*
    class-methods: get_control_internal importing i_salv         type ref to cl_salv_model_base
                                        exporting e_adapter_type type salv_de_adapter_type
                                                  e_control      type ref to object.


endclass.                    "lcl_salv_buddy DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_salv_buddy IMPLEMENTATION
*----------------------------------------------------------------------*
class lcl_salv_buddy implementation.




  method get_control_internal.


    data: lo_controller            type ref to cl_salv_controller_model,
          lo_adapter               type ref to cl_salv_adapter,
          lo_fullscreen_adapter    type ref to cl_salv_fullscreen_adapter,
          lo_grid_adapter          type ref to cl_salv_grid_adapter,
          lo_table_display_adapter type ref to if_salv_table_display_adapter,
          lo_tree_adapter_base     type ref to cl_salv_tree_adapter_base.


    check e_adapter_type is requested or
          e_control      is requested.


    if  e_adapter_type is requested.
      clear e_adapter_type.
    endif.


    if  e_control is requested.
      clear e_control.
    endif.


    lo_controller = i_salv->r_controller.
    check lo_controller is bound.


    lo_adapter = lo_controller->r_adapter.
    check lo_adapter is bound.


    if e_adapter_type is requested.
      e_adapter_type = lo_adapter->type.
    endif.


    check e_control is requested.


    case lo_adapter->type.
      when lo_adapter->if_salv_adapter~c_adapter_type_fullscreen.
        lo_fullscreen_adapter ?= lo_adapter.
        e_control = lo_fullscreen_adapter->get_grid( ).


      when lo_adapter->if_salv_adapter~c_adapter_type_grid.
        lo_grid_adapter ?= lo_adapter.
        e_control = lo_grid_adapter->get_grid( ).


      when lo_adapter->if_salv_adapter~c_adapter_type_hierseq.


      when lo_adapter->if_salv_adapter~c_adapter_type_list.
        lo_table_display_adapter ?= lo_adapter.
        e_control = lo_table_display_adapter->r_table.


      when lo_adapter->if_salv_adapter~c_adapter_type_tree.
        lo_tree_adapter_base ?= lo_adapter.
        e_control = lo_tree_adapter_base->r_tree.


      when lo_adapter->if_salv_adapter~c_adapter_type_append.


    endcase.


  endmethod.                    "get_control_internal


  method get_control_rtti.


    data: lv_adapter_type type salv_de_adapter_type,
          lo_control      type ref to object.


    check e_adapter_type is requested or
          e_control_rtti is requested.


    if  e_adapter_type is requested.
      clear e_adapter_type.
    endif.


    if  e_control_rtti is requested.
      clear e_control_rtti.
    endif.


    get_control_internal( exporting i_salv = i_salv importing e_adapter_type = lv_adapter_type e_control = lo_control ).


    if e_adapter_type is requested.
      e_adapter_type = lv_adapter_type.
    endif.


    if e_control_rtti is requested.
      e_control_rtti = cl_abap_typedescr=>describe_by_object_ref( lo_control ).
    endif.


  endmethod.                    "get_control_rtti


  method get_control.


    check r_control is requested.


    get_control_internal( exporting i_salv = i_salv importing e_control = r_control ).


  endmethod.                    "get_control


  method set_editable.
    constants: lc_stable type lvc_s_stbl value 'XX'.


    data: lo_grid     type ref to cl_gui_alv_grid,
          lt_fieldcat type lvc_t_fcat,
          ls_layout   type lvc_s_layo.


    field-symbols: <fs_fieldcat> like line of lt_fieldcat.


    lo_grid ?= get_control( i_salv_table ).
    check lo_grid is bound.


    if i_fieldname is supplied and
       i_fieldname is not initial.
      lo_grid->get_frontend_fieldcatalog( importing et_fieldcatalog = lt_fieldcat ).
      read table lt_fieldcat assigning <fs_fieldcat> with key fieldname = i_fieldname.
      check sy-subrc = 0.
      <fs_fieldcat>-edit = i_editable.
      lo_grid->set_frontend_fieldcatalog( lt_fieldcat ).
    else.
      lo_grid->get_frontend_layout( importing es_layout = ls_layout ).
      ls_layout-edit = i_editable.
      lo_grid->set_frontend_layout( exporting is_layout = ls_layout ).
    endif.


    check i_refresh = abap_true.
    i_salv_table->refresh( lc_stable ).


  endmethod.                    "set_editable


endclass.


data: gv_editable   type abap_bool,
      go_salv_table type ref to cl_salv_table,
      go_selections type ref to cl_salv_selections,
      go_events     type ref to cl_salv_events_table,
      go_grid       type ref to cl_gui_alv_grid.


*----------------------------------------------------------------------*
*       CLASS lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_handler definition.
  public section.
    class-methods: handle_double_click for event double_click of cl_salv_events_table
      importing row column.
    methods:
      on_user_command for event added_function of cl_salv_events
        importing e_salv_function.
endclass.                    "lcl_event_handler DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_handler implementation.
  method handle_double_click.
    if gv_editable = abap_false.
      gv_editable = abap_true.
    else.
      gv_editable = abap_false.
    endif.
    lcl_salv_buddy=>set_editable( i_fieldname = 'MENGE_P' i_salv_table = go_salv_table i_editable = gv_editable ).
    lcl_salv_buddy=>set_editable( i_fieldname = 'MEINS_P' i_salv_table = go_salv_table i_editable = gv_editable ).
    lcl_salv_buddy=>set_editable( i_fieldname = 'QTY_PNT' i_salv_table = go_salv_table i_editable = gv_editable ).
  endmethod.                    "handle_double_click


  method on_user_command.
    perform add_print_info using e_salv_function.  "实现按钮事件
  endmethod."on_user_command 
endclass.                    "lcl_event_handler IMPLEMENTATION


data: o_events type ref to lcl_event_handler.

实现按钮事件的PERFORM,可以用来参考下:

form add_print_info  using    p_function_code.
  data: lo_selections type ref to cl_salv_selections,
        lt_cells      type salv_t_cell,
        lt_rows       type salv_t_row,
        ls_cell       type salv_s_cell.
  data lv_row type i.


  data lt_print_info like table of gt_print_info.
  data ls_print_info like gt_print_info.


  "获取对象
  lo_selections = go_salv_table->get_selections( ).
  clear:lt_print_info.


  case p_function_code.
    when '&INS'.
      lt_rows = lo_selections->get_selected_rows( ). "获取选中的行
      if lt_rows is initial.
        “实现你的功能
      else.
        “实现你的功能
      endif.

    when '&DEL'.
      "选择的行(多单元格)
      lt_rows = lo_selections->get_selected_rows( ).
      if lt_rows is initial.
        "实现你的功能
      else.
                “实现你的功能
      endif.
    when 'PRINT'.
      perform prm_print_new.
    when others.
  endcase.


  go_salv_table->refresh( )."刷新
endform.

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ABAP SALVSAP List Viewer)是一个用于在SAP系统中创建和显示报表的工具。要在ABAP SALV报表中添加复选框(checkbox),你可以按照以下步骤进行操作: 1. 定义内表(table):首先,你需要定义一个内表来存储数据和复选框的状态。可以使用`TYPES`关键字定义一个包含所需字段的结构。 2. 填充内表数据:将需要显示的数据填充到内表中。可以使用`APPEND`语句将数据行添加到内表中。 3. 创建SALV对象:使用`CL_SALV_TABLE`类创建一个SALV对象,并将内表作为参数传递给构造方法。 4. 添加复选框列:通过调用`ADD_CHECKBOX_COLUMN`方法,为SALV对象添加一个复选框列。可以指定该列的标题和字段名。 5. 显示报表:使用`DISPLAY`方法显示SALV报表。 下面是一个简单的示例代码: ```abap DATA: lt_data TYPE TABLE OF (your_structure), lo_salv TYPE REF TO cl_salv_table. * 定义内表类型 TYPES: BEGIN OF ty_data, field1 TYPE data_type, field2 TYPE data_type, checkbox TYPE abap_bool, END OF ty_data. * 填充内表数据 APPEND VALUE #( field1 = value1 field2 = value2 ) TO lt_data. * 创建SALV对象 lo_salv = cl_salv_table=>factory( ). * 添加复选框列 lo_salv->add_checkbox_column( title = 'CheckBox' fieldname = 'CHECKBOX' ). * 设置内表数据 lo_salv->set_table_for_first_display( exporting i_structure_name = 'TY_DATA' changing it_outtab = lt_data ). * 显示报表 lo_salv->display( ). ``` 这段代码将在SALV报表中显示一个复选框列,用户可以选择或取消选择复选框。请根据你的实际需求修改字段名称、数据类型和内表数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花鸟風月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值