SAP ABAP用OO方式实现ALV

ABAP用OO方式实现ALV 

report  ytest_w_001.

*" 功能A: 基本显示

*" 功能B: 设定layout

*" 功能C: 设定fieldcat

*" 功能D: 增加复选框

*" 功能E: 设定某列可输入

*" 功能F: 设定某列为下拉列表

*" 功能G: 设定屏蔽标准ALV 工具栏的按钮

*" 功能H: 设定增加自定义ALV工具栏的按钮

*" 功能I: 设定响应双击

*" 功能J: 设定响应user command

*" 功能K: 设定TOP_OF_PAGE

*" 功能L: 设定动态修改layout

*" 功能M: 设定动态修改fieldcat

代码诠释:

data: begin of gt_user occurs 0."LIKE TABLE OF usr02." with header line.
        include structure usr02.
data: checkbox(1)," 功能D: 增加复选框
      color(4).
data: end of gt_user.
 
data: g_container type ref to cl_gui_custom_container." 功能A: 基本显示
data: g_grid type ref to cl_gui_alv_grid." 功能A: 基本显示
data: save_ok like sy-ucomm,
      ok_code like sy-ucomm.
data: gt_fieldcat type lvc_t_fcat, " 功能C: 设定fieldcat
      gs_fieldcat type lvc_s_fcat." 功能C: 设定fieldcat
data: ls_layout  type lvc_s_layo. " 功能B: 设定layout
data: gt_exclude type ui_functions." 功能G: 设定屏蔽标准ALV 工具栏的按钮
 
*----------------------------------------------------------------------*
*       CLASS lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_handler definition.
  public section.
    methods handle_double for event double_click of cl_gui_alv_grid importing e_row e_column . " 功能I: 设定响应双击
    methods handle_toolbar for event toolbar of cl_gui_alv_grid importing e_object. " 功能H: 设定增加自定义ALV工具栏的按钮
    methods handle_user_command for event user_command of cl_gui_alv_grid importing e_ucomm. " 功能J: 设定响应user command
    methods handle_top for event top_of_page of cl_gui_alv_grid. " 功能K: 设定TOP_OF_PAGE
 
endclass.                    "lcl_event_handler DEFINITION
 
*----------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_handler implementation.
 
  method handle_double. " 功能I: 设定响应双击
    message i000(ih) with e_row-index e_column-fieldname.
  endmethod.                    "handle_double
 
  method handle_toolbar. " 功能H: 设定增加自定义ALV工具栏的按钮
    data: ls_toolbar type stb_button.
    ls_toolbar-butn_type = '3'.
    append ls_toolbar to e_object->mt_toolbar.
 
    clear ls_toolbar.
    ls_toolbar-text = 'Click'.
    ls_toolbar-function = 'Z1'.
    ls_toolbar-icon = icon_calculation.
*QUICKINFO
*BUTN_TYPE
    append ls_toolbar to e_object->mt_toolbar.
  endmethod.                    "handle_toolbar
 
  method handle_user_command. " 功能J: 设定响应user command
    case e_ucomm.
      when 'Z1'.
        message i000(ih) with 'Y'.
    endcase.
  endmethod.                    "handle_user_command
 
  method handle_top.           " 功能K: 设定TOP_OF_PAGE
    write:/ 'this is end'.
    message i000(ih) with 'hi'.
  endmethod.
 
endclass.                    "lcl_event_handler IMPLEMENTATION
 
 
data: gr_event type ref to lcl_event_handler.
 
start-of-selection.
 
  perform frm_get_data.
  call screen 100.
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module status_0100 output.
  set pf-status '100'.
  set titlebar '100'.
  if g_container is initial.
    create object g_container" 功能A: 基本显示
      exporting
*      parent                      =
        container_name              = 'GC_1'        .
 
 
    create object g_grid" 功能A: 基本显示
      exporting
        i_parent          = g_container
      exceptions
        error_cntl_create = 1
        error_cntl_init   = 2
        error_cntl_link   = 3
        error_dp_create   = 4
        others            = 5        .
 
    gs_fieldcat-fieldname = 'CHECKBOX'. " 功能D: 增加复选框
    gs_fieldcat-coltext = 'CHECK'.      " 功能D: 增加复选框
    gs_fieldcat-checkbox = 'X'.         " 功能D: 增加复选框
    gs_fieldcat-edit = 'X'.             " 功能E: 设定某列可输入
    append gs_fieldcat to gt_fieldcat.  " 功能D: 增加复选框
 
    call function 'LVC_FIELDCATALOG_MERGE' " 功能C: 设定fieldcat
     exporting
*       I_BUFFER_ACTIVE              =
       i_structure_name             = 'USR02'
*       I_CLIENT_NEVER_DISPLAY       = 'X'
*       I_BYPASSING_BUFFER           =
*       I_INTERNAL_TABNAME           =
      changing
        ct_fieldcat                  = gt_fieldcat
     exceptions
       inconsistent_interface       = 1
       program_error                = 2
       others                       = 3 .
 
    data: lt_drop type lvc_t_drop,
          ls_drop type lvc_s_drop.
 
    ls_drop-handle = '1'. " 功能F: 设定某列为下拉列表
    ls_drop-value = 'A'.  " 功能F: 设定某列为下拉列表
    append ls_drop to lt_drop. " 功能F: 设定某列为下拉列表
    ls_drop-handle = '1'. " 功能F: 设定某列为下拉列表
    ls_drop-value = 'B'.  " 功能F: 设定某列为下拉列表
    append ls_drop to lt_drop." 功能F: 设定某列为下拉列表
 
    ls_drop-handle = '2'." 功能F: 设定某列为下拉列表
    ls_drop-value = 'ZZZ'." 功能F: 设定某列为下拉列表
    append ls_drop to lt_drop." 功能F: 设定某列为下拉列表
    ls_drop-handle = '2'." 功能F: 设定某列为下拉列表
    ls_drop-value = 'YYY'." 功能F: 设定某列为下拉列表
    append ls_drop to lt_drop." 功能F: 设定某列为下拉列表
    ls_drop-handle = '2'." 功能F: 设定某列为下拉列表
    ls_drop-value = 'XXX'." 功能F: 设定某列为下拉列表
    append ls_drop to lt_drop." 功能F: 设定某列为下拉列表
 
    loop at gt_fieldcat into gs_fieldcat.
      if gs_fieldcat-fieldname = 'ANAME'.
        gs_fieldcat-emphasize = 'C501'.
        gs_fieldcat-drdn_hndl = '1'.   " 功能F: 设定某列为下拉列表
        gs_fieldcat-edit = 'X'.        " 功能E: 设定某列可输入
        modify gt_fieldcat from  gs_fieldcat.
      elseif gs_fieldcat-fieldname = 'BNAME'.
        gs_fieldcat-emphasize = 'C501'.
        gs_fieldcat-drdn_hndl = '2'.  " 功能F: 设定某列为下拉列表
        gs_fieldcat-edit = 'X'.       " 功能E: 设定某列可输入
        modify gt_fieldcat from  gs_fieldcat.
      endif.
    endloop.
    call method g_grid->set_drop_down_table " 功能F: 设定某列为下拉列表
      exporting
        it_drop_down = lt_drop.
*    it_drop_down_alias =
    .
    create object gr_event.
    set handler gr_event->handle_double for g_grid. " 功能I: 设定响应双击
    set handler gr_event->handle_toolbar for g_grid. " 功能H: 设定增加自定义ALV工具栏的按钮
    set handler gr_event->handle_user_command for g_grid. " 功能J: 设定响应user command
    set handler gr_event->handle_top for g_grid. " 功能K: 设定TOP_OF_PAGE
 
    ls_layout-zebra = 'X'.            " 功能B: 设定layout
    ls_layout-grid_title = 'USER'.    " 功能B: 设定layout
    ls_layout-smalltitle = 'X'.       " 功能B: 设定layout
    ls_layout-box_fname = 'CHECKBOX'." 功能B: 设定layout
    ls_layout-sel_mode = ''.          " 功能B: 设定layout
*    ls_layout-no_TOOLBAR = 'X'.
 
    data gs_exclude type ui_func. " 功能G: 设定屏蔽标准ALV 工具栏的按钮
    gs_exclude = cl_gui_alv_grid=>mc_mb_sum. " 功能G: 设定屏蔽标准ALV 工具栏的按钮
    append gs_exclude to gt_exclude. " 功能G: 设定屏蔽标准ALV 工具栏的按钮
 
    call method g_grid->set_table_for_first_display     " 功能A: 基本显示
        exporting
*          i_buffer_active               =
*          i_bypassing_buffer            =
*          i_consistency_check           =
*          i_structure_name              =
*          is_variant                    =
*          i_save                        = 'X'
*          i_default                     = 'X'
          is_layout                     = ls_layout   " 功能B: 设定layout
*          is_print                      =
*          it_special_groups             =
          it_toolbar_excluding          = gt_exclude " 功能G: 设定屏蔽标准ALV 工具栏的按钮
*          it_hyperlink                  =
*          it_alv_graphics               =
*          it_except_qinfo               =
*          ir_salv_adapter               =
            changing
              it_outtab                     = gt_user[]
          it_fieldcatalog               = gt_fieldcat[] " 功能C: 设定fieldcat
*          it_sort                       =
*          it_filter                     =
            exceptions
              invalid_parameter_combination = 1
              program_error                 = 2
              too_many_lines                = 3
              others                        = 4
                  .
  else.
    call method g_grid->refresh_table_display " 功能A: 基本显示
*  EXPORTING
*    is_stable      =
*    i_soft_refresh =
      exceptions
        finished       = 1
        others         = 2
            .
  endif.
endmodule.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Form  FRM_GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_get_data .
  select * from usr02 into table gt_user.
endform.                    " FRM_GET_DATA
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module user_command_0100 input.
  save_ok = ok_code.
  clear ok_code.
  case save_ok.
    when 'BACK'.
      leave to screen 0.
    when 'SO_UP'.
      perform frm_hide_toolbar.
    when 'CH_CAT'.
      perform frm_change_caption.
    when others.
  endcase.
endmodule.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Form  FRM_SORT_UP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_hide_toolbar .
  data: ls_layout type lvc_s_layo.
 
  call method g_grid->get_frontend_layout
    importing
      es_layout = ls_layout.
 
  if ls_layout-no_toolbar = 'X'.
    ls_layout-no_toolbar = ''.
  else.
    ls_layout-no_toolbar = 'X'.
  endif.
 
  call method g_grid->set_frontend_layout
    exporting
      is_layout = ls_layout.
 
endform.                    " FRM_SORT_UP
*&---------------------------------------------------------------------*
*&      Form  FRM_CHANGE_CAPTION
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_change_caption .
  data: lt_fieldcat type lvc_t_fcat,
        ls_fieldcat like line of lt_fieldcat.
  call method g_grid->get_frontend_fieldcatalog
    importing
      et_fieldcatalog = lt_fieldcat.
 
  loop at lt_fieldcat into ls_fieldcat.
    if ls_fieldcat-fieldname = 'BNAME'.
      ls_fieldcat-coltext = 'X'.
      ls_fieldcat-seltext = 'SELECT'.
      modify lt_fieldcat from ls_fieldcat.
    endif.
  endloop.
 
  call method g_grid->set_frontend_fieldcatalog
    exporting
      it_fieldcatalog = lt_fieldcat.
 
endform.                    " FRM_CHANGE_CAPTION

  • 15
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 要使用OOALVABAP中生成对SFLIGHT数据库的报表,您可以按照以下步骤进行: 1. 在SE11中创建一个结构来定义您的报表列和行。结构必须与SFLIGHT数据库表中的列相匹配。 2. 在SEGW中创建一个OData服务,用于从SFLIGHT数据库中检索数据。 3. 在ABAP类中创建一个方法来获取OData服务返回的数据。 4. 在ABAP类中创建一个方法来定义您的ALV报表,并使用该方法创建ALV实例。 5. 在ALV实例中,将数据传递给ALV,并设置ALV字段目录和布局,以便在报表中正确显示数据。 6. 最后,您可以在ABAP程序中调用该类的方法来生成您的报表。 通过以上步骤,您可以在ABAP中使用OOALV来生成对SFLIGHT数据库的报表。 ### 回答2: 在ABAP实现OOALV(对象导向的ALV报表)方式生成对SFLIGHT数据库的报表,需要完成以下步骤: 1. 定义一个ALV树控制对象,用于展示报表的结构和布局。可以使用ABAP类的方法`cl_gui_alv_tree`来创建该对象。 2. 创建一个视图对象,用于检索和处理SFLIGHT数据库表中的数据。可以使用ABAP类的方法`cl_abap_view`来创建该对象,并将SFLIGHT作为目标数据库表。 3. 在ABAP程序中,通过实例化ALV树控制对象,设置相应的参数,例如标题、字段、列宽等。 4. 实例化视图对象,并使用`set_database_table`方法将SFLIGHT表与视图对象关联起来。 5. 使用`set_table_for_first_display`方法将ALV树控制对象与视图对象关联起来,完成报表的生成。 6. 根据需要可以通过方法`add_toolbar`向报表添加自定义工具栏按钮。 7. 最后,使用`display`方法显示报表。 简而言之,通过创建ALV树控制对象和视图对象,并将它们关联起来,然后添加所需的参数,最后显示生成的报表。这样就可以实现OOALV方式生成对SFLIGHT数据库的报表。 ### 回答3: 在ABAP中,可以通过使用OOALV(对象导向的ALV)的方式来生成对SFLIGHT数据库的报表。以下是实现方式的步骤: 1. 首先,创建一个ABAP程序,并导入必要的数据字典表和SFLIGHT数据库表。 2. 创建一个ABAP类来定义报表的逻辑和功能。这个类需要继承CL_SALV_TABLE类,以便使用ALV控件。 3. 在该类中,定义必要的数据对象和变量,以便在报表中存储和处理数据。 4. 实现一个方法来初始化和配置ALV控制对象。在该方法中,创建一个ALV对象,并将所有必要的属性和设置应用于该对象。例如,可以设置表格布局、列标题、排序和筛选选项等。 5. 实现一个方法来获取SFLIGHT数据库表中的数据,并将其存储在报表使用的数据对象中。可以使用SELECT语句从SFLIGHT表中检索数据,并将其填充到数据对象中。 6. 在报表类中实现另一个方法来显示和渲染报表。在该方法中,使用ALV控制对象显示报表,并通过将数据对象中的数据与ALV对象相关联来填充报表。 7. 最后,在报表程序中实例化/创建报表类的对象,并调用显示和渲染报表的方法。这将在屏幕上显示生成的报表。 通过这些步骤,可以在ABAP实现OOALV方式来生成对SFLIGHT数据库的报表。这种方式可以提供更灵活和可扩展的报表生成功能,同时具有更好的用户界面和功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ᝰ随心ꦿེএ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值